Monday, October 17, 2011

SSH without password

Use ssh to move between linux/unix servers without entering password each time.

Perform dsa key exchange once and hop around in btween two unix/linux servers without entering passwd each time.

Actions to perform :
1: run the attached shell script in the local server
ex : user neeraj in server server1

2: Enter data as asked on the command line. Enter target server name and passwd when asked.

3: Message displayed : " echo dsa kay exchange successfully done"

4: Done, check the new setting by typing command from local server: ssh target_server_name.


***********************************************
#!/bin/sh

# check if the key file exists in the home location in local server

file_count=`ls -l $HOME/.ssh/ | grep id | wc -l`

if [ $file_count -eq 0 ]
then
ssh-keygen -t dsa

fi

#secure the contents of the .ssh directory by by removing read write permission from group and others.

chmod go-x $HOME
chmod go-rwx $HOME/.ssh
chmod go-rwx $HOME/.ssh/*


#copy the file to remote server
echo enter target machine hostname
read target
current_user=`whoami`

echo enter password of $target server
scp $HOME/.ssh/id_dsa.pub $whoami@$target:/tmp

echo again enter the passwd for the server $target

#append the content of public key to authorized_keys2 file

ssh $current_user@$target 'cat /tmp/id_dsa.pub >> $HOME/.ssh/authorized_keys2 '

ssh $target chmod go-w $HOME
ssh $target chmod 700 $HOME/.ssh
ssh $target chmod go-rwx $HOME/.ssh/*

ssh $target rm /tmp/id_dsa.pub


echo dsa kay exchange successfully done.


***************************************************************************