How to Set up SSH Keys on Debian 11

Published on March 24, 2023 · Updated on March 24, 2023
How to Set up SSH Keys on Debian 11

SSH (Secure Shell) is a widely used protocol for secure remote login and other network services over an unsecured network. SSH keys are used to authenticate and establish secure connections between two machines. In this tutorial, we will learn how to set up SSH keys on Debian 9 to enable secure remote connections.

Step 1: Create the RSA Key Pair

The first step in setting up SSH keys is to create a key pair on the local machine. The key pair consists of a private key and a public key. The private key is kept on the local machine and the public key is shared with the remote server.

To create a key pair, open the terminal and run the following command:

ssh-keygen

This will generate a 2048-bit RSA key pair by default. You can optionally pass the -b flag to specify a different key size, for example:

ssh-keygen -b 4096

During the key generation process, you will be prompted to enter a passphrase to secure your private key. The passphrase adds an extra layer of security and makes it difficult for anyone who gains access to your private key to use it.

Copy the Public Key to Debian Server

After creating the SSH key pair, you need to copy the public key to the Debian server. There are several ways to do this, but the quickest and easiest way is to use the ssh-copy-id command.

ssh-copy-id username@server_ip_address

Alternatively, you can manually copy the public key to the remote server by adding it to the authorized_keys file in the ~/.ssh directory. You can use any text editor to add the public key to the file.

nano ~/.ssh/authorized_keys

Copy the public key and save the file by typing CTRL + O.

Authenticate to Debian Server Using SSH Keys

To authenticate to the Debian server using SSH keys, use the following command:

ssh username@server_ip_address

If you have configured a passphrase for your private key, you will be prompted to enter it. Once you have authenticated with your private key, you will be logged in to the Debian server.

Disable Password Authentication on your Server

For added security, you can disable password authentication on your Debian server and only allow SSH key authentication. To do this, open the sshd_config file in the /etc/ssh directory and add the following line:

PasswordAuthentication no

Save the file and restart the sshd service for the changes to take effect.

sudo systemctl restart sshd

Conclusion

In this tutorial, we learned how to set up SSH keys on Debian 11 for secure remote connections. SSH keys provide a secure and convenient way to authenticate and establish secure connections between two machines.

Load Comments