Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass password automatically for rsync SSH command?

People also ask

Can we pass password in SSH command?

sshpass command-line tool will do the job for us. It facilitates a simplified approach to non-interactive ssh sign-in and supports one-liner ssh password input. Firstly, you need to install the sshpass tool on your Linux operating system.

How do I put password in SSH?

First, open a terminal and run ssh-copy-id yourservername . You'll be asked to enter your password for the server. After entering your password, you'll be able to SSH into the server without needing a password again.

How do I rsync to another server without a password?

You can use rsync via ssh without a password by using public key authentication (key pair with no password) and the command option of authorized_keys . Note that you have to set PermitRootLogin forced-commands-only in /etc/ssh/sshd_config on the remote host if you want to run rsync on the root privilege.


Use "sshpass" non-interactive ssh password provider utility

On Ubuntu

 sudo apt-get install sshpass

Command to rsync

 /usr/bin/rsync -ratlz --rsh="/usr/bin/sshpass -p password ssh -o StrictHostKeyChecking=no -l username" src_path  dest_path

You should use a keyfile without passphrase for scripted ssh logins. This is obviously a security risk, take care that the keyfile itself is adequately secured.

Instructions for setting up passwordless ssh access


You can avoid the password prompt on rsync command by setting the environment variable RSYNC_PASSWORD to the password you want to use or using the --password-file option.


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

If you can't use a public/private keys, you can use expect:

#!/usr/bin/expect
spawn rsync SRC DEST
expect "password:"
send "PASS\n"
expect eof
if [catch wait] {
    puts "rsync failed"
    exit 1
}
exit 0

You will need to replace SRC and DEST with your normal rsync source and destination parameters, and replace PASS with your password. Just make sure this file is stored securely!


Use a ssh key.

Look at ssh-keygen and ssh-copy-id.

After that you can use an rsync this way :

rsync -a --stats --progress --delete /home/path server:path