How to Setup SSH Keys for your Linux Box

What are SSH Keys ? SSH Keys are based on the SSH cryptographic network protocol, which is responsible for the encryption of the information stream between you and the remote machine. Why should we use SSH Keys ? SSH keys provide a more secure way of logging into a virtual private server with SSH than using a password alone. While a password can eventually be cracked with a brute force attack, SSH keys are nearly impossible to decipher by brute force alone. sshkeys How does the SSH Key Login work ? The SSH Key is a key pair that provides you with two long string of characters: a public and a private key. The public key is kept on the server that is then unlock it by connecting to it with a client that already has the private key. When the two match up, the system unlocks without the need for a password. Is this safer than very complex text passwords ?  Hell yeah, see using public key pairs offers considerably more protection than using passwords or password lists which can be captured if the client, the server or the secure session is compromised. Now that you have an idea of how SSH Keys work and why should we use them lets see how to create and use them.

1  - Create the RSA Key Pair

The first step is to create the key pair on the client machine: We need to use the ssh-keygen utility that comes shipped with openshh tools. If you dont have it installed follow this tutorial - Install OpenSSH.
  • after you have installed OpenSSH run the following command:
ssh-keygen -t rsa
The output will be something like :
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/aodba/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/aodba/.ssh/id_rsa.
Your public key has been saved in /home/aodba/.ssh/id_rsa.pub.
The key fingerprint is:
4a:dd:0a:c6:35:4e:3f:ed:27:38:8c:74:34:4d:93:67
The key's randomart image is:
+--[ RSA 2048]----+
|          .oo.   |
|         .  o.E  |
|        + .  o   |
|     . = = .     |
|      = S = .    |
|     o + = +     |
|      . o + o .  |
|           . o   |
|                 |
+-----------------+
You can skip all questions and choose default values or you can provide your own options. I normally go with the default ones unless i have special passphrase i wanna add. The public key is now located in /home/aodba/.ssh/id_rsa.pub and the private key (identification) is now located in /home/aodba/.ssh/id_rsa

2 - Copy/Use the generated Key.

So what do i mean by Copy/Use ? Once the key pair is generated, it's time to place the public key on the virtual server that we want to use. You can copy the public key into the new machine's authorized_keys file.
cat ~/.ssh/id_rsa.pub | ssh user@123.12.12.123 "mkdir -p ~/.ssh  cat   ~/.ssh/authorized_keys"
  • so this will copy the key to the server you want to have access to your local(where the key was generated) server using the key.

3 - Test access using SSH Key

  •  from the Server(123.12.12.123) where you have copied the content of the id_rsa.pub into authorized_keys try to login into your initial server using the command bellow:
ssh user@<remove host

The authenticity of host 'server ip (server ip )' can't be established.
ECDSA key fingerprint is fd:fd:d4:f9:77:fe:73:84:e1:55:00:ad:d6:6d:22:fe.
Are you sure you want to continue connecting (yes/no)? yes
  • the "warning/message " that you see is because is the first time you do a handshake using the key with this server. After this you will no longer see this message as the host will be in the known_host file (this file make verifies the server's legitimacy).
Ok so setting up ssh key for Linux is done. I hope this was helpful and if you have doubts or encounter any issues setting up ssh keys fell free to drop a comment or question and i will do my best to answer back or help you as much as i can,