How to generate SSH Key pair and connect to Ubuntu using SSH keys via Putty

In this blog post, we will see how to create SSH Keys on an Ubuntu server and then use the SSH keys to connect to the server using Putty. First Lets connect to the server by using username root and with password based authentication. Once you are connected to the server. Type below command to generate SSH Key Pair. I have used a command with Comment “test01”, you can change it to provide whichever comment value is suitable. The comment value is just for reference purpose.

ssh-keygen -t rsa -b 4096 -C "test01"

After entering this command, Press Enter to keep the same file name as shown which is id_rsa. You can either enter a passphrase to secure the SSH key pair or just press Enter to skip without a Password.

connect to Ubuntu using SSH keys via Putty

Type command ls -al to check the .ssh folder.

connect to Ubuntu using SSH keys via Putty

Change the folder location to .ssh by using cd .ssh and then use ls -al to verify SSH Key Pair files created.

connect to Ubuntu using SSH keys via Putty

Next you will need to copy the SSH Public Key into authorized_key file. Even if you do not see authorized_keys file under .ssh folder. You can still run this below command this will create authorized_keys file and copy the SSH Public Key.

cat ~/.ssh/id_rsa.pub >> authorized_keys
cat authorized_keys
connect to Ubuntu using SSH keys via Putty

Download Putty.exe, PSCP.exe and PuttyGen.exe

Download all three programs from https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html link and save it somewhere on your system for example under C:\temp.

connect to Ubuntu using SSH keys via Putty

Once you have download all three exe’s and saved it in C:\temp. We need to download the private key from the server using below command. Create a folder named SSHKeys or any name you like. Once you run this command it will download id_rsa file on your PC. Replace the IP Address of the server with your server IP Address.

pscp.exe root@137.184.218.96:/root/.ssh/id_rsa "c:\temp\SSHKeys"
connect to Ubuntu using SSH keys via Putty

File id_rsa download under C:\temp\SSHKeys folder.

connect to Ubuntu using SSH keys via Putty

If you have saved puttygen.exe under C:\temp then launch puttygen.exe program from command prompt by typing puttygen.exe and press ener or you can also double click on it to open the exe file. Once the program is opened click on Conversations and then click on Import key.

connect to Ubuntu using SSH keys via Putty

Select the file id_rsa downloaded in C:\temp\SSHKeys.

connect to Ubuntu using SSH keys via Putty

Next step is to click on Save Private Key. Click on Yes if you want to save this key without a passphrase or you can also provide a key passphrase and then Save private key.

connect to Ubuntu using SSH keys via Putty

Provide a Name of the Private key and save it on your system. Its a .ppk file which will be used to connect to the server using Putty program. For Example: server_private_key.

connect to Ubuntu using SSH keys via Putty

Launch Putty.exe and then provide the Host Name. In my case its root@137.184.218.96. Keep the Port 22 as it is. Change the IP address to your server IP Address.

connect to Ubuntu using SSH keys via Putty

Click on SSH and then Click on Auth. Browse to the Private Key saved in previous step. In my case its server_private_key.ppk file.

connect to Ubuntu using SSH keys via Putty

To save the time, you can also save this session by providing a name for example: MyServer_Session_host and then click on Save button. Next time you will launch Putty, you will not have to provide the private key file each time as it will be saved in this session. You can simply select the session and click on Load and then click on Open to connect to your server.

connect to Ubuntu using SSH keys via Putty

After clicking on Open button, Putty will connect to the server using the username and SSH Key. As you can see from below screenshot, the authentication is completed with public key “test01”

connect to Ubuntu using SSH keys via Putty

Conclusion

Its very easy to generate SSH Key pair and use that to connect to the Ubuntu server. Once you have connected to the server using SSH Key, you can also disable password based authentication which will provide additional security to the server. I have not used the passphrase while creating or saving the private key. Its recommended that you provide a secure and complex password while saving the private key so that it cannot be misused. Also, make sure to save the private key in a secure location.