36

How can I set up a rsync between two hosts without providing any password?

1

5 Answers 5

48

Below is the article from The Geek Stuff:

1. Test rsync over ssh (with password):

Do a rsync to make sure it asks for the password for your account on the remote server, and successfully copies the files to the remote server.

The following example will synchronize the local folder /home/test to the remote folder /backup/test (on 192.168.200.10 server).

This should ask you for the password of your account on the remote server.

rsync -avz -e ssh /home/test/ [email protected]:/backup/test/

2. ssh-keygen generates keys.

Now setup ssh so that it doesn’t ask for password when you perform ssh. Use ssh-keygen on local server to generate public and private keys.

$ ssh-keygen

Enter passphrase (empty for no passphrase):

Enter same passphrase again: Note: When it asks you to enter the passphrase just press enter key, and do not give any password here.

3. ssh-copy-id copies public key to remote host

Use ssh-copy-id, to copy the public key to the remote host.

ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]

Note: The above will ask the password for the user account on the remote host, and copy the public key automatically to the appropriate location. If ssh-copy-id doesn’t work for you, use the method we discussed earlier to setup ssh password less login.

4. Perform rsync over ssh without password

Now, you should be able to ssh to remote host without entering the password.

ssh [email protected]

Perform the rsync again, it should not ask you to enter any password this time.

rsync -avz -e ssh /home/test/ [email protected]:/backup/test/
3
1

Genarate the public key in ServerA

$ ssh-keygen
$ Enter passphrase (empty for no passphrase):
$ Enter same passphrase again:

The public key will be generated and stored in

~/.ssh/id_rsa.pub

Copy public key to remote host

ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.1.100

Or

  • Open id_rsa.pub, copy the content
  • Login to ServerB using the same user in the rsync command
  • In ServerB, append the contents to ~/.ssh/authorized_keys. Create the file if not exist. Make sure the file mode is 700.
0

All these rsync suggestions are failing using the latest version in August 2017 on Ubuntu 16.04 LTS. Not a single one of them work.

They also all share the deficiency of requiring a rsync daemon running on the file server.

This answer works with a generic Linux NAS

HERE ARE STEPS:
1) USE rsync as shown below. (to a directory under /mnt or /media you created or on a device you mount. it doesn't matter which) 2) TRANSFER files WITH scp as shown below. FileZilla will work, too.

All of this (except FileZilla) can work in cron without a password.


This setup works very well. The only time you need the password is when you set up the initial ssh-copy-id to set up the RSA passwordless logins. Then you program it into FileZilla once. After that, day by day, no password prompts happen. This is EASY. And the best part is that you can use all the benefits of the rsync program.

This answer explains how to use rsync itself without a password.

Also, there is no need to install yet another daemon (rsync) on either system.

If you haven't already, do this:

ssh-keygen
ssh-copy-id -i ~/.ssh/id_rse.pub NASserver

and test it out with this:

ssh NASserver

and maybe something like this:

scp myfile myusername@NASserver:Documents

I have a second hard drive so I use rsync to copy the boot drive to a subdirectory on sdb1 (mounted under /mnt and excluded from the rsync).

If you do not have a physical hard drive, and have enough space available, you just create a subdirectory under /mnt (or /media) and use that.

As long as the directory is excluded it doesn't matter if it is on a separate drive or not.

Here is the backup script:

cls
echo "EMPTYING TRASH"
rm ~/.local.share/Trash/*
echo "====================================================================="
echo "         BEGINNING rsync from root to /mnt/full/sysbkp"
echo "====================================================================="
time sudo rsync -aAXv / --delete --ignore-errors --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /mnt/full/mysystem

AFTER the rsync use a script:

I always make a new directory on the target NASserver:/mnt/fullsys/mysystem so only the relevant files are transferred.

ssh -e"mv /mnt/fullsys/mysystem mysystem.bak" myusername@NASserver
ssh -e"mkdir /mnt/fullsys/mysystem" myusername@NASserver
scp -r /mnt/full/mysystem myusername@NASserver:/mnt/fullsys/mysystem

Voila! It takes a while but then it is done.

Both scripts can work fine in cron.


The alternative is to use FileZilla to send it the NAS server manually.

Since there can be deletions I always make a new directory and enter it via FileZilla on the target 1TB HDD so only the relevant files are transferred.

Only when the transfer is finished do I remove the older version.

Voila. Success.

0

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.

First, make a key pair without a password and copy the public key to the remote host:

[user@local user]$ sudo ssh-keygen -t dsa -N "" -f /root/.ssh/rsync
[user@local user]$ sudo scp /root/.ssh/rsync.pub [email protected]:~/

Then add the public key to authorized_keys on the remote host:

[user@remote user]$ sudo sh -c 'cat ~user/rsync.pub >> /root/.ssh/authorized_keys'

Then find out the command executed on the remote host when you connect with rsync by using -vv option. (In this example, rsync --server -vvulogDtprz . /home/backup/ is the command):

[user@local backup]$ sudo rsync -vv -az -e "ssh -i /root/.ssh/rsync" /home/backup/ [email protected]:/home/backup/
Password:
opening connection using ssh -i /root/.ssh/rsync -l root remote.example.com rsync --server -vvulogDtprz . /home/backup/ 
protocol version mismatch - is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(69)
[user@local backup]$ 

Then set the command on the command option of authorized_keys:

[user@remote user]$ sudo vi /root/.ssh/authorized_keys
Password:
command="rsync --server -ulogDtprz . /home/backup/" ssh-dss AAA...
    :
[user@remote user]$

Finally, you have to add a line to crontab:

0 6 * * wed     /usr/local/bin/rsync -az -e "ssh -i /root/.ssh/rsync" /home/backup/ [email protected]:/home/backup/

For more information, see http://www2s.biglobe.ne.jp/~nuts/labo/inti/cron-rsync-ssh-nodaemon.html (in Japanese).

-1

mark the user in address would be better

ssh-copy-id -i ~/.ssh/id_rsa.pub <user_in_server>@192.168.1.100
2
  • 2
    While this may answer the question, it would be a better answer if you could provide some explanation why it does so.
    – DavidPostill
    Commented Jul 27, 2017 at 7:16
  • Also the syntax. I believe this is used as a command substitution something like this: rsync -aAVx $(ssh-copy-id -i /home/myusername/.ssh/id_rsa.pub) then all the excludes and --delete etc. I haven't made it work yet but this is the closest I have seen. Supposedly you have to do this even if you can already ssh and scp to the target server, and rsync daemon needs to be running because it does not use normal sftp format. I am still searching.
    – SDsolar
    Commented Aug 10, 2017 at 5:51

You must log in to answer this question.