SSH-keys are useful when authenticating via SSH to a server. Keys are generally a lot more secure than using plain-text passwords.

To generate a key, type in the following command.

ssh-keygen -t rsa -b 4096

You can append the -c flag to the command to add a comment to the key (useful if using several keys).

ssh-keygen -t rsa -b 4096 -C "my comment here"

The generator will ask you to enter a path where the key should be stored. Just press enter to use the default one (if this is a second key, you should set the path to something else).

Enter a file in which to save the key (/home/user/.ssh/id_rsa): [Press enter]

It will also ask you to set a password. It’s strongly recommended to set a password.

Your key is now generated. To use the key for SSH authentication, you will need to add it to your ssh-agent.

Start the agent if it’s not already running.

eval "$(ssh-agent -s)"

Then add the key to the agent. Replace filename id_rsa if you set it to something else earlier.

ssh-add ~/.ssh/id_rsa

Congratulations! You now have a working SSH key.