SSH stands for Secure Shell and it is used widely as a means of accessing remote servers. The usage of SSH is recommended because it is considered a highly secure way of authenticating users remotely. Normally, we use passwords to authenticate users, however, passwords are prone to different security attacks. Therefore, they are not much recommended. A good alternative to the password-based approach for authenticating users is the use of SSH keys. Since these keys are encrypted, that is why they are considered a more secure means of user authentication. Therefore, in this article, we will be learning how to create or generate SSH keys in Ubuntu and Linux Mint.
Generating SSH Keys in Ubuntu
For generating SSH keys in Ubuntu, you will need to perform the following steps:
Launch the terminal in Ubuntu by clicking on the terminal icon located on your taskbar. The newly opened terminal window is shown in the image below:
For generating your SSH key pair, type the following command in your terminal and then press the Enter key:
The keygen command essentially generates an RSA key pair of 2048 bits. If you want to double the size of this key pair i.e. 4096 bits for enhanced security, you can add the –b flag at the end of this command i.e. your command will become ssh-keygen -b. However, if you are fine with a 2048 bits RSA key pair, then you can go with the default keygen command. It is also shown in the following image:
As soon as you will run this command, your terminal will notify you that your system is generating the RSA key pair. Meanwhile, it will also prompt you to choose a destination file for saving your newly generated key pair. You can give any location of your choice over here and then press the Enter key. However, if you want to go with the default provided location, then you can simply press the Enter key without typing anything to proceed as shown in the image below:
Now you will be asked to set up a passphrase if you want for enhancing the security of your RSA key pair even more. Although it is optional it is highly recommended that you set up a passphrase. It acts as an added layer of security for your RSA key pair and user authentication. However, if you do not want to do it, you can simply press the Enter key to proceed without typing anything. In this example, we have set a passphrase and then pressed the Enter key as shown in the following image:
Once you have entered your passphrase successfully, you will be asked to re-enter it for confirmation. Enter the same passphrase once again and then press the Enter key as shown in the image below:
As soon as you will press the Enter key, your terminal will notify you that your identification information and the public key has been saved. It will also share your key fingerprint and your key’s random art image with you. This is shown in the following image:
If you want to verify whether a key pair has actually been generated or not, you can simply navigate to the location you have provided for generating your key pair and then list down its contents. In this example, since we have proceeded with the default location provided by our system, therefore, we will move to that directory. For doing that, type the following command in your terminal and then press the Enter key:
Here, you will be given the path that you initially chose for generating your key pair. This command is shown in the image below:
Once you are in the desired directory, type the following command in your terminal and then press the Enter key for listing down its contents:
This command is also shown in the following image:
As soon as you will run this command, your terminal will display the contents of this directory which in this case will be your public/private key pair as shown in the image below:
By using the easy and straightforward steps discussed in this article, we can conveniently generate an SSH key pair for our authentication while using Ubuntu Linux. The best thing about this method is that it literally takes just a few minutes to complete, however, its benefits are huge in terms of security.