How to Set Up SSH Keys on Ubuntu 20.04

Generate a New SSH Key Pair

To generate a new SSH key pair, use the ssh-keygen command:

 

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

  • -t rsa: Specifies the type of key (RSA in this case).
  • -b 4096: Sets the key length to 4096 bits for added security.
  • -C "your_email@example.com": Adds a label (your email) to the key for identification.

Press Enter to accept the default file location (~/.ssh/id_rsa), or specify a different path if needed.

You’ll be prompted to set a passphrase. This is optional but adds an extra layer of security.

 

Add the SSH Key to the SSH Agent

The SSH agent makes it easier to manage your keys. Start the agent and add your key:

 

ssh-copy-id username@remote_host

Copy the Public Key to the Remote Server

You need to copy the public key to the server you want to connect to. Use the ssh-copy-id command:

 

ssh-copy-id username@remote_host

Replace username with your remote server's username and remote_host with its IP address or domain name.

This command will:

  • Prompt you for the password of the remote user.
  • Add your public key to the server’s ~/.ssh/authorized_keys file.

If ssh-copy-id is not available, you can manually copy the public key:

  1. Display the key: cat ~/.ssh/id_rsa.pub
  2. Copy the output and paste it into the ~/.ssh/authorized_keys file on the remote server.

Test the SSH Connection

To verify the setup, try connecting to the server:

If configured correctly, you should log in without being prompted for a password.

Optional: Secure SSH Configuration

To further secure your server:

  • Disable password authentication: Edit the SSH configuration file on the server:

sudo nano /etc/ssh/sshd_config

Find and update these lines:

PasswordAuthentication no
PubkeyAuthentication yes

Then restart the SSH service:

sudo systemctl restart ssh


  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Secure SSH

RHEL 7 RHEL 7.1 uses OpenSSH 6.6.1p1, including curve25519 and chacha20-poly1305. HostKey...

SSH public key auth CentOS

How to add your SSH public key to CentOS In this tutorial we are going to add our public key...