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. To start, we will initially 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 just press Enter to skip without a Password.
- Type the 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’ll need to copy the SSH public key into the “authorized_keys” file. If you don’t see an “authorized_keys” file in the “.ssh” folder, you can run the following command. This command will create the “authorized_keys” file and copy the SSH public key into it.
cat ~/.ssh/id_rsa.pub >> authorized_keys
cat 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 downloaded all three executable files and saved them in C:\temp, you need to 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"
- File id_rsa downloaded under C:\temp\SSHKeys folder.
- 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 also double-click on the executable file to open it. Once the program is open, click on “Conversions,” and then select “Import key.”
- Select the file id_rsa downloaded in C:\temp\SSHKeys.
- 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 also provide a key passphrase and then click “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.”
- 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.
- Click on SSH and then Click on Auth. Browse to the Private Key saved in the previous step. In my case it’s server_private_key.ppk file.
- To save time, you can also 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 simply select the session, click “Load,” and then click “Open” to connect to your server.
- After clicking the “Open” button, Putty will connect to the server using the specified username and SSH key. As shown in the screenshot below, the authentication is completed using the public key “test01.”
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.