rsync有兩種常用的認證方式,一種為rsync-daemon方式,另外一種則是ssh。
在一些場合,使用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).