In this tutorial, we will learn how to set up SSH keys on Ubuntu 20.04. Secure Shell (SSH) is a protocol used to securely connect to a remote server or computer. It provides a secure way to transfer files, execute remote commands, and manage remote systems. SSH keys are a more secure way to authenticate with a server or computer than passwords.
Step 1: Check for existing SSH Keys on Ubuntu
Before creating new SSH keys, we need to check whether SSH keys already exist on our system. To do this, open the terminal and type the following command:
ls ~/.ssh/id_rsa*
If you get a message that says “No such file or directory,” it means that you don’t have any SSH keys on Ubuntu system. If you see files with names that start with “id_rsa,” you already have SSH keys on your system.
Step 2: Generate SSH Keys on Ubuntu
To generate SSH keys on Ubuntu, we will use the ssh-keygen command. This command generates a public and a private key pair. The private key is kept on the local computer, and the public key is uploaded to the remote server.
To generate SSH keys on Ubuntu, open the terminal and type the following command:
ssh-keygen
You will be asked to enter a file name for the SSH key. Press Enter to accept the default file name, or enter a new file name. Next, you will be asked to enter a passphrase. The passphrase is used to encrypt the private key, so make sure to use a strong passphrase and remember it. If you leave the passphrase blank, the private key will not be encrypted, and anyone who gains access to it can use it to access the server.
After entering the passphrase, the ssh-keygen command will generate a public and a private key pair and store them in the ~/.ssh directory.
ls ~/.ssh/id_rsa*
/root/.ssh/id_rsa /root/.ssh/id_rsa.pub
Step 3: Copy the public key to the remote server
Once you have generated SSH keys, you need to copy the public key to the remote server. You can do this by using the ssh-copy-id command. The ssh-copy-id command copies the public key to the remote server’s authorized_keys file.
To copy the public key to the remote server, open the terminal and type the following command:
ssh-copy-id user@remote_server
Replace “user” with your username on the remote server and “remote_server” with the IP address or hostname of the remote server. You will be prompted to enter your password for the remote server. After entering your password, the public key will be added to the authorized_keys file on the remote server.
Step 4: Test SSH authentication
To test whether SSH authentication is working, open the terminal and type the following command:
ssh user@remote_server
Replace “user” with your username on the remote server and “remote_server” with the IP address or hostname of the remote server. If SSH authentication is working, you will be logged in to the remote server without entering your password.
Step 5: Disable password authentication (Optional)
If you want to enhance security, you can disable password authentication and only allow SSH key authentication on the remote server. To do this, log in to the remote server and open the sshd_config file:
sudo nano /etc/ssh/sshd_config
Find the line that says “#PasswordAuthentication yes” and remove the “#” at the beginning of the line. Then change “yes” to “no” so that it reads “PasswordAuthentication no”. Save and exit the file.
Next, restart the SSH service:
sudo systemctl restart sshd
From now on, you will only be able to log in to the remote server using SSH keys.
In this tutorial, we learned how to set up SSH keys on Ubuntu 20.04. SSH keys provide a more secure way to authenticate with a server or computer than passwords. By using SSH keys, you can securely transfer files, execute remote commands, and manage remote systems. Once you have set up SSH keys, you can disable password authentication to enhance security.
0 Comments