使用Linux中使用非标准端口的rsync ssh

时间:2020-03-05 15:29:20  来源:igfitidea点击:

rsync是用于本地复制文件的工具,以通过任何远程shell或者从远程rsync守护程序到/从另一个主机复制文件。
rsync广泛用于备份数据。

本教程介绍了如何使用rsync连接到ssh来复制数据,如果ssh正在不同的端口上运行。

rsync语法

rsync命令的基本用法是:

rsync [options] src [dest]

默认情况下,如果没有提供目标,则rsync命令将仅列出源中的文件。
例如,

$rsync /usr/
drwxr-xr-x 4096 2011/04/26 04:26:48 .
drwxr-xr-x 53248 2012/09/26 23:05:59 bin
drwxr-xr-x 4096 2011/04/26 04:27:37 games
drwxr-xr-x 20480 2012/09/08 19:17:32 include
drwxr-xr-x 69632 2012/09/26 23:05:50 lib
drwxr-xr-x 4096 2011/04/26 04:26:48 lib64
drwxr-xr-x 4096 2011/11/16 13:02:25 local
drwxr-xr-x 12288 2012/09/26 23:05:51 sbin
drwxr-xr-x 12288 2012/09/22 14:25:28 share
drwxrwsr-x 4096 2012/09/08 19:46:27 src

现在,要在本地复制,我们可以发出以下命令

$rsync -av file1 dir1/
sending incremental file list
sent 45 bytes received 12 bytes 114.00 bytes/sec
total size is 0 speedup is 0.00

这将将名为"file1"的文件复制到目录'dir1'。
此处的-V选项仅用于详细输出。

rsync ssh到特定端口

如果我们想连接到使用rsync的某些特定端口上运行的机器运行ssh,那么我们可以运行以下语法

$rsync --rsh='ssh -p1234' <sourcefile> user@host:/path/to/destination/directory

使用-p选项指定了SSH正在运行的端口,如上所述。
其中应使用SSH服务器运行的端口更改端口1234.
上述语法的一个例子是

$rsync -av --rsh='ssh -p22' file1 [email protected]:/mnt
The authenticity of host '192.168.1.8 (192.168.1.8)' can't be established.
ECDSA key fingerprint is fe:44:85:8c:2c:63:fb:d7:df:dd:27:17:45:04:28:9b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.8' (ECDSA) to the list of known hosts.
[email protected]'s password:
sending incremental file list
file1
sent 84 bytes received 31 bytes 10.95 bytes/sec
total size is 0 speedup is 0.00

这将使用rsync + ssh复制文件,并且ssh未在其默认端口上运行(尽管在上面的示例中,端口默认为:22)。