How to configure key-based authentication for SSH (2024)

Posted: May 3, 2022 | | by Evans Amoany (Sudoer)

Remote connections to a server via Secure Shell (SSH) can be authenticated in two ways. The traditional and default method is to use password authentication. The second approach is key-based authentication, which is based on a private-public key pair.

Key-based authentication provides two primary benefits:

  • Helps mitigate brute-force password attacks against SSH
  • Prevents administrators from being required to manually type passwords in automated processes such as scripts or Ansible

Passwordless key-based authentication is often the assumed configuration on modern Linux systems.

The key pair

For key-based authentication, a matched pair of cryptographic key files is generated. The pair consists of a private key and a public key that uniquely identify the user. The private key usually has a permission of 600 and is kept on the local server. The public key is copied to the remote system where the user intends to log in. This public key is stored in the ~/.ssh/authorized_keys or ~/.ssh/authorized_keys2 file of the authenticating user. For example, if you log into a remote server with the user sadmin, the public key is added to the /home/sadmin/.ssh/authorized_keys file.

Create the key pair

The ssh-keygen command generates the private and public key pair. By default, the command saves these keys to the user's ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub files. The id_rsa is the private key, and id_rsa.pub is the public key. The OpenSSH suite also contains an ssh-copy-id command, which you can use to distribute the public keys to other systems.

To generate the keys, enter the following command:

[server]$ sudo ssh-keygen

Note: Press Enter for all questions because this is an interactive command.

By default, all files are stored in the /home/sysadmin/.ssh/ directory. You can enter a new file name when running the ssh-keygen command. The command also offers the option to add a passphrase to unlock the key file.

[ Get this free download: Advanced Linux commands cheat sheet. ]

Share the public key

To share the public key with other systems the sadmin user will access, use:

[server]$ sudo ssh-copy-id remoteuser@remoteserver

Enter the user's password. The public key is shared with the remote server, and the user can log in without a password. There is also the ssh-agent command, which you can use to store private keys used for public key authentication. This is normally useful when a passphrase protects keys.

To add a private key stored in /home/sadmin/sshkeys/id_rsa to ssh-agent, use these commands:

[server]$ sudo eval $(ssh-agent)[server]$ sudo ssh-add

Skip to the bottom of list

Enter the passphrase, which is then stored in memory until the agent is stopped. In this case, you don't need to enter a passphrase when using SSH to access a remote server.

Wrap up

Passwordless connection is very useful, especially in automated transfers and scripting. It's a fairly standard configuration on many Linux systems because it reduces the burden on manual SSH authentication and allows for more simplified automation solutions.

How to configure key-based authentication for SSH (2024)

FAQs

How to enable key-based SSH authentication? ›

To configure the SSH server to support key-based authentication, follow these steps:
  1. Log in to the server console as the bitnami user.
  2. Create a key pair, consisting of a public and private key, as shown below. ...
  3. Copy the private key file named id_rsa to a secure location.
Feb 6, 2023

How to setup a key pair in SSH? ›

Generate an SSH Key Pair
  1. Run the ssh-keygen command. You can use the -t option to specify the type of key to create. ...
  2. The command prompts you to enter the path to the file in which you want to save the key. ...
  3. The command prompts you to enter a passphrase. ...
  4. When prompted, enter the passphrase again to confirm it.

How to set up rsa keys for SSH? ›

Procedure
  1. Use the ssh-keygen tool to create a key pair. ...
  2. Validate that the keys were generated. ...
  3. Enable key-based authentication in the /etc/ssh directory on the SSH server. ...
  4. Copy the rsa. ...
  5. If you have an existing authorized_keys file, edit it to remove any no-pty restrictions.

How do I specify a key for SSH? ›

To specify which private key should be used for connections to a particular remote host, use a text editor to create a ~/. ssh/config that includes the Host and IdentityFile keywords. Once you save the file, SSH will use the specified private key for future connections to that host.

How does SSH key-based authentication work? ›

For key-based authentication, a matched pair of cryptographic key files is generated. The pair consists of a private key and a public key that uniquely identify the user. The private key usually has a permission of 600 and is kept on the local server.

How to generate and configure SSH certificate based authentication? ›

Generating and Configuring SSH Certificate-Based Authentication (Step-by-Step Guide)
  1. Prerequisites.
  2. Step 1: Generate SSH Key Pair.
  3. Step 2: Copy the Public Key to the Server.
  4. Step 3: Verify SSH Key Authentication.
  5. Introduction to SSH Certificates.
  6. Generating a Certificate from the Public Key.
Mar 8, 2024

Why isn't my SSH key working? ›

Check that key-based authentication is allowed by the server. Make sure the private key is readable by the SSH client. If you're using PuTTY, make sure your SSH keys are properly configured for the session. If you're using an OpenSSH client, be sure your private SSH key has the proper permissions.

How do I check if I have a SSH key? ›

Checking for existing SSH keys
  1. Open Terminal .
  2. Enter ls -al ~/. ssh to see if existing SSH keys are present. ...
  3. Check the directory listing to see if you already have a public SSH key. ...
  4. Either generate a new SSH key or upload an existing key.

Where is SSH authorized_keys? ›

Location of the Authorized Keys File

With OpenSSH, the authorized keys are by default configured in . ssh/authorized_keys in the user's home directory. Many OpenSSH versions also look for ssh/authorized_keys2 . Some organizations use custom OpenSSH builds with different default paths.

What permission should a SSH key have? ›

ssh directory permissions should be 700 (drwx------). The public key (. pub file) should be 644 (-rw-r--r--). The private key (id_rsa) on the client host, and the authorized_keys file on the server, should be 600 (-rw-------).

How do I force SSH to use RSA? ›

Or, if you insist on having the RSA key approach, you can type ssh-keygen -t rsa on the server that you intend to SSH to. That should generate RSA public and private keys under '~/. ssh/id_rsa'.

How to activate SSH key in Linux? ›

Set up personal SSH keys on Linux
  1. Install OpenSSH on your device.
  2. Start the SSH Agent.
  3. Create an SSH key pair.
  4. Add your key to the SSH agent.
  5. Provide Bitbucket Cloud with your public key.
  6. Check that your SSH authentication works.

How do I enable SSH key in Windows? ›

Open the Windows 10 Start menu and search for “Apps & Features”. In the “Apps & Features” heading, click “Optional Features”. Scroll down the list to see if “OpenSSH Client” is listed. If not, click the plus sign next to “Add a feature”, select OpenSSH Client, and click “Install”.

How do I troubleshoot SSH key authentication? ›

Here are some steps you can take to troubleshoot this issue:
  1. Make sure the authorized_keys file and the private key itself have the correct permissions and ownership.
  2. Check that key-based authentication is allowed by the server.
  3. Make sure the private key is readable by the SSH client.
Mar 13, 2024

Top Articles
Latest Posts
Article information

Author: Delena Feil

Last Updated:

Views: 5832

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Delena Feil

Birthday: 1998-08-29

Address: 747 Lubowitz Run, Sidmouth, HI 90646-5543

Phone: +99513241752844

Job: Design Supervisor

Hobby: Digital arts, Lacemaking, Air sports, Running, Scouting, Shooting, Puzzles

Introduction: My name is Delena Feil, I am a clean, splendid, calm, fancy, jolly, bright, faithful person who loves writing and wants to share my knowledge and understanding with you.