SSH is one of the best ways to handle tasks such as automated backups, file synchronization, and remote server access and management. SSH passwordless login is an SSH authentication method that employs a pair of public and private keys for asymmetric encryption. The public key resides on the server, and only a client that presents the private key can connect.
SSH does have its pain points, with or without SSH keys, including:
Fortunately, there’s an alternative to managing all of these things by hand, which we’ll go into later.
This tutorial will walk you step by step through how to manually set up SSH passwordless login to a Linux server. These commands should work on the majority of Linux distributions, and instructions are included for modern client machines of the macOS, Windows, and Linux varieties.
Use ssh-keygen to generate a key pair consisting of a public key and a private key on the client computer. This command can be run on any modern Linux client distribution, the Terminal in macOS, or in the Command Prompt in Windows 10/11.
ssh-keygen -t rsa
The -t rsa option specifies that the type of the key should be RSA. Other choices include DSA, ECDSA, and ED25519. Select the protocol your SSH connection will use.
When prompted, enter a filename for the key.
Enter file in which to save the key. (C:\Users\annem_000\.ssh\id_rsa):Enter Passphrase (empty for no passphrase):
Adding a passphrase is an important step for securing the local key, which otherwise will be usable by anyone who acquires the key itself. Choose a passphrase with the same rigor that you would use to create any secure passphrase. Some clients can be configured to save passphrases for a true “passwordless” access experience, while others may require it to be entered with each use. This will be covered in more detail later in the tutorial.
Type a passphrase (it will not be displayed, even though you are correctly entering it) and hit Enter (or hit Enter to continue with the default of no passphrase). Confirm the passphrase when prompted. The result will look similar to this:
With the initial step to set up SSH passwordless login using ssh keygen completed, you now have two files:
Next, add the public key on the server you want to connect to. With your existing username and password, connect to the server using SSH, using whatever command line or client program you normally use for such connections. Check to see if the .ssh directory already exists by attempting to list the files within it:
ls .ssh
If it does not, you will not be able to move into that directory and should instead create it:
mkdir -p .ssh
(Note the required dot at the beginning of the directory name, which makes this a hidden directory.)
On a macOS or Linux client, use ssh-copy-id to propagate the public key to the server, like this:
ssh-copy-id user@somedomain
Make sure to replace user with a valid username from the server and somedomain with the valid IP or domain of the server.
With a Windows client, you can accomplish this task via the Windows Command Prompt. You will need to refer to the results of your earlier attempt to list the contents of the .ssh directory and see if it contained a file called authorized_keys or not.
A) If you had to create the .ssh directory yourself, or if the remote server doesn’t already have an authorized_keys file, on the client computer command line, enter the following to copy the public key to the .ssh directory on the server (if you changed the name of your key from id_rsa.pub, change it here):
scp .ssh/id_rsa.pub user@somedomain:~/.ssh/authorized_keys
B) If the remote server has an existing authorized_keys file, the new key must be appended rather than overwriting the existing file. This is very important so that existing users do not lose access unintentionally. First, you will copy the file to the remote server. Then on the remote server, use the cat command to append it to the existing file:
On the client: scp .ssh/id_rsa.pub user@somedomain:~/.ssh
On the remote server: cat .ssh/id_rsa.pub >> .ssh/authorized_keys
On the remote server: rm .ssh/id_rsa.pub (clean up after yourself and remove the now-unnecessary key file)
In your SSH session with the remote machine, update the permissions of the .ssh directory and authorized_keys file in case they need it:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
Now, close your connection and your Terminal or Command Prompt. When you reopen it and try to connect to the remote server again from the client where you have your private key saved, you should receive a request to enter the passphrase instead of your username and password. Test it out:
ssh user@somedomain
👍 Success! Now, to avoid entering the SSH key passphrase every time:
A public key can be re-derived from a private key, but not vice-versa, making it especially important to back up private keys. To do so, simply back up the directory where they reside, which in our above examples, was the .ssh directory in your user’s home directory. Both keys in the pair will be backed up because you generated them there, unless you removed one of them from the directory.
Most servers allow both username/password authentication and SSH key authentication, but if you want to allow only SSH key authentication, then you can disable the use of usernames and passwords. Be certain that you have thought through the ramifications before doing so, because once you take this action, successful certificate authentication will be the only way to access your server.
This is accomplished through the sshd_config file. The exact location of this file varies by Linux distribution. Often it’s in the /etc/ssh directory. Edit this file to include the following parameters:
PasswordAuthentication no ChallengeResponseAuthentication no UsePAM no SSH passwordless login facilitates remote system login for off-site developers as well as on-site staff and scripted automation, but it comes with some potential complications:
Excellent access control and monitoring strategies are a crucial part of any infrastructure plan in today’s highly distributed environment. Yet manually distributing, revoking, rotating, and auditing SSH keys is a lot of work. Incorporating a good control plane is the answer:
Delinea improves workflow and simplifies administration. If you’re ready to step away from the hassles of traditional SSH key management to more modern ways of authenticating, securing, and tracking access, try a demo. You’ll only need five minutes to connect to your first database or server.
Yes, it is possible to use ssh-keygen to create an SSH key without a password. When generating a key pair with ssh-keygen, you are prompted to enter a passphrase. If you simply press Enter without typing a passphrase, the key will be created without one. While this allows for passwordless authentication, it is important to note that such a key is less secure because it can be used by anyone who obtains the private key.