How to pass password automatically for rsync SSH command?

Viewed 348611

I need to do rsync by ssh and want to do it automatically without the need of passing password for ssh manually.

15 Answers

I got it to work like this:

sshpass -p "password" rsync -ae "ssh -p remote_port_ssh" /local_dir  remote_user@remote_host:/remote_dir

The official solution (and others) were incomplete when I first visited, so I came back, years later, to post this alternate approach in case any others wound up here intending to use a public/private key-pair:

Execute this from the target backup machine, which pulls from source to target backup

rsync -av --delete -e 'ssh -p 59333 -i /home/user/.ssh/id_rsa' user@10.9.9.3:/home/user/Server/ /home/keith/Server/

Execute this from the source machine, which sends from source to target backup

rsync -av --delete -e 'ssh -p 59333 -i /home/user/.ssh/id_rsa' /home/user/Server/ user@10.9.9.3:/home/user/Server/

And, if you are not using an alternate port for ssh, then consider the more elegant examples below:

Execute this from the target backup machine, which pulls from source to target backup:

sudo rsync -avi --delete user@10.9.9.3:/var/www/ /media/sdb1/backups/www/

Execute this from the source machine, which sends from source to target backup:

sudo rsync -avi --delete /media/sdb1/backups/www/ user@10.9.9.3:/var/www/

If you are still getting prompted for a password, then you need to check your ssh configuration in /etc/ssh/sshd_config and verify that the users in source and target each have the others' respective public ssh key by sending each over with ssh-copy-id user@10.9.9.3.

(Again, this is for using ssh key-pairs without a password, as an alternate approach, and not for passing the password over via a file.)

Though you've already implemented it by now,

you can also use any expect implementation (you'll find alternatives in Perl, Python: pexpect, paramiko, etc..)

Exposing a password in a command is not safe, especially when using a bash script, if you tried to work with keyfiles thats will be nice. create keys in your host with ssh-keygen and copy the public key with ssh-copy-id "user@hostname.example.com and then use rsync addin the option -e "ssh -i $HOME/.ssh/(your private key)" to force rsync using ssh connection via the the private key that you create earlier.

example :

rsync -avh --exclude '$LOGS' -e "ssh -i $HOME/.ssh/id_rsa" --ignore-existing $BACKUP_DIR $DESTINATION_HOST:$DESTINATION_DIR;

Here's a secure solution using a gpg encrypted password.

1.Create a .secret file containing your password in the same folder as your rsync script using the command:

echo 'my-very-secure-password' > .secret

Note that the file is hidden by default for extra security.

2.Encrypt your password file using the following gpg command and follow the prompts:

gpg -c .secret

This will create another file named .secret.gpg. Your password is now encrypted.

3.Delete the plain text password file

rm .secret

4.Finally in your rsync script use gpg and sshpass as follows:

gpg -dq secret.gpg | sshpass rsync -avl --mkpath /home/john user_name@x.x.x.x/home

The example is syncing the entire home folder for the user named john to a remote server with IP x.x.x.x

Related