How to specify SSH port in rsync command
Introduction
When transferring files between two remote servers, the Rsync command is one of the most efficient ways to do so. It not only transfers files quickly but also synchronizes them, making sure that none of the files are missed or corrupted. However, sometimes you may need to use a non-standard SSH port for security reasons. In this blog post, we will discuss how to specify the SSH port in the Rsync command.
rsync
use port 22 by default, if you want to use a different port, you can use -e
option to specify the SSH port.
Specify SSH Port in Rsync Command
By default, Rsync uses SSH to transfer files between two remote servers. The SSH port used by Rsync is 22, which is the standard SSH port. However, sometimes you may need to use a non-standard SSH port, for example, 2222. To specify the SSH port in the Rsync command, you need to use the “-e” option followed by the SSH command with the port number.
The following is an example of how to specify the SSH port in the Rsync command:
rsync -e 'ssh -p 1234' username@hostname:SourceFile DestFile
In the example above, the “-e” option is used to specify the SSH command with the port number. The “ssh -p 2222” is the SSH command with the port number. The rest of the Rsync command is the same as the standard Rsync command.
Automate SSH Port in Rsync Command
If you need to use a non-standard SSH port frequently, you may want to automate the process of specifying the SSH port in the Rsync command. You can do so by creating an SSH configuration file, which specifies the SSH port for a particular host.
To create an SSH configuration file, you need to create a file named “config” in the .ssh directory in your home directory. The “config” file should contain the following information:
Host remote
Hostname remote.server.com
Port 2222
In the example above, “remote” is the name of the remote host, http://remote.server.com/
is the hostname or IP address of the remote server, and “2222” is the SSH port number.
After creating the SSH configuration file, you can use the following Rsync command to transfer files between two remote servers:
rsync -avz /path/to/source user@remote:/path/to/destination
In the example above, the SSH port is automatically specified because of the SSH configuration file.
Leave a message
Disclaimer
- Welcome to visit the knowledge base of SRE and DevOps!
- License under CC BY-NC 4.0
- Made with Material for MkDocs and improve writing by generative AI tools
- Copyright issue feedback me#imzye.com, replace # with @