Why you can trust ZDNET
:ZDNET independently tests and researches products to bring you our best recommendations and advice. When you buy through our links, we may earn a commission.Our process
'ZDNET Recommends': What exactly does it mean?
ZDNET's recommendations are based on many hours of testing, research, and comparison shopping. We gather data from the best available sources, including vendor and retailer listings as well as other relevant and independent reviews sites. And we pore over customer reviews to find out what matters to real people who already own and use the products and services we’re assessing.
When you click through from our site to a retailer and buy a product or service, we may earn affiliate commissions. This helps support our work, but does not affect what we cover or how, and it does not affect the price you pay. Neither ZDNET nor the author are compensated for these independent reviews. Indeed, we follow strict guidelines that ensure our editorial content is never influenced by advertisers.
ZDNET's editorial team writes on behalf of you, our reader. Our goal is to deliver the most accurate information and the most knowledgeable advice possible in order to help you make smarter buying decisions on tech gear and a wide array of products and services. Our editors thoroughly review and fact-check every article to ensure that our content meets the highest standards. If we have made an error or published misleading information, we will correct or clarify the article. If you see inaccuracies in our content, please report the mistake via this form.
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.
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.
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.
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.