wiki:waue/sshKeyAuth

簡化設定叢集的登入驗證

動機

透過ssh免密碼登入主機來控制,已經是很普遍的一種方式來操控遠端Linux系統,不外乎將對方的public key加入到 authorized_keys,但是遇到網路環境中有大量的電腦需要互相驗證的時候,如在有10個節點的叢集環境中,因為要跑叢集程式,因此程式在每台電腦之間透過ssh運行都要暢行無阻。

以前的作法

  1. 以第一節點為平台,ssh 登入到第二台,ssh-keygen產生密碼對,登出;再登入到第三台...依此類推。
  2. 在第一節點收集其他電腦的public keys
  3. 把十組public keys內容貼到authorized_keys
  4. 把known_hosts檔與authorized_keys檔scp到其他電腦的.ssh資料夾中

詳細作法可參考:兩小時叢集電腦

改進的作法

據小澤源所建議,只要在第一節點產生完密鑰對,然後scp -r .ssh到其他台電腦就可以了。以下列出詳細作法:

$ cd ~
$ ssh-keygen -t rsa -b 1024 -N "" -f ~/.ssh/id_rsa
$ cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
$ scp -r ~/.ssh/ "其他節點IP_or_HostName":~/

然後就可以快樂的使用此帳戶連到各台電腦都不用密碼了。

補充

免密碼的問題解決了,不過登入到沒有列在known_hosts的主機時,會有要求是否加入的詢問出現,因此要確定每台都有登入過。如果有一台沒登入過且是自動化程式的話,就會卡住,等你看到之後,回答yes,才會繼續,但為時已晚...。

因此可以修改ssh的編譯檔,使它預設不要詢問,而是自動加入到Known_hosts

編寫 /etc/ssh/ssh_config :

StrictHostKeyChecking no

ps : 注意原本是ask 改成是no,如果改成yes的話就會被Strict住,比ask更慘

其他設定

  • 關於ssh_config的其他有趣設定參數,列如下
Host Restricts the following declarations (up to the next Host key-word) to be only for those hosts that match one of the patterns given after the keyword.
BatchMode If set to yes, passphrase/password querying will be disabled. This option is useful in scripts and other batch jobs where no user is present to supply the password. The argument must be yes or no. The default is no.
PubkeyAuthentication Specifies whether to try public key authentication. The argument to this keyword must be yes or no. The default is yes. This option applies to protocol version 2 only.
  • 每台ssh重新啟動

改寫一個shell如下,就可以自動把改好的ssh_config檔複製到每台節點並重新啟動ssh

#!/bin/bash

for ((i=2;i<=7;i++));
do
 scp /etc/ssh/ssh_config "192.168.1.$i":/etc/ssh/ssh_config
 ssh "192.168.1.$i" /etc/init.d/ssh restart
done
Last modified 15 years ago Last modified on Feb 23, 2009, 5:58:05 PM