1. 程式人生 > 其它 >ssh 雙機互信:免密碼登入設定步驟及常見問題

ssh 雙機互信:免密碼登入設定步驟及常見問題

在 linux 系統管理中,設定免密碼登入,進行機器的批量管理是最常用的一個方法。比如針對幾十甚至上百臺線上機器,通常我們會設定一臺“釋出機”作為中央控制機對其它線上機器免密碼登入,然後進行軟體、配置檔案的分發、更新、部署。當然了,針對上面的問題,解決方案並非最優且唯一,比如你也可以用 expect 模擬自動輸入來完成自動登入驗證這一過程,或者用現在一些開源的軟體自動化配置和部署工具,比如 Puppet,但這都不在本文的討論範疇,今天咱們要說的就是這種最原始、最有效、最直接的方式:免密碼登入。

(1)問題:

假設:現有2臺機器

1、個人機192.168.1.110

2、伺服器192.168.1.112

要使110無需密碼通過ssh登入112

(2)步驟

1、建立金鑰

miao@u32-192-168-1-110:~/.ssh$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): (忽略:直接回車即可)
Enter same passphrase again: (忽略:直接回車即可)
Your identification has been saved in /home/miao/.ssh/id_rsa.
Your public key has been saved in /home/miao/.ssh/id_rsa.pub.
/******************

2、複製公密到伺服器

miao@u32-192-168-1-110:~/.ssh$ scp id_rsa.pub [email protected]:/home/miao/.ssh/192.168.1.110
[email protected]'s password: 
id_rsa.pub                                                                   100%  404     0.4KB/s   00:00    
miao@u32-192-168-1-110:~/.ssh$

3、新增公密到192.168.112的信任區域

miao@debian-192-168-1-112:~/.ssh$ cat 192.168.1.110 >> authorized_keys
miao@debian-192-168-1-112:~/.ssh$

注: 2、3兩步可由命令ssh-copy-id一步到位

miao@ubuntu-192-168-1-110:~/.ssh$ ssh-copy-id [email protected]
#如果錯誤就用 
ssh-copy-id -i ~/.ssh/id_rsa.pub IP
Password:
Now try logging into the machine, with "ssh '[email protected]'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.

4、測試

此時整個步驟已經完成,你可以嘗試 ssh ip 或者 /etc/hosts 中的主機名試試。

(3)可能遇到的問題

1、在使用 ssh-copy-id 錯誤提示 

ssh-copy-id:/usr/bin/ssh-copy-id: ERROR: No identities found

表現:

# 公鑰,私鑰已經生成,執行上述命令完畢出現如下錯誤:

$ ssh-copy-id remote-machine

/usr/bin/ssh-copy-id: ERROR: No identities found

解決方法:

g 之發現缺少公鑰路徑,通過 -i 加上即可:

$ ssh-copy-id -i ~/.ssh/id_dsa.pub user@remote_ip

2、ssh ip 可以成功登入,ssh hostname 卻失敗

表現:

ssh hostname

ssh: connect to host localhost port 22: Connection refused

解決方法:

看下對方的主機名是不是在 /etc/hosts 檔案中和 ip 做了對映,沒有就加上即可。

3、ssh 的配置目錄許可權問題

由於 ssh 的許可權直接關係到伺服器的安全問題,因此 ssh 每次讀取配置都會校驗相關資料夾和檔案的許可權,以防止許可權過大對外暴露。

表現:

設定了.ssh目錄,在authorized_keys設定了key後登入還提示需要輸入密碼。

解決方法:

注意許可權,.ssh許可權700,authorized_keys許可權600,就KO啦!

chmod 700 ~/.ssh/

chmod 600 ~/.ssh/authorized_keys

4、ssh localhost:publickey 授權失敗

sudo vi /etc/ssh/sshd_config

RSAAuthentication yes  
PubkeyAuthentication yes  
AuthorizedKeysFile     .ssh/authorized_keys 

service sshd restart

注:ssh可同時支援publickey和password兩種授權方式,publickey預設不開啟,需要配置為yes。 
如果客戶端不存在.ssh/id_rsa,則使用password授權;存在則使用publickey授權;
如果publickey授權失敗,依然會繼續使用password授權。
不要設定 PasswordAuthentication no ,它的意思是禁止密碼登入,這樣就只能本機登入了!

5、ssh localhost:Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

vi /etc/selinux/config  
SELINUX=disabled  

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

最後重啟你的 linux 執行 ssh localhost

6、ssh localhost:需要密碼

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

7、ssh ip 或 hostname 均提示:connection refused

  • 目標主機的ssh server端程式是否安裝、服務是否啟動,是否在偵聽22埠;
  • 是否允許該使用者登入;
  • 本機是否設定了iptables規則,禁止了ssh的連入/連出;

具體請參考: http://hi.baidu.com/leejun_2005/item/bfc0ded296cb8ebf32db907e

8、kerberos 下 ssh localhost 啟動 hadoop 需要密碼

這個時候編輯 .k5login 新增 host/[email protected] 已經無效,需要在本地建立公鑰與祕鑰。

1. ssh-keygen -t rsa
   # Press enter for each line 
2. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
3. chmod 0755 ~ && chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys

(4)最後的問題

本文介紹的建立基於免密碼登入,雙機互信的管理方式簡單、直接,但是安全就是個問題了,稍有不慎容易被一鍋端,建議加上防火牆的埠、ip 策略,有條件的話試試動態口令,這樣會更穩妥些。再有就是當機器規模達到上百、上千臺的時候,如果對檔案資料、機器狀態的實時同步、一致性的要求很高的時候,這種管理方式的弊端就出來了。

(5)Refer:

[1]  ssh-copy-id幫你建立信任

http://blogread.cn/it/article/6103?f=wb

[2]  ssh無密碼登入設定 ssh-keygen ssh-copy-id

http://www.lvtao.net/server/54.html

[3]  一次由SELinux引起的ssh公鑰認證失敗問題

http://www.cnblogs.com/qcly/archive/2013/07/27/3219535.html

[4] How to ssh to localhost without password?

https://stackoverflow.com/questions/7439563/how-to-ssh-to-localhost-without-password