Generate SSH Keys in Ubuntu linux shell ubuntu

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.

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:

<img alt="Ubuntu Linux Console" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1043.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="101" loading="lazy" src="data:image/svg xml,” width=”657″>

For generating your SSH key pair, type the following command in your terminal and then press the Enter key:

ssh-keygen

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:

<img alt="SSH keygen command" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1044.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="96" loading="lazy" src="data:image/svg xml,” width=”653″>

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:

<img alt="SSH keypair" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1045.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="130" loading="lazy" src="data:image/svg xml,” width=”651″>

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:

<img alt="Key password" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1046.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="90" loading="lazy" src="data:image/svg xml,” width=”535″>

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:

<img alt="Repeat passphrase" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1047.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="109" loading="lazy" src="data:image/svg xml,” width=”542″>

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:

<img alt="random image" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1048.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="382" loading="lazy" src="data:image/svg xml,” width=”628″>

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:

cd /home/kbuzdar/.ssh

Here, you will be given the path that you initially chose for generating your key pair. This command is shown in the image below:

<img alt="List SSH key" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1049.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="26" loading="lazy" src="data:image/svg xml,” width=”417″>

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:

ls

This command is also shown in the following image:

<img alt="List directory contents" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1050.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="31" loading="lazy" src="data:image/svg xml,” width=”313″>

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:

<img alt="SSH public and private key" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1051.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="72" loading="lazy" src="data:image/svg xml,” width=”322″>

Conclusion

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.