SSH (Secure Shell Protocol) allows you to securely transfer data and files between two computers on a network or over internet. It requires password-based or key-based authentication to allow communication between two computers. In this article, we will learn how to setup SSH keys in Linux.
How to Setup SSH Keys in Linux
Here are the steps to setup SSH keys in Linux.
1. Create SSH keys in Linux
The first step is to generate SSH keys in Linux. For this purpose, open terminal on your local computer and run the following command.
$ ssh-keygen
If you see any prompts, enter y to proceed. You will be asked to enter passphrase, which is optional. If you don’t want to use passphrase, just hit enter to proceed, and once again to confirm the passphrase.
Run the following command to check if the .pub file has been created as a output of SSH key generation.
$ ls -l ~/.ssh/id_*.pub
2. Adding SSH Key to Remote Server
Next, you need to add the key file generated above to your remote server. You can do this using ssh-copy-id command on your local system. This command is pre-installed in almost every Linux system. But for this purpose, you must have password-based access to your remote server. If that is the case, run the following command to securely copy the key file to remote server. In the following command, replace user with your remote username and ip-address with the IP address of remote server.
$ ssh-copy-id user@ip-address
You will be prompted to enter remote user’s password. Enter it to copy the key files to remote server.
3. Login to Server with SSH keys
Run the following command to login to your remote server with SSH keys. Replace user with your remote username and ip-address with the IP address of remote server below.
$ ssh user@ip-address
You will be prompted for remote user’s password. Enter it to login.
4. Disable password-based authentication
Now that you are able to authenticate via SSH keys, it is advisable to disable password-based authentication in SSH. For this purpose, login to SSH server with the above command.
$ ssh user@ip-address
Once you have entered the password and logged in, run the following command to open SSH config file.
$ sudo vi /etc/ssh/sshd_config
Once you have opened SSH configuration file, change the following line
PasswordAuthentication yes
to
PasswordAuthentication yes
If ‘PasswordAuthentication no’ is commented, then remove # at its beginning to uncomment it.
Also change the following line
ChallengeResponseAuthentication yes
to
ChallengeResponseAuthentication no
Restart SSH service with the following command.
$ sudo systemctl restart sshd
In this article, we have learnt how to create SSH keys to login to remote server via SSH protocol.
Also read:
How to Create Nested Directory in Python
How to Find Package for File in Ubuntu
How to Prompt for User Input in Shell Script
How to Uninstall SQL Server in Ubuntu
How to Install or Upgrade Software from Unsupported Release
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.