Ubuntu 18.04 Setup SSH Public Key Authentication

To set up SSH public key authentication on an Ubuntu 18.04 system, you can use the following steps:

  1. Open a terminal and run the following command to generate a new SSH key pair:
ssh-keygen

By default, the key will be saved in the ~/.ssh/ directory with the file name id_rsa.

  1. Press Enter to accept the default location and file name for the key, or enter a different file name if desired.
  2. Next, you will be prompted to enter a passphrase. If you want to use password-less authentication, you can leave the passphrase blank.
  3. Once the key is generated, run the following command to copy the public key to the remote server:
ssh-copy-id user@remote-server-ip

Replace “user” with your username on the remote server and “remote-server-ip” with the IP address or hostname of the remote server.

  1. You will be prompted to enter the password for the remote user, and then the public key will be added to the remote user’s authorized_keys file.
  2. Now you can use ssh user@remote-server-ip command to log in to the remote server without being prompted for a password.
  3. To disable password authentication you can edit the ssh config file by running the following command:
sudo nano /etc/ssh/sshd_config
  1. Locate the line that starts with PasswordAuthentication and change it to no and save the changes.
  2. Restart the SSH service:
sudo systemctl restart ssh

By following these steps, you should now be able to log in to the remote server using SSH public key authentication.

It is also recommended to use ssh-agent and ssh-add to add the ssh key to the ssh-agent and ssh-add command to add the key to ssh-agent every time you log in to the system.

Leave a Comment