How to Set up SSH keys?
How to Set Up SSH Keys for Secure Server Access: A Complete Guide
Secure Shell, most commonly known as SSH, is a common network protocol that enables users to securely access a computer/server over an unsecured network. SSH keys gives added security to SSH in the sense that a password need not be used when making use of SSH keys.
SSH key pair comprises of both a public key and private key, both of which are a long string of characters. The public key is saved in the server to which SSH connection is to be done and the private key is stored on the system/computer from which SSH connection is requested.
Setting up SSH keys is one of the most effective ways to secure your server from unauthorized access. SSH (Secure Shell) is a network protocol used to connect securely to a remote server over an unsecured network. By using SSH keys, you can eliminate the need for password-based authentication, which enhances security and simplifies access for users.
In this guide, we will walk you through the process of generating, configuring, and testing SSH keys to securely access your server without the need for passwords.
The below steps can be followed to set up SSH Key based authentication.
1. Create SSH Key pair
On the computer from which SSH connection is requested, make use of the below command to generate a key pair
ssh-keygen -t rsa
A sample output of this command will be as below:
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:q5CKez4qSUCNiNbuNfy8B+3+XtCc9b/WYMp9WX6k7kw user@DESKTOP-8UVUO7D
The key’s randomart image is:
+—[RSA 2048]—-+
|o + |
|o+ o |
|o . . . |
|. . + o o .|
|. . . + S . + .|
| . . . + o . o +|
|.. o = . =E==|
|o.o. . o o +ooo*|
|==o. . o.oo o=o.|
+—-[SHA256]—–+
You may give a passphrase (password) at the time of key pair generation when prompted to. The passphrase adds extra security for the SSH key pair.
Once the command has been executed, the public key will be stored accordingly as id_rsa.pub and the private key as id_rsa in the path that has been specified at the time of key pair generation.
2. Copy the public key to the remote server
The public id_rsa.pub key then needs to be copied to the server to which the SSH connection is to be made. For this, there are two available options.
- Use the ssh-copy-id command as below, where user is the username with which you are connecting to the server with IP address 123.123.123.123
ssh-copy-id [email protected]
- You can alternatively copy the public key to the authorized_keys file located inside the .ssh folder in your server 123.123.123.123.
3. Test the SSH connection.
Once the public key has been added to the server, you can test the SSH connection to the server from the computer which has the private key, using the below command.
There will not be any prompt for the password. However, you may be prompted to enter the passphrase, if it had been set at the time of SSH key pair generation.
4. Disable SSH direct root login
Once you have confirmed that the server is accessible using SSH key pair, you can proceed to disable direct root login to your server through SSH. For this, the below steps can be followed.
- Open the SSH configuration file /etc/ssh/sshd_config
- Change the value of PermitRootLogin to no
- Restart SSH service using the below command
service sshd restart
By setting up SSH keys, you ensure that your server is accessed securely and efficiently without the need for passwords. This not only improves security but also streamlines the login process for trusted users. Follow the steps outlined in this guide to create SSH keys, configure your server for key-based authentication, and enhance your server’s security by disabling root login.
With SSH key authentication in place, you can confidently manage your server and safeguard it from unauthorized access.