SSH Login without password so that tasks can be automated via shell scripts

Prerequisite

If “openssh-server” & “openssh-client” are not installed in say “hostA”, then install them in “hostA” using “apt-get” with “sudo” for actions with root user privileges.

Setting up passwordless connection

The aim is to connect from “hostA” as “userA” to “hostB” as “userB” without being prompted to enter the password for userB@hostB. This allows you to scp files from hostA to hostB, create new directory on hostB from hostA, etc without being promoted for password to connect to hostB from hostA.

This allows you to automate repetitive tasks via shell scripting.

Q. What is the benefit?
A. You can automate tasks via shell scripts without requiring to enter the password to connect from one host to another.

Step 1: Create private/public key pairs from “hostA” so that you can connect to “hostB” without passwords.

Enter through all the prompts without any passphrases. This should have created two files, i.e. public/private key pairs “id_rsa.pub” and “id_rsa” under “~/.ssh”.

Step 2: Use ssh to create a folder “.ssh” under userB@hostB from userA@hostA.

You will be prompted to enter the password for “userB@hostB”. This creates a folder “~/.ssh” on “userB@hostB”.

Step 3: Append the public key “id_rsa.pub” from hostA to hostB’s “~/.ssh/authorized_keys”.

You will be prompted to enter the password for “userB@hostB” for the last time.

Step 4: Now the public key is copied over to the hostB’s “~/.ssh/authorized_keys”, you can connect from userA@hostA to userB@hostB without requiring to enter the password. You can create a new folder named “test” on “userB@hostB” from “userA@hostA” as shown below.


Java Interview FAQs

800+ Java Interview Q&As

Top