How to generate SSH keys and use to login remote server

SSH or Secure Shell is network protocol that allows users to connect with remote server using command line. To login with SSH, you can either use remote server password or you can use SSH keys which doesn't require to input password on connection.

To connect with remote server, first you need to generate SSH keys in your local system. Then you need to upload keys in your account in provider.

Run the below OpenSSH command to generate SHA256 RSA key pair in your local system.

ssh-keygen

This will ask you to select the location where you want to generate keys. The default path is ~/.ssh. SSH automatically check the keys at this location so it is advised to keep default location.

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa): 

Then it will ask to enter and confirm passphrase. You can simply press enter to keep passphrase empty.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

Once you press the Enter SSH keys will be generated on the selected. The below out 

Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:p1lWzPRj0Nr2bEI/5bd6VHc user@Dell-XPS-PC
The key's randomart image is:
+---[RSA 3072]----+
|            .    |
|           + o   |
|           .= *. |
|           .o= +E|
|        S + .*=BB|
|         *  .+B+O|
|        o   .+ O+|
|           o+ +.+|
|         += .o..o|
+----[SHA256]-----+

You can see ~/.ssh directory generated. In this directory id_rsa is private key, id_rsa.pub is your Public key and known_hosts with string.

Now you need to add your SSH public key in your account. You can add it through your providers account portal. For example, if you are using Digital Ocean server, then you can find option in Settings > Securiry.

When you click Add SSH Key, it will ask to input public key.

Paste the public key from /home/user/.ssh/id_rsa.pub and save it. Now connect with your remote server using ssh command.

ssh [email protected]

If everything goes right, you will connected with remote server without asking for password.

Tags: