How to Setup Rsync without password with SSH on UNIX / Linux?

19

13

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

user2010736

Posted 2013-02-22T12:38:51.943

Reputation:

That should be very helpful: https://blogs.oracle.com/jkini/entry/how_to_scp_scp_and

– Tom – 2013-02-22T12:40:30.503

Maverick143 has provided an answer, but this question doesn't really belong on StackOverflow as it has little to do with programming. SuperUser.com would have been more relevant. – Martin – 2013-02-22T12:50:41.683

Answers

29

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/ user@192.168.200.10:/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 user@192.168.200.10

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 user@192.168.200.10

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

rsync -avz -e ssh /home/test/ user@192.168.200.10:/backup/test/

MangeshBiradar

Posted 2013-02-22T12:38:51.943

Reputation: 571

If you need to use a different user, you can do so when you perform the ssh-copy-id:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@192.168.200.10 – Finni McFinger – 2017-09-08T18:10:44.253

This well help better. http://troy.jdmz.net/rsync/index.html

– MangeshBiradar – 2013-02-22T12:44:48.503

5

Clear copy and paste from http://www.thegeekstuff.com/2011/07/rsync-over-ssh-without-password/ you should reference the original author.

– Lawrence Cherone – 2014-05-09T20:12:52.277

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.

Johny

Posted 2013-02-22T12:38:51.943

Reputation: 71

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.

SDsolar

Posted 2013-02-22T12:38:51.943

Reputation: 1 206

-1

mark the user in address would be better

ssh-copy-id -i ~/.ssh/id_rsa.pub <user_in_server>@192.168.1.100

DreamAndDead

Posted 2013-02-22T12:38:51.943

Reputation: 1

2While this may answer the question, it would be a better answer if you could provide some explanation why it does so. – DavidPostill – 2017-07-27T07:16:58.127

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 – 2017-08-10T05:51:08.393