考慮到在本機上備份資料,一旦該機器硬碟出現故障,資料無法取出。遠程手動備份資料費時費力且 不及時。最好的方法就是通過指令碼實現遠程自動互備。但遠程無論是通過SSH登陸,還是通過scp拷貝檔案 都需要輸入密碼。為了克服這個問題,首先需要實現不要求輸入密碼的SSH登陸,這樣就可以使用rsync,scp ,rexec等命令來做的遠程備份了。
1. 設定無需密碼的ssh登陸,方法如下:
假設A,B兩伺服器,現在需要在A機上用root登陸B機,而不需要輸入密碼,那我們可按照下面的步驟 來做:
1)在A機上產生鑰匙對,執行以下命令:
ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa
Enter passphrase (empty for no passphrase):直接斷行符號
Enter same passphrase again:直接斷行符號
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f6:61:a8:27:35:cf:4c:6d:13:22:70:cf:4c:c8:a0:23 root@host1
這樣,在/root/.ssh/路徑下會產生id_rsa,和id_rsa.pub,其中id_rsa是密鑰,id_rsa.pub是公開金鑰。
2)把在A機產生的id_rsa.pub拷貝到B機上,假設拷貝到B機的臨時目錄下,如:
scp /root/.ssh/id_rsa.pub root@218.242.214.20:/tmp
3)用root帳號登陸B機,進入其主目錄,建立authorized_keys檔案,並設定好許可權。
cd ~/.ssh
cat /tmp/id_rsa.pub >>authorized_keys
chmod 400 authorized_keys
rm -f /tmp/id_rsa.pub
4)測試
在A機上轉到root帳號,嘗試登入B機。看看是不是不要密碼.
說明:
authorized_keys檔案的許可權很重要,如果設定為777,那麼登入的時候,還是需要提供密碼的。
記得將臨時目錄下的id_rsa.pub刪除,養成個好習慣。
本方法在Red Hat9.0上測試通過。