Servers in stock
 Checking availability...
50% off 1st month on Instant Servers - code 50OFF +1-646-490-9655
Build your server
Ubuntu 16.04 • SSH key authentication tutorial

How to Set Up SSH Keys on Ubuntu 16.04

Follow this step-by-step guide to configure SSH key-based authentication on your Ubuntu server. Sign in securely without a password and optionally disable password authentication entirely.

4-step guide Ubuntu 16.04 24/7 live support

Step-by-step guide

Follow each step carefully to set up SSH key-based authentication on your Ubuntu 16.04 server.

1

Create the RSA key pair

Run the following command on your local machine to generate a 2048-bit RSA key pair (use -b 4096 for a stronger 4096-bit key):

$ ssh-keygen

When prompted for a file path, press Enter to accept the default (~/.ssh/id_rsa). Optionally set a passphrase for an extra layer of security.

Two files are created: ~/.ssh/id_rsa (private key — keep this secret) and ~/.ssh/id_rsa.pub (public key — this goes on the server).

2

Copy the public key to your Ubuntu server

Method 1 — ssh-copy-id (recommended):

$ ssh-copy-id username@remote_host

Enter your password when prompted. The public key will be appended to ~/.ssh/authorized_keys on the server.

Method 2 — via SSH pipe:

cat ~/.ssh/id_rsa.pub | ssh username@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

Method 3 — manually: Display your public key with cat ~/.ssh/id_rsa.pub, then append its output to ~/.ssh/authorized_keys on the remote server.

3

Authenticate using SSH keys

Test that key-based authentication works by connecting to your server:

$ ssh username@remote_host

If you set a passphrase in step 1, you will be prompted to enter it. If you did not, you will be logged in immediately without any password prompt.

4

Disable password authentication (optional)

Once you have confirmed key-based login works, you can disable password authentication to harden the server against brute-force attacks. Open the SSH daemon config:

sudo nano /etc/ssh/sshd_config

Find PasswordAuthentication, uncomment the line if needed, and set it to no:

PasswordAuthentication no

Save the file (Ctrl+X, then Y, then Enter) and restart SSH:

$ sudo systemctl restart ssh

Open a new terminal and verify you can still connect before closing your current session.

FAQ

Common questions about setting up SSH key authentication on Ubuntu.

What is the difference between the public key and the private key?

The public key (id_rsa.pub) is placed on servers you want to access — it is safe to share. The private key (id_rsa) stays on your local machine and must never be shared. The two keys work together to authenticate you without a password.

Should I set a passphrase on my private key?

Yes, a passphrase is strongly recommended. It adds an extra layer of protection so that if your private key file is ever stolen, it cannot be used without the passphrase. Use ssh-agent to cache the passphrase so you don't have to type it every time.

What happens if I lock myself out by disabling password authentication?

If you disable password authentication before confirming your key works, you may lose access. Always open a new terminal and test your SSH key login before closing the current session. If you are locked out, you will need console access (e.g. via your server provider's web panel) to re-enable password authentication.

Can I use this guide for Ubuntu versions other than 16.04?

Yes. The steps and commands are the same for Ubuntu 18.04, 20.04, 22.04, and later versions. The only difference may be the SSH service name — use sudo systemctl restart sshd if ssh does not work.

Need a server to deploy on?

Our dedicated and VPS server plans come with full root access, SSD storage, and 24/7 expert support to get you up and running fast.