How to Generate & Set Up SSH Keys on Debian

January 16, 2025

Introduction

Secure Shell (SSH) is a method to establish secure remote connections between two computers. SSH is usually used to log in and manage remote servers.

This guide will help you generate and set up an SSH key pair on Debian Linux.

How to Generate & Set Up SSH Keys on Debian

Prerequisites

Step 1: Check for Existing SSH Keys on Debian

If the system has previously connected to the remote server via SSH, it already has SSH keys. Check for pre-existing keys with the following command:

ls -l ~/.ssh
ls -l ~/.ssh no such file or directory terminal output

If the command responds that there is no such file or directory, there are no pre-existing SSH keys.

If the command lists files, it has an SSH key pair. To continue using this key pair, skip to Step 3. Alternatively, back up this key pair, then generate a new SSH key pair.

Step 2: Generate an SSH Key Pair on Debian

To create a public and private SSH key, follow the steps below:

1. In the terminal, enter the following command:

ssh-keygen

The command creates a 2048-bit RSA key pair by default. For added security, create a 4096-bit RSA key pair instead:

ssh-keygen -b 4096

2. The system processes the request and then prompts for the location to save the key pair.

ssh-keygen enter file default location terminal output

The default location is ~/.ssh/id_rsa. Press Enter to accept the default location or provide a different path or key name.

3. If a key already exists on this system, it prompts you to overwrite the old key. The system will no longer be able to authenticate using the old key. Press y to continue.

ssh-keygen overwrite prompt terminal output

4. Enter and confirm a passphrase. The passphrase is an added layer of SSH security that forces users to enter a password when connecting via SSH.

ssh-keygen enter passphrase terminal output

Set and confirm a passphrase, or leave it blank and press Enter to skip this step.

5. The command shows the identification and public key location, the key fingerprint, and the key's randomart image.

ssh-keygen location fingerprint randomart terminal output

The keys are now available in the ~/.ssh directory unless saved at a different location. The public key default file name is id_rsa.pub, while the private key is id_rsa.

Step 3: Copy the Public Key to the Remote Server

There are two ways to copy the public key to the remote server. The SSH utility has a copy function, which is helpful for remote servers. Alternatively, copy the key manually if you have physical access to the remote server.

Copy Public Key Using SSH

Copy the public key to the remote server:

1. Enter the following command:

ssh-copy-id [username]@[hostname_or_IP]

Replace [username] and [hostname_or_IP] with the values for the remote server.

2. The system now attempts to connect to the remote system using provided credentials. On the first connection, the system responds that the host's authenticity cannot be established.

ssh-copy-id terminal output

Type yes and press Enter to continue connecting.

3. The system scans the local system for the id_rsa.pub key file generated in Step 2. Then, it prompts for the password for the user account on the remote system.

ssh-copy-id password terminal output

Provide the password and press Enter.

4. The system shows the number of keys added and the instructions for logging in to the remote system.

ssh-copy-id key added terminal output

Note: By default, Debian 10 and later includes the ssh-copy-id command. If it's not included, it is part of the openssh-server package: sudo apt install openssh-server.

Copy the Public Key Manually

Alternatively, copy the key manually. Follow the steps below:

1. Display the SSH key with the following command:

cat ~/.ssh/id_rsa.pub
Display SSH key generated on Debian using the cat command.

The cat command shows the contents of the SSH public key file. Copy the output.

2. Access the remote server.

3. Open a terminal and create a new hidden directory called .ssh in the home directory using the mkdir command:

sudo mkdir -p ~/.ssh

Note: If the ~/.ssh directory already exists, the command will exit without making changes.

4. Next, add the public key to the authorized_keys file by entering the following:

sudo echo [id_rsa.pub] >> ~/.ssh/authorized_keys
Copying SSH keys generated on Debian to authorized keys file.

Replace [id_rsa.pub] with the actual public key displayed in Step 1.

5. Set the correct permissions for the new directory:

sudo chmod -R go= ~/.ssh

6. If you're using the root Debian user account on the server, the directory owner must be set to the user account that will be logging in remotely to the server:

sudo chown -R [username]:[username] ~/.ssh

The chown command changes the directory's ownership.

Step 4: Log in to Remote Server

With the public key copied to the remote server, SSH access is now available:

1. From the client system, open a terminal window and enter the following:

ssh [username]@[hostname_or_IP]

Replace the placeholders with the actual values.

2. The system may display that the authenticity of the host can't be established. Type yes and press Enter.

3. If you set a passphrase, enter it when prompted.

ssh remote server debian terminal output

You should now have an SSH connection to the remote server.

Step 5 (Optional): Disable Password Authentication

Disabling password authentication is a security precaution. It prevents brute-force attacks from intruders attempting to log in to the server.

Before continuing, double-check the following:

To disable password authentication, edit the SSH config file:

1. Log into the remote server:

ssh [username]@[hostname_or_IP]

The provided username should have sudo privileges.

2. Edit the sshd_config file in a text editor of your choice (we are using nano):

sudo nano /etc/ssh/sshd_config

3. Find and modify the following lines. They should look like the following:

PasswordAuthentication no
ChallengeResponseAuthentication no
sshd_config disable password authentication

Alternatively, add them to the file if they do not exist.

4. Write the changes, then exit the editor.

5. Restart the SSH service to apply the changes:

sudo systemctl restart ssh

The system no longer allows password authentication for logins.

Conclusion

This guide showed how to set up and generate SSH keys on Debian. Now, you can configure and connect to a remote system using SSH key pairs.

Next, learn more about how SSH works or master common SSH commands.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Use SSH Port Forwarding
July 5, 2024

This article demonstrates 3 distinct methods used to port forward SSH connections. It examines the syntax of...
Read more
How to Set Up Passwordless SSH Login
October 3, 2024

Speed up connecting to remote servers by enabling passwordless SSH login via public key authentication. In...
Read more
How to Fix "ssh_exchange_identification: read: Connection reset by peer" Error
January 19, 2024

This article deals with the most common causes of the "ssh_exchange_identification: read: Connection reset by...
Read more
How to Transfer Files with Rsync over SSH
January 31, 2020

Rsync is a Linux tool that allows you to transfer data over SSH to a remote server securely. Use the options...
Read more