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.
Type command ls -al to check the .ssh folder.
Change the folder location to .ssh by using cd .ssh and then use ls -al to verify SSH Key Pair files created.
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
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.
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 email@example.com:/root/.ssh/id_rsa "c:\temp\SSHKeys"
File id_rsa download under C:\temp\SSHKeys folder.
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.
Select the file id_rsa downloaded in C:\temp\SSHKeys.
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.
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.
Launch Putty.exe and then provide the Host Name. In my case its firstname.lastname@example.org. Keep the Port 22 as it is. Change the IP address to your server IP Address.
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.
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.
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”
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.