How to pass password automatically for rsync SSH command?

后端 未结 13 1606
情歌与酒
情歌与酒 2020-12-02 05:39

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

相关标签:
13条回答
  • 2020-12-02 06:09

    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
    
    0 讨论(0)
  • 2020-12-02 06:10

    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
    
    0 讨论(0)
  • 2020-12-02 06:11

    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.

    0 讨论(0)
  • 2020-12-02 06:11

    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!

    0 讨论(0)
  • 2020-12-02 06:12

    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..)

    0 讨论(0)
  • 2020-12-02 06:13

    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.)

    0 讨论(0)
提交回复
热议问题