Secure Shell (SSH) is the de facto standard for gaining access to remote Linux machines. SSH took the place of telnet long ago, to add a much-needed layer of security for remote logins.
That doesn’t mean, however, that the default SSH configuration is the best option for those who are a bit more concerned about the security of their systems. Out of the box, SSH works with traditional user and password logins. And even though those logins are far more secure than they were with telnet, you’re still typing and sending a password across the internet.
Should anyone intercept that password, they could access your machines (so long as they also knew your username).
There’s a much better way. Said way is SSH Key Authentication. With Key Authentication, you bypass the username and password authentication, and replace it with a key pair. Why is this important? The primary reason this adds extra security is that the only way to access those servers (when they are configured properly for SSH key authentication) is by having the matching key pair.
Here’s how it works:
- You generate an SSH key.
- You upload the public key to a remote server.
- You configure SSH to only allow key authentication.
- You log in from a desktop that contains the private key that matches the public key on the server.
Once configured properly, the only way you’ll be allowed remote access to the server is if you have the matching private key. Without that key, you cannot gain access. So long as you keep that private key private, all is well.
But how do you pull this off? Let me show you.
To set up SSH key authentication, you’ll need at least two Linux machines, one you log in to and one you log in from. I’ll demonstrate with Pop!_OS as my desktop and Ubuntu Server as my remote server. This should, however, work the same on nearly any Linux distribution. You’ll also need a user with sudo privileges. You’ll also want to make sure you have the same username on both local and remote machines.
That’s it. Let’s make some SSH magic.
How to set up SSH key authentication in Linux for more secure logins
1. Open the terminal window
On your desktop operating system, open a terminal window.
2. Generate your SSH key pair
At the terminal window, generate your SSH key pair with the command:
You’ll first be asked where you want to save the key. I suggest saving it to the default location, so just hit Enter when prompted. You’ll then be asked to type and verify a password for the key pair. Make sure this password is strong and unique. Do not go with an empty password, as that isn’t secure.
3. Copy your new public key to the remote server
Here’s where it gets slightly tricky. You need to send the public key to the remote server. For that, you’ll need to know the IP address of the server. You can get the IP address of the server by logging into it and running the command ip a. You should see the IP address listed. With that information in hand, go back to the desktop and send the public key to the server with the command:
Where SERVER is the IP address of the remote server.
You’ll be prompted for the password for your user on the remote server. Once you’ve successfully authenticated, the public key will be copied and SSH key authentication is ready. When you attempt to log into the remote server, you will now be prompted for your SSH key password and not your user password.
How to configure the remote server for SSH key authentication
Now that you have your key copied, log into the remote machine. What we’re going to do now is configure the SSH server to only allow connections via SSH. One thing to keep in mind before you do this is once it’s configured, only those with SSH key authentication set up on the machine will be allowed access. Because of this, you’ll want to make sure you’ve copied SSH keys from all the desktop machines you’ll use to log into the remote server.
With that out of the way, open the SSH daemon configuration file on the remote server with the command:
sudo nano /etc/ssh/sshd_config
In that file, look for the line:
Change that line to:
Save and close the file. Restart SSH with:
sudo systemctl restart sshd
Now, the only way you can successfully remote into that machine is by way of SSH key authentication. Any machine that doesn’t have a matching key pair will be denied access.
Congratulations, you’ve just added another layer of security to your Linux servers.