To set up SSH public key authentication on an Ubuntu 18.04 system, you can use the following steps:
- 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
.
- Press
Enter
to accept the default location and file name for the key, or enter a different file name if desired. - Next, you will be prompted to enter a passphrase. If you want to use password-less authentication, you can leave the passphrase blank.
- 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.
- 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.
- Now you can use
ssh user@remote-server-ip
command to log in to the remote server without being prompted for a password. - To disable password authentication you can edit the ssh config file by running the following command:
sudo nano /etc/ssh/sshd_config
- Locate the line that starts with
PasswordAuthentication
and change it tono
and save the changes. - 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.