Generate SSH Key Pair and Connect to Ubuntu with Putty

In this blog post, we will explore the process of creating SSH keys on an Ubuntu server and using these keys to connect to the server via Putty. Initially, we will connect to the server using root as the username and password-based authentication.

Once connected, use the following command to generate an SSH key pair. I’ve included an example command with the comment “test01,” which you can modify according to your preference. The comment serves as a reference for the key pair.

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 press Enter to skip without a Password.

generate an SSH key pair
generate an SSH key pair
  • Type the command ls -al to check the .ssh folder.
 ls -al to check the .ssh folder
ls -al to check the .ssh folder
  • Change the folder location to .ssh using cd .ssh, and then ls-al to verify if SSH Key Pair files are created.
Change the folder location to .ssh by using cd .ssh
Change the folder location to .ssh by using cd .ssh
  • Next, copy the SSH public key into the authorized_keys file. Run the following command if you don’t see an authorized_keys file in the .ssh folder. This command will create the authorized_keys file and copy the SSH public key.
cat ~/.ssh/id_rsa.pub >> authorized_keys
cat authorized_keys
copy the SSH public key into the "authorized_keys" file
copy the SSH public key into the “authorized_keys” file

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

Download Putty.exe, PSCP.exe and PuttyGen.exe
Download Putty.exe, PSCP.exe and PuttyGen.exe
  • Once you have downloaded all three executable files and saved them in C:\temp, you must download the private key from the server using the following command.
  • Create a folder named SSHKeys or any name you prefer. When you run this command, it will download the “id_rsa” file to your PC. Make sure to replace the IP address of the server with the IP address of your server.
pscp.exe root@137.184.218.96:/root/.ssh/id_rsa "c:\temp\SSHKeys"
download the "id_rsa" file to your PC
download the “id_rsa” file to your PC
  • File id_rsa downloaded under C:\temp\SSHKeys folder.
File id_rsa downloaded
File id_rsa downloaded
  • If you have saved puttygen.exe in the C:\temp directory, launch the puttygen.exe program from the command prompt by typing “puttygen.exe” and pressing Enter. Alternatively, you can double-click on the executable file to open it. Once the program opens, click “Conversions,” then select “Import key.”
Import key
Import key
  • Select the file id_rsa downloaded in C:\temp\SSHKeys.
Select the file id_rsa downloaded
Select the file id_rsa downloaded
  • The next step is to click on Save Private Key. Click on Yes if you want to save this key without a passphrase. Alternatively, you can provide a key passphrase and click Save private key.
Save Private Key
Save Private Key
  • Provide a name for the private key and save it on your system. It will be saved as a .ppk file, which you will use to connect to the server using the Putty program. For example, you can name it “server_private_key.”
Provide a name for the private key
Provide a name for the private key
  • Launch Putty.exe and then provide the Host Name. In my scenario, it’s root@137.184.218.96. Keep Port 22 as it is. Change the IP address to your server’s IP Address.
Launch Putty.exe and then provide the Host Name
Launch Putty.exe and then provide the Host Name
  • Click on SSH and then Click on Auth. Browse to the Private Key saved in the previous step. In my scenario, it’s server_private_key.ppk file.
Click on SSH and then Click on Auth
Click on SSH and then Click on Auth
  • To save time, you can save this session by giving it a name, for example, “MyServer_Session_Host,” and then clicking the “Save” button.
  • The next time you launch Putty, you won’t need to provide the private key file each time because it will be saved in this session. You can select the session, click Load, and click Open to connect to your server.
click "Load," and then click "Open"
click “Load,” and then click “Open
  • After clicking the Open button, Putty will connect to the server using the specified username and SSH key. The screenshot below shows that the authentication is completed using the public key test01.
Authentication is completed using the public key
Authentication is completed using the public key

Conclusion

Generating an SSH key pair and using it for server connections is a straightforward process. Once you’ve connected to the server using an SSH key, you can further enhance security by disabling password-based authentication.

If you decide to use a passphrase while creating or saving the private key, ensure it is a strong and complex one for added security. Additionally, store the private key in a secure location to prevent unauthorized access.

Leave a Comment