ServerTips

@server @tips @rsync @remote @host

Using rsync as sudo on remote machine

Sometimes we want to use rsync to transfer files to a remote server, but we need sudo on the remote machine in order to transfer (e.g. if the destination is in / on the server). However with rsync we do not ever get the option to enter a sudo password on the remote. To do this, we need to allow the SSH user to use rsync as sudo on the remote machine:

# first find out the path to rsync on the remote machine
$ which rsync

# you get a response like
/usr/bin/rsync

# then open the sudoers file
$ sudo nano /etc/sudoers

# In the file, at the bottom add this line. replace the rsync path with what you got above. <user> is the remote user.
<user> ALL=NOPASSWD:/usr/bin/rsync

# then, use rsync like:
$ rsync <source> --rsync-path="sudo rsync" <host>:<destination>
# e.g.
$ rsync ~/file.txt --rsync-path="sudo rsync" fa1:/etc/

Using rsync to copy files FROM remote TO local

Assuming you have either the standard port 22 open for SSH, or you have configured your hosts such that you don't need to specify the port (see here) you can do:

$ rsync --stats <host>:<source> <destination>

If you don't have a host set up, you can do:

$ rsync -chavzP -e "ssh -p <portnumber>" <user>@<server>:<source> <destination>

In this case, <source> is the path to the source file, and <destination> is the path to the destination directory. Don't forget the trailing / .