How to generate SSH keys

In order to generate SSH keys in Ubuntu, we need the ssh-keygen utility. This utility comes with the openssh-client deb package, which should already be installed by default. And so, let's get started.

1. First you need to create a folder for storing ssh keys if it is not already there

 mkdir  ~/.ssh

2. Run the command to generate ssh keys

 ssh-keygen -t rsa -b 2048 -C igor-sverdlov@mail.ru
– The -t rsa option is responsible for using the RSA algorithm
– The -b 2048 option indicates that we want to generate a 2048-bit key
– Option -C igor-sverdlov@mail.ru comment to the key. Usually this is your email with a domain username.

The utility will ask several questions during the generation process. The first thing she will ask is where and under what name to save the key. If the default path suits you, and in most cases it is, then press Enter. If the default does not suit us, then we indicate our path and press Enter. The second is the password (at least 5 characters). If you want to create an ssh key without a password, you can just press enter. If you entered a password, the utility will ask you to confirm the password again. Here is the output of the utility
 ssh-keygen -t rsa -b 2048 -C igor-sverdlov@mail.ru
 Enter file in which to save the key (/home/madskill/.ssh/id_rsa):
 Enter passphrase (empty for no passphrase):
 Enter same passphrase again:
 Your identification has been saved in /home/madskill/.ssh/id_rsa.
 Your public key has been saved in /home/madskill/.ssh/id_rsa.pub.
 The key fingerprint is:
 SHA256:FrcsHSW/ATlQzIhOGdU8oT/CqNJGeWZWgWYY0XA0yag igor-sverdlov@mail.ru
 The key's randomart image is:
 +---[RSA 2048]----+
 |    +X=B+O=o.    |
 |    o.@..oO=     |
 |   . =  + ooo    |
 |  E  ..+ * o o   |
 |    o * S * .    |
 |   o * . o .     |
 |  . +            |
 |   o             |
 |                 |
 +----[SHA256]-----+
As a result, we will get two files
 ls ~/.ssh
 id_rsa  id_rsa.pub

It remains to set the correct file permissions and you're done.

 chmod 700 ~/.ssh
 chmod 600 ~/.ssh/id_rsa
 chmod 644 ~/.ssh/id_rsa.pub