在一些场合,使用rsync-daemon方式会比较缺乏灵活性,ssh方式则成为首选。但是今天实际操作的时候发现当远端服务器的ssh默认端口被修改后,rsync时找不到一个合适的方法来输入对方ssh服务端口号。
在查看官方文档后,找到一种方法,即使用-e参数。
-e参数的作用是可以使用户自由选择欲使用的shell程序来连接远端服务器,当然也可以设置成使用默认的ssh来连接,但是这样我们就可以加入ssh的参数了。
具体语句写法如下:
rsync -e 'ssh -p 1234' username@hostname:SourceFile DestFile
其他参数完全按照rsync的规定格式加入即可。
上面语句中比较新鲜的地方就是使用了单引号,目的是为了使引号内的参数为引号内的命令所用。没有引号的话系统就会识别-p是给rsync的一个参数了。我的描述可能比较烂,详情可以参考rsync官方描述:
Command-line arguments are permitted in COMMAND provided that COMMAND is presented to rsync as a single argument. You must use spaces (not tabs or other whitespace) to separate the command and args from each other, and you can use single- and/or double-quotes to preserve spaces in an argument (but not backslashes). Note that doubling a single-quote inside a single-quoted string gives you a single-quote; likewise for double-quotes (though you need to pay attention to which quotes your shell is parsing and which quotes rsync is parsing).
下面是rsync配置过程中常遇到的一些文件,大家可以可以参考一下
一般使用默认端口的话, 在服务端的启动命令为:
/usr/bin/rsync --address=192.168.1.23 --daemon
如果在客户端需要换另外的端口侦听, 则使用
/usr/bin/rsync --address=172.18.16.89 --port=8081 --daemon
客户端命令:
/usr/bin/rsync -vzrc --progress --bwlimit=300 /usr/local/tomcat/webapps/.gif 172.18.16.89::appLogo/
rsync: failed to connect to 172.18.16.89: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(107) [sender=2.6.8]
此时在客户端也必须指定端口, 不然会报错。
改为
/usr/bin/rsync -vzrc --port=8081 --progress --bwlimit=300 /usr/local/tomcat/webapps/.gif 172.18.16.89::appLogo/
就OK
三、FAQ
Q:如何通过ssh进行rsync,而且无须输入密码?
A:可以通过以下几个步骤
1. 通过ssh-keygen在server A上建立SSH keys,不要指定密码,你会在~/.ssh下看到identity和identity.pub文件
2. 在server B上的home目录建立子目录.ssh
3. 将A的identity.pub拷贝到server B上
4. 将identity.pub加到~[user b]/.ssh/authorized_keys
5. 于是server A上的A用户,可通过下面命令以用户B ssh到server B上了
e.g. ssh -l userB serverB
这样就使server A上的用户A就可以ssh以用户B的身份无需密码登陆到server B上了。









