Thursday, April 1, 2010

SSH authorization between hosts with no password

This script will allow to login to remote servers with no password after first time execution.

ssh_script.sh
----------------------------------------------------------
#!/bin/sh
source ~/.bash_profile

# Uncomment and execute this line only one for generation of keys on local server running the script
#ssh-keygen -t rsa

user=myuser
servers="server1-ip server2-ip"

for server in $servers
do
  #Make sure .ssh directory has permissions 700 (Higher privileges cause authorization issues)
  ssh
$user@$server 'mkdir ~/.ssh; chmod -R 700 ~/.ssh'
  echo "Copying to server $server"

  # the line copies the authorized keys to the remote server
  #scp ~/.ssh/id_rsa.pub $user@$server:~/.ssh/authorized_keys

  #-------- Append local server id_rsa.pub content into remote server authorized_keys file
  cat ~/.ssh/id_rsa.pub | ssh $user@$server "cat >> ~/.ssh/authorized_keys"
done
exit 0
----------------------------------------------------------

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home

Newer›  ‹Older