Passwordless login using SSH keys offers a secure and convenient way to connect to a remote server. It eliminates the need to enter a password each time you log in while providing a higher level of security. In this article, you will learn how to configure SSH keys for passwordless login.
Generating SSH Keys
The first step is to generate a pair of SSH keys (public and private) on your local computer.
- Open a terminal.
- Use the
ssh-keygen
command to create a new key pair. You can specify the key type using the-t
parameter, for example,ssh-keygen -t rsa -b 4096
, where-b
specifies the key length. - During the process, you will be prompted to enter the location where you want to save the keys (usually
.ssh/id_rsa
) and optionally set a passphrase for the private key.
Uploading the Public Key to the Server
Next, you need to upload the public key to the target server.
- Use the
ssh-copy-id user@server
command, whereuser
is your username on the server andserver
is the server's address. This command will automatically add your public key to the~/.ssh/authorized_keys
file on the server. - If you cannot use
ssh-copy-id
, you can upload the public key manually. First, display the contents of the public key usingcat ~/.ssh/id_rsa.pub
, copy it, log in to the server, and paste it into the~/.ssh/authorized_keys
file.
SSH Server Configuration
To enable passwordless login, the server must be properly configured.
- Log in to the server.
- Open the SSH server configuration file using an editor, for example,
sudo nano /etc/ssh/sshd_config
. - Check or modify the following lines:
PasswordAuthentication no
disables password authentication.PubkeyAuthentication yes
enables authentication using public keys.
- After making changes, restart the SSH server, for example, with
sudo systemctl restart ssh
.
Testing Passwordless Login
Finally, test if passwordless login works:
- Try to connect to the server using
ssh user@server
. - If you set a passphrase for your private key, you will be prompted to enter it. Otherwise, the connection should be established without entering a password.
Setting up passwordless login using SSH keys not only enhances the security of your connection to a remote server but also significantly simplifies the login process. By following the steps outlined above, you can easily configure SSH keys and enjoy the convenience of passwordless access.