Linux Commands

Automate SSH Login Without Password

If you want to perform tasks on a remote machine using SSH, you will need to enable automatic SSH logins to eliminate the need to enter a password in every instance. This can be incredibly helpful if you are calling ssh from a shell command.

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:

ssh-keygen

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:

[centos@centos8 ~]$ ssh-keygen

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:

SHA256:gkjD1bEfh00O4tP2xD7VpbIBjaBC7cJzSGXjdLXGCss centos@centos8.linuxvmimages.local

The key's randomart image is:

+---[RSA 3072]----+

| o+B.+o+o .|

| . o.=oB O.o.. o |

| +o.oB = X + o |

| . o=+o* O . + |

| . .+E S + . |

| . . |

| |

| |

| |

+----[SHA256]-----+

[centos@centos8 ~]$

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:

$ ls -la ~/.ssh

This will list the private and public key as shown:

drwx------. 2 centos centos 38 May 17 01:14 .

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:

ssh-copy-id username@remote_ip_address

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:

ssh username@remote_host

This will log in automatically without requiring a password.

NOTE: Ensure that you have your private key pair to log in successfully.

Conclusion

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list