ssh Auto Login without prompt for password

Problem: 
On 4 of my Linux boxes i want to run some scripts by running them on all of boxes. I Login to one machine and run the script to use all 4 machines to perform the task.
We use SSH to run our tasks. Therefore we need an automatic login from host machine to other machines. We don't want to enter any passwords, because you want to call ssh from a within a shell script.

Steps to Perform this:
We have 4 machines named linhtr01, linhtr02, linhtr03, linhtr04. We have same user and password on all 4 machines.
Lets Assume Host machine as linhtr01.

1. Login to Host machine linhtr01.

2. Generate a pair of authentication keys without entering a passphrase
 
user1@linhtr01:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user1/.ssh/id_rsa): 
Created directory '/home/user1/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/user1/.ssh/id_rsa.
Your public key has been saved in /home/user1/.ssh/id_rsa.pub.
The key fingerprint is: 
 
3. Now use ssh to create a directory ~/.ssh as user1 on linhtr02 ad others. (The directory may already exist): 

user1@linhtr01:~> ssh user1@linhtr02 mkdir -p .ssh
user1@linhtr02's password:
 
4.  Finally append new public key to user1@linhtr02:.ssh/authorized_keys
 
user1@linhtr01:~> cat .ssh/id_rsa.pub | ssh user1@linhtr02 'cat >> .ssh/authorized_keys'
user1@linhtr02's password: 

This is last time you need enter password for linhtr02 machine.

5. From now on you can log into linhtr02 as user1 from linhtr01 without password.
user1@linhtr01:~> ssh linhtr02 
user1@linhtr02:>

Sometimes this may not work depending on your version of SSH.
In case it still prompts for password, you might also have to do the following changes:

Change the permissions of .ssh to 700 
Change the permissions of .ssh/authorized_keys to 640
 
 Try again. It will work.
 
You also do the same for different users on both machines.
Like you can log into linhtr02 as user2 from linhtr01 as user1 without password. Just change the user and host. 

Comments

Popular posts from this blog

How to use a value between two different threads in Jmeter

Steps to Analyze AWR Report in Oracle

Correlation and checking in Results