Wesbytes Knowledge Base

Search our articles or browse by category below

How to Securely Transfer Files via rsync and SSH on Linux

Last modified: July 2, 2022
You are here:
Estimated reading time: 1 min

1. Securely Download From a Server

i. Standard SSH Port:

rsync -avHe ssh user@server:/path/to/file /home/user/path/to/file
  • user: The username of the remote user through which you’ll be logging into the target (remote) server.
  • server: The hostname or IP address of the target (remote) server.
  • /path/to/file: The path to the file that needs to be downloaded from the target (remote) server. Besides, file is the file name.
  • /home/user/path/to/file: The local path where you would like to store the file that is downloaded from the target (remote) server. File is the file name.

Example:

rsync -avHe ssh [email protected]:/home/adam/testfile1 /home/localuser/testfile1

ii. Alternate SSH Port:

rsync -avHPe "ssh -pPORTNUMBERuser@server:/path/to/file /home/user/path/to/file
  • PORTNUMBER: The destination (remote) server’s SSH port number.
  • user: The remote user’s username, which you will use to log into the target server (which is remote).
  • server: the target (remote) server’s hostname or IP address.
  • /path/to/file: the location of the file on the target (remote) server, where file is the name of the file.
  • /home/user/path/to/file: the local path, where file is the file name, and file is the file that you want to download from the target (remote) server.

Example:

rsync -avHPe "ssh -p1337" [email protected]:/home/adam/testfile1 /home/localuser/testfile1

2. Securely Upload To a Server

i. Standard SSH Port:

rsync -avH /home/user/path/to/file -e ssh user@server:/path/to/file
  • /home/user/path/to/file: the local path, where file is the file name, where file will be uploaded to the target (remote) server.
  • user: The remote user’s username, which you will use to log into the target server (which is remote).
  • server: the target (remote) server’s hostname or IP address.
  • /path/to/file: the remote path, where file is the file name, for the file that will be uploaded to the target (remote) server.
rsync -avH /home/localuser/testfile1 -e ssh [email protected]:/home/adam/testfile1

ii. Alternate SSH Port:

rsync -avHPe "ssh -pPORTNUMBER/home/user/path/to/file -e ssh user@server:/path/to/file
  • PORTNUMBER: The port number for SSH on the target (remote) server.
  • /home/user/path/to/file: The local path where the file that will be uploaded to the target (remote) server exists, where file is the file name.
  • user: The username of the remote user through which you’ll be logging into the target (remote) server.
  • server: The hostname or IP address of the target (remote) server.
  • /path/to/file: The remote path for the file that will be uploaded to the target (remote) server, where file is the file name.

Example:

rsync -avHPe "ssh -pPORTNUMBER" /home/localuser/testfile1 -e ssh [email protected]:/home/adam/testfile1
Was this article helpful?
Dislike 0
Views: 5