how to set up ssh keys on ubuntu 20.04

admin3 April 2024Last Update :

Mastering SSH Key Setup on Ubuntu 20.04: A Comprehensive Guide

Welcome to the definitive guide on setting up SSH keys on Ubuntu 20.04. Secure Shell (SSH) is an essential tool for secure communication between machines, and mastering its setup is a critical skill for any system administrator or developer working in a Linux environment. This article will walk you through the process of creating and deploying SSH keys, ensuring your connections are both secure and convenient.

Understanding SSH Keys and Their Importance

Before diving into the setup process, it’s crucial to understand what SSH keys are and why they’re important. SSH keys are a pair of cryptographic keys that can be used to authenticate to an SSH server as an alternative to password-based logins. A private key, which is secret, is kept on your computer, and a public key, which is shared, is placed in the ~/.ssh directory on the servers you wish to access. Using SSH keys not only enhances security but also streamlines the login process.

Prerequisites for Setting Up SSH Keys

To get started, ensure you have the following:

  • An instance of Ubuntu 20.04 running either on a local machine or a remote server.
  • A user account with sudo privileges.
  • Access to a terminal window/command line (Ctrl-Alt-T).

Step-by-Step Guide to Generating SSH Keys

Installing OpenSSH

First, make sure you have OpenSSH installed on your Ubuntu system. OpenSSH is a suite of secure networking utilities based on the Secure Shell protocol. To install OpenSSH, open your terminal and run:

sudo apt update
sudo apt install openssh-client

Generating Your SSH Key Pair

With OpenSSH installed, you can now generate a new SSH key pair using the following command:

ssh-keygen -t rsa -b 4096

This command generates a new RSA key pair with a length of 4096 bits, offering robust security. You’ll be prompted to enter a file in which to save the keys and an optional passphrase for additional security.

Deploying Your Public SSH Key

Copying the Public Key to Your Server

After generating your SSH key pair, the next step is to place the public key on the server you want to access. You can do this manually by copying the contents of the public key file (~/.ssh/ and pasting it into the ~/.ssh/authorized_keys file on the server. Alternatively, use the ssh-copy-id utility:

ssh-copy-id username@remote_host

Replace “username” with your actual username and “remote_host” with the server’s IP address or hostname.

Verifying Key-Based Authentication

To verify that key-based authentication is working, attempt to log into the server:

ssh username@remote_host

If you’ve set a passphrase, you’ll be prompted to enter it. Otherwise, if everything is configured correctly, you should gain access without needing to enter your user password.

Enhancing Security with SSH Configurations

Disabling Password Authentication

For increased security, disable password authentication on your server. Edit the SSH configuration file:

sudo nano /etc/ssh/sshd_config

Find the line that reads “#PasswordAuthentication yes” and change it to:

PasswordAuthentication no

Save the file and restart the SSH service:

sudo systemctl restart sshd

Managing Multiple SSH Keys

If you use multiple SSH keys for different servers or services, you can create a config file in your ~/.ssh directory to manage them efficiently. Here’s an example entry:

Host myserver
    User myusername
    IdentityFile ~/.ssh/myserver_rsa

This allows you to simply type `ssh myserver` to connect to the server with the specified configuration.

Best Practices for SSH Key Management

  • Regularly update your SSH keys to mitigate the risk of old keys being compromised.
  • Use strong passphrases for your private keys.
  • Never share your private key; keep it secure on your local machine.
  • Use an SSH agent to securely store your passphrase so you don’t need to enter it every time.

Frequently Asked Questions

What is the difference between RSA and ECDSA keys?

RSA keys are widely supported and recommended for compatibility, while ECDSA keys are newer and provide equivalent security with smaller key sizes.

How often should I rotate my SSH keys?

It’s good practice to rotate your SSH keys at least once a year or according to your organization’s security policy.

Can I use the same SSH key for multiple servers?

Yes, you can use the same public key on multiple servers, but for enhanced security, it’s better to use unique keys for different servers or environments.

What should I do if my private key is compromised?

If you suspect your private key has been compromised, immediately revoke access by removing the corresponding public key from all servers and generate a new key pair.


Setting up SSH keys on Ubuntu 20.04 is a straightforward process that significantly improves the security and efficiency of server management. By following the steps outlined in this guide, you’ll be able to establish a secure, password-less connection to your servers, streamline your workflow, and adopt best practices for SSH key management.


Leave a Comment

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

Comments Rules :

Breaking News