Connecting from one server to another server often asks the password before making connection. Also it asks the password while copying the data using scp command.
This creates issue while automating the tasks using shell script. So to avoid the problem we need to configure the password less ssh login between the two servers/systems.
So let’s move on to the scenario.
Suppose we have got two systems
- kalwa1 ip 192.168.216.130
- kalwa2 ip 192.168.216.131
& the password less configuration need to perform for the user manmohan from the system kalwa1(192.168.216.130) to the system kalwa2(192.168.216.131). Then simply follow below steps:
1. login into the system kalwa1(192.168.216.130) using the user manmohan.
2. Create the directory “.ssh” under home dir of manmohan as below:
[manmohan@kalwa1 ~]$ pwd
/home/manmohan
[manmohan@kalwa1 ~]$ mkdir .ssh
[manmohan@kalwa1 ~]$ ls -lal
total 28
drwx------. 3 manmohan manmohan 4096 Jun 27 22:37 .
drwxr-xr-x. 5 root root 4096 May 31 20:13 ..
-rw------- 1 manmohan manmohan 10 Jun 27 22:37 .bash_history
-rw-r--r--. 1 manmohan manmohan 18 Jan 27 2011 .bash_logout
-rw-r--r--. 1 manmohan manmohan 176 Jan 27 2011 .bash_profile
-rw-r--r--. 1 manmohan manmohan 124 Jan 27 2011 .bashrc
drwxrwxr-x 2 manmohan manmohan 4096 Jun 27 22:37 .ssh
3. Now generate the public key using the command “ssh-keygen”
[manmohan@kalwa1 ~]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/manmohan/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/manmohan/.ssh/id_rsa. Your public key has been saved in /home/manmohan/.ssh/id_rsa.pub. The key fingerprint is: f5:ec:f9:e6:38:cb:2b:d6:14:3d:06:31:47:f2:f3:df manmohan@kalwa1.home.com The key's randomart image is: +--[ RSA 2048]----+ | +oo | | .= | | . oo | | . o. +o | | S oo ..| | ... o| | oo E| | o.oo. | | . .=*o | +-----------------+
Press enter for asked question if you do not want any change.
4. This will create the file “id_rsa.pub” under the .ssh directory.
[manmohan@kalwa1 .ssh]$ ll total 8 -rw------- 1 manmohan manmohan 1675 Jun 27 22:41 id_rsa -rw-r--r-- 1 manmohan manmohan 406 Jun 27 22:41 id_rsa.pub
5. Now copy this file “id_rsa.pub” from kalwa1(192.168.216.130). to kalwa2(192.168.216.131) using scp command or whichever method you prefer.
scp -pr id_rsa.pub manmohan@192.168.216.131:/home/manmohan/.ssh/
6. Now logon to the system kalwa2(192.168.216.131) with manmohan user.
7. Go to directory .ssh and execute below command.
cat id_rsa.pub >.ssh/authorized_keys
make sure this file “authorized_keys” has permission 700.
8. Now If you try login from kalwa1(192.168.216.130). to kalwa2(192.168.216.131) it will not asks for any password … mission completed!!!!
[manmohan@kalwa1 ~]$ ssh 192.168.216.131 Last login: Tue Aug 4 07:10:49 2015 from 192.168.216.130 [manmohan@kalwa2 ~]$
View Comments (2)
For point 5; You can also use the ssh-copy-id command
source_server# ssh-copy-id destination_server
THanks for your comment!!!