Skip to content

Instantly share code, notes, and snippets.

@lgloege
Last active November 2, 2023 17:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save lgloege/cb88664328896b80b6992c07bf9fb0ef to your computer and use it in GitHub Desktop.
Save lgloege/cb88664328896b80b6992c07bf9fb0ef to your computer and use it in GitHub Desktop.
Use rsync to copy from a server with bastion server between them. Does not require scp or ssh tunneling.

Setup keyless login

  1. On local make an RSA key if you haven't already (ssh-keygen)
  2. Copy that key into .ssh/authorized_keys on the first (i.e. bastion) server (ssh-copy-id user@first.server) 2b. Test that you can ssh user@first.server and connect without password
  3. ssh to first.server and create an RSA key on first.server if you haven't already (ssh-keygen)
  4. ssh-copy-id to the server you want to access (what I am calling second.server) 4b. test that you can ssh from first.server to second.server without entering a password

To copy from server to local

rsync -e "ssh user@first.server ssh" user@second.server:/path/to/file /path/to/local/dest

To copy from local to server

rsync -e "ssh user@first.server ssh" /path/to/local/src user@second.server:/path/to/server/dest

@maximbashevoy
Copy link

Great snippet, thank you! A couple of tips:

  • I think you need extra flags like "-azP", otherwise everything will be skipped
  • If you get 'user@second.server password:' prompt despite having keys set up, try adding "-A" flag after ssh commands:
rsync -azP -e "ssh -A user@first.server ssh -A" user@second.server:/path/to/file /path/to/local/dest

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment