Tips & Tricks for Technologists & System Administrators | About & Contact

Using rsync on an Alternate SSH Port or with OpenSSH Keys


Posted 9th October in Linux, Shell, System Administration. 4 Comments

Best practices state that you should run ssh on an non-standard port. Unfortunately some programs use port 22 by default and it isn’t obvious what the switch is to change this port.

One of these programs is the eminently useful rsync. It states how to do this in the man file, but it is hidden and non-obvious. You simply add the option via the -e command to pass ssh options. -e is the shell rsync uses to connect to a remote host.

Using rsync on an alternate ssh port

1
rsync -avz -e "ssh -p $port" username@ip:/path/to/files/ /local/files/

Using rsync with an openssh key

1
rsync -avz -e "ssh -i /path/to/private/key" username@ip:/path/to/files/ /local/files/

Or with both an alternate port and openssh key:

1
rsync -avz -e "ssh -i /path/to/private/key -p $port" username@ip:/path/to/files/ /local/files/

This can be used in a bash script if you set the $port variable or directly on the command line by using the set port instead of $port.

Here are some other useful pages regarding using rsync. If you don’t use rsync for remote file transfer – I highly recommend it. The transfer speed is much faster than scp over ssh.


Originally posted 20121009 and last touched 20121009
Dave Drager+ is the Chief Technology Officer at XDA-Developers, where he keeps the server farm running efficiently for millions of visitors per day. He has written previously for the technology blogs Lifehacker and MakeUseOf.


  • Craig

    >> Best practices state that you should run ssh on an non-standard port

    Really?

  • http://davedrager.com Dave Drager

    My response…

  • Pingback: On Using Alternate Ports for SSH - The Lone Sysadmin

  • garison tone

     
    I found your blog ( ) and interested in paid post. Let me know if you are interested then I will give you more details.

    I can offer you best to make it worth your time.

    Regards