設置ssh免密碼登錄(使用RSA Key)
機器1:10.37.64.52
機器2:10.37.64.53
設置機器1和機器2之間免密碼登錄
以下全是使用root用戶,若以其他用戶登錄(如postgres用戶),則先切換到該用戶環境下在執行以下步驟。
機器1免密碼登錄機器2
1)機器1上生成RSA Key
cd ~/.ssh/
rm -f ./*
ssh-keygen -t rsa //隨後按3次ENTER鍵
此時在.ssh目錄下生成了id_rsa和id_rsa.pub。其中id_rsa是私鑰,本機要用;id_rsa.pub是公鑰,給其他的機器用。
2) 設置自身ssh互信
cd ~/.ssh/
cp id_rsa.pub authorized_keys
3)將公鑰上傳到機器2上
scp ./id_rsa.pub [email protected]:/root/.ssh/1_id_rsa.pub
4)將公匙寫入機器2中的~/.ssh/authorized_keys文件
cat ~/.ssh/1_id_rsa.pub >> ~/.ssh/authorized_keys
機器2免密碼登錄機器1
與“機器1免密碼登錄機器2”的操作類似。
常見問題
- 設置ssh免密碼後仍需要輸入密碼
在root用戶下查看系統日誌文件(rhel6.3目錄 /etc/log/secure,centOS6.5目錄/var/log/secure),發現如下日誌:
Jun 26 15:28:14 sndsdevapp14 sshd[18439]: Authentication refused: bad ownership or modes for directory /var/lib/pgsql
Jun 26 15:28:16 sndsdevapp14 sshd[18439]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=sndsdevapp12 user=postgres
由日誌可知,是目錄/var/lib/pgsql的權限問題,將該目錄權限設爲700即可。
注:如果發現secure文件中沒有日誌記載,可能是rsyslog服務未啓動,執行 ’ service rsyslog start ’ 啓動即可。