Ssh – Port forwarding for Rsync

port-forwardingrsyncssh

Every port on my server is blocked except port 222 which is were ssh connects too. This server is pretty much a backup server, and I have my clients rsync to it.

I do this by using ssh's port forwarding (-P 222 -L 873:myserver.com:873), however, I want to do this with just using the rsync command. Is that possible?

This is what I've been trying:

sudo rsync -avv --progress --inplace --rsh='ssh -p 222 -L 873:myserver.com:873' . rsync://bt-backup@localhost/backup-bt-backup

but it doesn't work because I try to log into localhost, instead of myserver.com.

doing:

sudo rsync -avv --progress --inplace --rsh='ssh -p 222 -L 873:myserver.com:873' . rsync://bt-backup@myserver.com/backup-bt-backup

lets me login through ssh, but rsync tries the remote host instead of the localhost where the port has been forwarded too

Best Answer

If the server is running SSHd then you can just use rsync over SSH directly.

For instance I run things like
rsync -a --inplace some/dir/ user@remote.server.tld:/destination/dir/
all the time. There is no need to run rsync as a daemon or have port forwarding enabled as rsync will start a copy of itself using the SSH link and handle all synchronisation between itself and that instance over the SSH link too (and the remote copy closes as the connection is dropped so you don't end up with a bunch of zombie processes hanging around).

To talk over a port other than the standard one you just need to give rsync a little extra info about how it should manage the connection, like so:
rsync -a --inplace -e 'ssh -p 222' some/dir/ user@remote.server.tld:/destination/dir/

This is actually more secure (all the rsync communication is protected by SSHs secure transport, whereas with port forwarding everything is sent plain) and more convenient (no rsyncd needed, and you can use SSHs key based auth)

Related Topic