Sunday, September 27, 2009

Automatic ssh

I am sharing the information which i got after searching through several blogs and forums .
In Linux platform-
suppose there are two machines A and B

and you want to connect from machine A to machine B without being prompted for a password.
since you want to connect from A to machine B , machine B needs to verify the identity of machine A.
so what we do is add the identity of machine A to machine B.

so here are the steps.
1 In machine A, you have to create a public/private key pair
for that
first enter the .ssh folder in the home folder
type "ssh-keygen -t dsa" in the terminal

you get a message
"Generating public/private dsa key pair."
and a prompt
"Enter file in which to save the key (/home/username/.ssh/id_dsa): "
just press Enter as blank will do
and another prompt
"Enter passphrase (empty for no passphrase): "
again press Enter as we use blank for this too...

and another prompt
"Enter same passphrase again"
again press Enter as we use blank for this to

now if you check the directory in which you are in, you can see two files id_dsa and id_dsa.pub

Now copy the file id_dsa.pub to the machine B.
you may use "scp id_dsa.pub B:" command where B is 'username@ip'

In machine B.
check if a file named "authorized_keys" is present in .ssh folder in B's home folder
this is a hidden folder in the home directory.

In order to display hidden files and folders you may use the command
"ls -a"

Even if the file "authorized_keys" is not present in the .ssh folder, you need not worry
now in order to add the identity of A to B, follow the steps

Append the contents of file "id_dsa.pub" which you just copied now, to the "authorized_keys" file

if you are in your home folder...
type the command "cat id_dsa.pub >> .ssh/authorized_keys"

And that's all , now if you try to ssh or scp from machine A to machine B it will not ask you a password.

if you want to login in to any other system from A you can append the id_dsa.pub in the authorized_keys file of the remote system

deleting id_dsa.pub does not cause any problem. but make sure that id_dsa file is not deleted.