How to Set Up SSH Keys on Ubuntu 18.04

How to Set Up SSH Keys on Ubuntu 18.04

Setup SSH Key

Secure Shell (SSH) is a cryptographic network protocol used for a secure connection between a client and a server and supports various authentication mechanisms.

The two most popular mechanisms are passwords based authentication and public key-based authentication. Using SSH keys is more secure and convenient than traditional password authentication.

In this tutorial, we will walk through how to generate SSH keys on Ubuntu 18.04 machines. We will also show you how to set up an SSH key-based authentication and connect to your remote Linux servers without entering a password.

Creating SSH keys on Ubuntu

Before generating a new SSH key pair first, check for existing SSH keys on your Ubuntu client machine. You can do that by running the following ls command:

$ ls -l ~/.ssh/id_*.pub

If the command above prints something like No such file or directory or no matches found it means that you don’t have SSH keys on your client machine and you can proceed with the next step, and generate SSH key pair.

If there are existing keys, you can either use those and skip the next step or backup up the old keys and generate new ones.

Generate a new 4096 bits SSH key pair with your email address as a comment by typing:

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

The output will look something like this:


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

Press Enter to accept the default file location and file name.

Next, you’ll be prompted to type a secure passphrase. Whether you want to use passphrase, it’s up to you. If you choose to use passphrase you will get an extra layer of security.


Enter passphrase (empty for no passphrase):

If you don’t want to use passphrase just press Enter.

The whole interaction looks like this:

after generating the SSH KEY

To verify your new SSH key pair is generated, type:

$ ls ~/.ssh/id_*

/home/yourusername/.ssh/id_rsa /home/yourusername/.ssh/

Copy the Public Key to Ubuntu Server

Now that you generated your SSH key pair, the next step is to copy the public key to the server you want to manage.

The easiest and the recommended way to copy your public key to the server is to use a utility called ssh-copy-id. On your local machine terminal type:

$ ssh-copy-id [email protected]_ip_address

You will be prompted to enter the remote_username password:


[email protected]_ip_address's password:

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


Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]_ip_address'" and check to make sure that only the key(s) you wanted were added.

If by some reason the ssh-copy-id utility is not available on your local computer, you can use the following command to copy the public key:

$ cat ~/.ssh/ | ssh [email protected]_ip_address "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Leave a Reply

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