好久之前在公司的 PC 機上設(shè)置了 alias 登錄服務(wù)器,感覺挺方便的.例如:
alias 184='ssh -lroot xxx.xxx.xxx.184'
輸入 184 就可以登錄到 IP 以184結(jié)尾的服務(wù)器上了.可是后來有些服務(wù)器修改了密碼,不再使用默認(rèn)密碼了,隨著這種情況越來越多,想記住密碼也越來越難.
想不用自己記住密碼,選擇有兩個:一種是使用 expect 做登錄時自動填寫密碼;另一種是使用 ssh 的公鑰,免密碼登錄.看起來 ssh 至少不需要寫代碼,我又懶得要死,所以就選了免密碼登錄.
ssh免密碼登錄的設(shè)置
這個方法真的是非常簡單先在本機生成ssh公鑰和密鑰,輸入 ssh-keygen 然后一路回車,搞定.
# ssh-keygen
接下來將 ~/.ssh/id_rsa.pub 中的內(nèi)容復(fù)制進 ~/.ssh/authorized_keys 里面就可以了.
# 184
vod_dev:~ #
按照這個方法我很快的搞定了大部分的服務(wù)器免密碼登錄,就剩下一臺服務(wù)器嘗試了好幾遍都不行.
在 ssh 客戶端找問題
我首先想到的是看看 ssh 登錄命令的輸出中能不能看出什么問題.
# ssh -lroot -vv 10.18.207.25
debug2: we sent a gssapi-with-mic packet, wait for reply
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug2: we did not send a packet, disable method
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/likewise-open/HISENSE/jiangxun1/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Offering DSA public key: /home/likewise-open/HISENSE/jiangxun1/.ssh/id_dsa
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /home/likewise-open/HISENSE/jiangxun1/.ssh/id_ecdsa
debug1: Trying private key: /home/likewise-open/HISENSE/jiangxun1/.ssh/id_ed25519
debug2: we did not send a packet, disable method
debug1: Next authentication method: password
root@10.18.207.253's password:
最先吸引我注意力就是ssh回去查找id_dsa這個文件作為私鑰,而本機只有id_rsa,我猜測是因為服務(wù)器是因為設(shè)置不同,需要使用 dsa 作為加密算法.那么就想辦法讓我使用的PC機產(chǎn)生 id_dsa 文件.
# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/likewise-open/HISENSE/jiangxun1/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/likewise-open/HISENSE/jiangxun1/.ssh/id_dsa.
Your public key has been saved in /home/likewise-open/HISENSE/jiangxun1/.ssh/id_dsa.pub.
The key fingerprint is:
d3:26:96:59:12:f6:f2:e6:f8:73:bc:e4:b1:4b:5a:31 HISENSE\jiangxun1@jiangxun1c1
The key's randomart image is:
+--[ DSA 1024]----+
| o |
| . o |
| o o |
| O |
| S = E |
| . B o |
| . ..= |
| ..*oo |
| oo=o |
+-----------------+
在ssh的服務(wù)器端查找問題.
產(chǎn)生 RSA 的密鑰后,問題依舊.這個時候我就有些沒有頭緒了,猜想也許需要重啟啟動一下服務(wù)器上的 sshd 才能起作用吧.
[root@jhx /]# systemctl restart sshd
[root@jhx /]#
重啟后依舊不能免密碼登錄,而且 sshd 重啟后在終端里一點輸出都沒有,都不知 ssh 是否已經(jīng)重新啟動,更重要的是服務(wù)器的 sshd 在收到免密碼登錄請求時,是否報錯?
我對 Linux 的日志輸出的設(shè)置不了解,又一時沒有想到什么的關(guān)鍵詞去搜索,就直接本機 grep 搜索了,確實很 low,不過好在很快就找到了 ssh 的日志放在了 /var/log/secure 文件里.ssh 重啟后可以看到下面的幾行輸出,證明 sshd 是正常重啟了.
May 28 12:54:30 jhx sshd[27117]: Received signal 15; terminating.
May 28 12:54:30 jhx sshd[28262]: Server listening on 0.0.0.0 port 22.
May 28 12:54:30 jhx sshd[28262]: Server listening on :: port 22.
并且找到了之前免密登錄時的報錯信息:
May 28 11:41:34 jhx sshd[27313]: Authentication refused: bad ownership or modes for directory /root
May 28 11:41:34 jhx sshd[27313]: Authentication refused: bad ownership or modes for directory /root
報錯信息非常明顯,就是 /root 目錄的權(quán)限和所有權(quán)設(shè)置不正確.接下來我就到google上去搜這條報錯信息了,找到了這篇文章,文章中的主要解決方案就是修改權(quán)限,但是改過之后問題照舊.
SSH Authentication Refused: Bad Ownership or Modes for Directory
現(xiàn)在回過頭來想,真的應(yīng)該好好在本機上分析一下原因,而不是不停的在搜索引擎上不停的找現(xiàn)成的解決方案.
當(dāng)然最終我還是因為對 google 的依賴,一條道走到黑了.在網(wǎng)上瞎晃了半個小時以后,我終于在服務(wù)器上發(fā)現(xiàn)了 /root 目錄的問題所在./root 目錄的所有者居然不是 root.
# ll /
drwxr-xr-x. 15 1054761 1049089 4096 May 28 11:36 root
修改之后,終于可以成功的免密碼登錄了.
chown root:root /root
總結(jié)
以前我就遇到過ssh免密總是無法成功的情況,一直沒有找到解決辦法.這次終于算是解決了.不過復(fù)盤自己分析問題的過程,還是可以發(fā)現(xiàn)自己過于依賴搜索引擎和stackoverflow,總是想找到完全符合自己問題的答案.更嚴(yán)重的問題是我自己還沒有意識到這其實是不正確思考方式.缺乏自己分析和解決問題的意愿,以后遇到問題一定要先主動分析,看看自己能不能憑借自己的力量解決,實在是沒有思路再去搜索引擎上尋求幫助,把搜索引擎當(dāng)做場外的援助,而千萬不能讓搜索引擎替代自己思考.
最后搜索了一下 CentOS7 的日志輸出設(shè)置,這篇文章講的挺清楚的:
SSHD is not logging in /var/log/secure