Configure _No Password SSH Keys Authentication_ with PuTTY on Linux Servers


SSH (Secure Shell) is a popular protocol for securely connecting to remote servers over a network. When you use SSH to connect to a server, you typically need to provide a username and password to authenticate your identity. However, this process can be tedious if you need to connect to multiple servers frequently. Fortunately, there is a solution: No Password SSH Keys Authentication.

In this article, we'll walk you through the process of setting up No Password SSH Keys Authentication on a Linux server using PuTTY, a popular SSH client for Windows. With this setup, you can connect to your servers quickly and securely without needing to type in a password every time.

Prerequisites

Before we begin, make sure you have the following −

  • A Linux server to which you want to connect using SSH.

  • PuTTY installed on your Windows machine. You can download PuTTY from the official website.

  • Basic knowledge of Linux commands and how to use the command line.

Why use No Password SSH Keys Authentication?

There are several benefits to using No Password SSH Keys Authentication −

  • Improved security − SSH keys provide a more secure way to authenticate than passwords, as they are much more difficult to guess or crack.

  • Convenience − With SSH keys, you don't need to remember or enter a password every time you connect to a server.

  • Automation − SSH keys can be used to automate tasks on remote servers, such as running scripts or executing commands.

Generating SSH keys with PuTTYgen

To use No Password SSH Keys Authentication, you'll need to generate a public/private key pair. PuTTYgen is a tool used to generate and manage SSH keys with PuTTY.

Follow these steps to generate an SSH key with PuTTYgen −

  • Download and install PuTTY from the official website.

  • Open PuTTYgen by searching for it in the Start menu.

  • Click the "Generate" button to create a new key pair. Move your mouse around the blank area to generate random data for the key.

  • Set a passphrase for your private key. This passphrase will be required to use the private key, so make sure to remember it.

  • Click the "Save private key" button to save your private key. Choose a secure location to save the file.

  • Copy the contents of the "Public key for pasting into OpenSSH authorized_keys file" field to your clipboard. This is your public key.

Adding the public key to the authorized_keys file on the Linux server

Now that you have generated your SSH key pair, you'll need to add the public key to the authorized_keys file on the Linux server you want to connect to. Follow these steps −

  • Log in to the Linux server using your password.

  • Create a .ssh directory in your home directory if it doesn't already exist. Run the following command − mkdir -p ~/.ssh.

  • Open the authorized_keys file in a text editor. Run the following command: nano ~/.ssh/authorized_keys.

  • Paste the contents of the public key you copied earlier into the authorized_keys file.

  • Save and close the file by pressing Ctrl-X, then Y, then Enter.

  • Set the permissions for the .ssh directory and authorized_keys file by running the following commands −

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Setting permissions for the .ssh directory and authorized_keys file on the Linux server

For SSH to work correctly, the .ssh directory and authorized_keys file on the Linux server need to have the correct permissions. Follow these steps to set the permissions correctly −

  • Log in to the Linux server using your password.

  • Navigate to your home directory by running the following command: cd ~.

  • Set the permissions for the .ssh directory by running the following command: chmod 700 ~/.ssh.

  • Set the permissions for the authorized_keys file by running the following command: chmod 600 ~/.ssh/authorized_keys.

Using SSH agent forwarding with PuTTY

SSH agent forwarding is a useful feature that allows you to use your SSH keys on a remote server. This is useful if you need to connect to multiple servers using the same key, or if you need to perform multiple SSH hops to reach a remote server.

To enable SSH agent forwarding, you need to perform the following steps −

  • Check if SSH agent forwarding is enabled on your local machine −

    Run the following command on your local machine to check if SSH agent forwarding is enabled −

$ echo "$SSH_AUTH_SOCK"

    If the output is a path to a socket file (e.g. /tmp/ssh-XXXXXXXX/agent.XXXXXX), then SSH agent forwarding is already enabled.

  • Enable SSH agent forwarding on the remote server −

    You need to add the following line to the SSH configuration file (/etc/ssh/sshd_config) on the remote server −

AllowAgentForwarding yes

    After making this change, you need to restart the SSH service on the remote server −

$ sudo systemctl restart sshd
  • Connect to the remote server with SSH agent forwarding enabled −

    To connect to the remote server with SSH agent forwarding enabled, use the -A option with the ssh command −

$ ssh -A remote_server

    Once you are connected to the remote server, you can use your SSH key to connect to other servers that recognize the key.

    For example, suppose you need to connect to a second remote server (second_remote_server) that recognizes your SSH key. You can connect to it from the remote server (remote_server) using the following command −

Configure PuTTY to Use Private Key

The final step is to configure PuTTY to use your private key when connecting to the Linux server. Here's how to do it −

  • Open PuTTY, and go to the "Session" category.

  • Enter the hostname or IP address of the Linux server in the "Host Name (or IP address)" field.

  • Under the "Connection" category, expand the "SSH" subcategory, and click on "Auth".

  • Click the "Browse" button next to the "Private key file for authentication" field, and select the private key file that you saved in Step 1.

  • Click the "Open" button to start the SSH session.

You should now be able to connect to the Linux server without needing to enter a password. PuTTY will automatically use the private key for authentication.

Troubleshooting

If you are unable to connect to the Linux server using your private key, try the following troubleshooting steps −

  • Make sure the public key is added to the authorized_keys file on the Linux server correctly.

  • Make sure the permissions for the .ssh directory and authorized_keys file on the Linux server are set correctly.

  • Make sure the private key file is saved in the correct format (PuTTY uses the .ppk format by default).

  • Make sure the private key file is selected correctly in PuTTY.

Conclusion

No Password SSH Keys Authentication is a convenient way to connect to Linux servers quickly and securely. By following the steps outlined in this article, you should now be able to connect to your Linux servers using PuTTY without needing to enter a password. If you run into any issues, make sure to double-check your settings and permissions.

Updated on: 26-Jun-2023

236 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements