Introduction:
Secure Shell (SSH) is a popular protocol used for securely accessing and managing remote computers, especially in a server environment. One of the most secure methods of authentication is through the use of SSH keys. In this tutorial, we will guide you through the process of setting up SSH key authentication on Ubuntu 20.04.
Step 1: Generate Your SSH Keys (if You Haven’t Already)
Before proceeding with the configuration, you need to generate your SSH keys. Open a terminal window and run the following command:
sudo genkey
This will create a pair of keys in your home directory. The id_rsa
file will be used for authentication, while the id_rsa.pub
file will be used for authorization.
Step 2: Copy the Public Key to the Remote Server
Next, you need to copy the public key to the remote server where you want to authenticate using SSH. You can do this by running the following command in the terminal:
scp id_rsa.pub user@remote-server:/home/user/.ssh/authorized_keys
Replace user
with the username of the remote server and /home/user/.ssh/authorized_keys
with the path where the public key will be saved on the remote server.
Step 3: Configure the Remote Server to Accept the Public Key
On the remote server, you need to configure the SSH daemon to accept the public key. Open the sshd_config
file in a text editor and add the following lines at the end of the file:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile /home/user/.ssh/authorized_keys
Save and close the file.
Step 4: Test SSH Key Authentication
Now, try to connect to the remote server using SSH key authentication. Open a terminal window and run the following command:
ssh user@remote-server
If everything is set up correctly, you should be able to connect to the remote server without entering any passwords.
Tips and Variations:
- You can use multiple keys for authentication by separating them with commas in the
AuthorizedKeysFile
directive. For example:
AuthorizedKeysFile /home/user/.ssh/authorized_keys1, /home/user/.ssh/authorized_keys2
This will allow you to use any of the listed keys for authentication.
- If you want to disable password authentication for all users, you can add the following line at the end of the
sshd_config
file:
Match User *
ChrootDirectory /var/empty
PasswordAuthentication no
This will prevent password authentication for any user on the remote server.
- You can also use SSH keys to authenticate multiple users on a single remote server. To do this, create a separate
authorized_keys
file for each user and specify the path to each file in theAuthorizedKeysFile
directive. For example:
AuthorizedKeysFile /home/user1/.ssh/authorized_keys1, /home/user2/.ssh/authorized_keys2
This will allow each user to use their own SSH key for authentication.
Conclusion:
In this tutorial, we have covered the basics of setting up SSH key authentication on Ubuntu 20.04. By following these steps, you can securely authenticate to a remote server using your own SSH keys. Remember to always keep your private keys safe and secure, and never share them with anyone unless absolutely necessary. Happy SSHing!