In this tutorial, you will learn how to implement passwordless SSH logins in three simple steps.
What is SSH?
Secure Shell, commonly known as SSH, is a network protocol used for secure connections between a client and a remote server. It is a cryptographic service that allows users to log in and manage machines remotely. By default, it uses a username and password authentication.
How to Enable SSH Password-less Login
Setting up an automated password-less SSH login in Linux is very simple. All you need is to generate a public key and copy it to the remote host.
The steps provided below describe the process of creating and copying the public key to the remote host.
Step 1. Generating a New SSH Key
To generate a new SSH key pair, use the command provided below:
This will interactively generate a public and private key which you can use to authenticate SSH.
The output will be similar to the one shown below:
Generating public/private rsa key pair.
Enter file in which to save the key (/home/centos/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
The key's randomart image is:
| o+B.+o+o .|
| . o.=oB O.o.. o |
| +o.oB = X + o |
| . o=+o* O . + |
| . .+E S + . |
| . . |
[[email protected] ~]$
NOTE: You can create an SSH key pair without a passphrase by pressing ENTER during the creation process.
If you are looking for maximum security, ensure to set a passphrase. For automated logins, simply skip the passphrase.
Step 2. Verify SSH Key
To verify that the SSH key pair has generated successfully, you can list the files in the ~/.ssh directory as shown:
This will list the private and public key as shown:
drwx------. 17 centos centos 4096 May 17 01:14 ..
-rw-------. 1 centos centos 2635 May 17 01:14 id_rsa
-rw-r--r--. 1 centos centos 588 May 17 01:14 id_rsa.pub
Step 3. Copy Public Key to Remote Host
With the SSH key pair at hand, we need to copy it to the remote host. Thus, logging in without a password.
The simplest way to copy your public key to the remote host is to use the ssh-copy-id command as:
This will authenticate the set username and append the public key to the authorized_keys file in the remote host.
Once the key is uploaded, you can log in to the remote host using the command:
This will log in automatically without requiring a password.
NOTE: Ensure that you have your private key pair to log in successfully.
This tutorial showed you how to set up an SSH password-less login using key pairs. This allows you to automate tasks and manage multiple remote hosts.