1. 程式人生 > >無需密碼通過ssh執行rsync來同步檔案的方法

無需密碼通過ssh執行rsync來同步檔案的方法

1.通過ssh執行rsync(需要密碼)
通過ssh帳戶(需要密碼)執行rsync,將檔案同步映象到遠端伺服器。
下面這個例子將本地的/home/ramesh同步到遠端目錄/backup/ramesh(伺服器地址192.168.200.10)。
執行以下操作時,伺服器將會提示需要使用者使用密碼登入。
rsync -avz /home/ramesh/ [email protected]:/backup/ramesh/

2.使用ssh-keygen生成密匙
現在我們來設定ssh,以便在執行ssh操作時不需要密碼,使用ssh-keygen在本地生成公鑰和私鑰。
ssh-keygen
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

提示:當提示輸入密碼時,只需輸入兩次回車鍵,不指定密碼字元。

ssh-keygen -t [rsa|dsa],rsa為預設值,將會生成金鑰檔案和私鑰檔案 id_rsa,id_rsa.pub或id_dsa,id_dsa.pub

3.使用ssh-copy-id將公匙拷貝至遠端主機
執行ssh-copy-id,將通過ssh-keygen生成的公匙拷貝至遠端主機。

ssh-copy-id -i ~/.ssh/id_rsa.pub 192.168.200.10

重新來執行rsync,現在就不會提示輸入密碼了

把同步程式碼新增到svn勾子裡面,可實現SVN提交自動同步程式碼到其他伺服器