Skip to content
Follow
Ubuntu Linux

Setup SSH Key Authentication on Ubuntu

Richard
Written by
Richard
Sep 18, 2021 Updated Jul 14, 2026 4 min read
How to Use Sticky Notes in Windows 11
How to Use Sticky Notes in Windows 11

SSH key authentication on Ubuntu lets you log into your server securely without typing a password.

This method uses a pair of cryptographic keys: a public key that lives on your Ubuntu server and a private key you keep safe on your computer.

Setting this up means you replace your password with this key pair, making remote access to Ubuntu versions like 20.04 LTS or 22.04 LTS much safer and quicker.

You generate a unique key pair and then copy your public key to your server to allow password-free logins.

⚡ Quick Answer

Generate an SSH key pair using `ssh-keygen`, then copy your public key to the server with `ssh-copy-id`. Finally, disable password authentication in `/etc/ssh/sshd_config` and restart the SSH service.

Create SSH keys keypair

Create your SSH keys pair on Ubuntu to start using SSH key authentication. This process makes a private key you keep safe and a public key you can share. The `ssh-keygen` command is used to generate these keys, allowing you to set things like key strength.

The command below generates a new 4096 bits SSH key pair with your email address as a comment.

💻Code
ssh-keygen -t rsa -b 4096 -C "your_username@example.com"

After running the commands above, you’ll be prompted to specify a filename for the keys. In most cases, the default location and filename should work.

💻Code
Enter file in which to save the key (/home/yourusername/.ssh/id_rsa):

Next, you’ll be asked to type a secure passphrase. A passphrase adds an extra layer of security, so you must type the passphrase before you use the key to log in to the remote machine.

💻Code
Enter passphrase (empty for no passphrase):

Press ENTER without typing a passphrase.

On your screen, the entire interaction should look similar to the one below.

💻Code
Generating public/private rsa key pair.
Enter file in which to save the key (/home/richard/.ssh/id_rsa): 
Created directory '/home/richard/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/richard/.ssh/id_rsa
Your public key has been saved in /home/richard/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:F217Tplf9iVDvyTRBRfkeXEdQfCugtgC16BrpRqQYpE admin@example.com
The key's randomart image is:
+---[RSA 4096]----+
|             .=OO|
|  .        .  +.*|
| E     .  . o..=.|
|  o   . o  o oo+.|
|.+   o oS.. ..Bo=|
|o .   * o..  ++==|
|   . + o o . ...o|
|    +   .   .    |
|   .             |
+----[SHA256]-----+
⚠️Warning
Once done, two new files should be created in your home directory (id_rsa and id_rsa.pub).

That’s it! You have successfully created a key pair.

Copy the public key to target system

Copy your SSH public key to the target system in Ubuntu after you’ve created it. This step is crucial for SSH key authentication because it tells the server that your computer is allowed to log in without a password. The `ssh-copy-id` command makes this easy.

Run the command below to copy your public key to a remote server.

💻Code
ssh-copy-id username@server_ip_address
⚠️Warning
Replace the username and server_ip_address with your account on the remote server.

Since key-based authentication isn’t yet configured, you’ll be prompted to type in your SSH password.

Once authenticated, the public key ~/.ssh/id_rsa.pub will be appended to the remote user ~/.ssh/authorized_keys file, and the connection will be closed.

💻Code
richard@10.0.2.17's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'richard@10.0.2.17'"
and check to make sure that only the key(s) you wanted were added.

Configure SSH for passwordless login

Configure SSH on Ubuntu to allow passwordless login by disabling password authentication. You’ll do this by logging into your server and changing the SSH configuration file, usually found at `/etc/ssh/sshd_config`. The goal is to turn off the option for using passwords to log in.

Log on to the remote server with your password, then open the SSH configuration file by running the commands below.

🐧Bash / Shell
sudo nano /etc/ssh/sshd_config

In the file, find the lines below and change the value to match these.

💻Code
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no

Save the file and exit.

Restart the SSH server on the remote host.

🐧Bash / Shell
sudo systemctl restart ssh

After that, password login should be disabled.

Using the command `ssh-copy-id user@remote_host` sends your SSH public key to a remote server. This action configures the remote server to recognize your key, allowing passwordless logins for future connections.

💻Code
ssh username@server_ip_address

That should do it!

Conclusion:

Setting up key-based SSH authentication on Ubuntu Linux uses a process that this article explained. Users can report errors or share additions using the comment form.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

How to Set Up SSH Key Authentication in Windows 11
Ubuntu Linux How to Set Up SSH Key Authentication in Windows 11
Tired of Typing Passwords? Let's Make Your Microsoft Account Passwordless!
Windows Tired of Typing Passwords? Let's Make Your Microsoft Account Passwordless!
How to Add a PIN to Your Account in Windows 11
Windows How to Add a PIN to Your Account in Windows 11
Secure SSH Access on Ubuntu with Google Authenticator
Ubuntu Linux Secure SSH Access on Ubuntu with Google Authenticator

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *