公司某台服務器不知為何無法ssh連接上,進入現場查看:
1.執行netstat -atnlp|grep ssh,沒有找到ssh端口
2.執行ps aux|grep ssh,沒找到相關進程
3.執行service sshd start,顯示綠色的OK,但用ps和netstat看不到ssh任何信息,echo $?結果為0
4.執行service sshd status,顯示:openssh-daemon is stopped
4.執行service sshd stop,無報錯信息,echo $?結果為0
5.執行service sshd restart,出現下圖錯誤提示:
解決過程:
一.查看日志:
查看/var/log/messages,和/var/log/secure文件內容
(目的:查看SSH的所有相關日志信息,以便於分析,但發現這兩個文件被刪掉了)
用touch命令重新建立messages和secure文件,再重啟sshd服務,但這兩個文件內容依舊為空。(先不管它)
二.用yum重新安裝(沒成功):
1.用 rpm -qa | grep openssh 查看有哪些ssh包
#rpm -qa | grep openssh
openssh-server-5.3p1-94.el6.x86_64
openssh-clients-5.3p1-94.el6.x86_64
openssh-5.3p1-94.el6.x86_64
openssh-askpass-5.3p1-94.el6.x86_64
2.用 yum remove openssh-server 把軟件包刪掉,其他的openssh包保險起見沒刪,怕全刪了會因依賴關系包被刪,進而影響其他業務的運行。
3.用yum install openssh-server 重新安裝該包,操作完成后,用service sshd start 發現和當初一樣,start是OK狀態,但服務起不來。
三.改用編譯方式重裝openssh(成功):
1.首先用yum remove openssh-server刪掉原先的包,在openssh官網下載高版本:
(我用的是openssh-7.3p1.tar.gz,在官網沒找到,所以在csdn下載了該包)
2.編譯安裝:
tar -xvzf openssh-7.3p1.tar.gz -C /opt && cd /opt/openssh-7.3p1
./configure && make && make install
【編譯參數可參考:http://www.2cto.com/os/201703/615376.html】
【 ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-md5-passwords --with-pam --with-tcp-wrappers --with-ssl-dir=/usr/local/ssl --without-hardening】
3.拷貝ssh服務文件:
cp ./contrib/redhat/sshd.init /etc/init.d/sshd
chmod +x /etc/init.d/sshd
4.修改SSHD服務文件:
vim /etc/init.d/sshd
修改以下內容
SSHD=/usr/sbin/sshd 為 SSHD=/usr/local/sbin/sshd 【25行】
/usr/sbin/ssh-keygen -A 為 /usr/local/bin/ssh-keygen -A 【41行】
保存退出
5.加入系統服務:
chkconfig --add sshd
查看系統啟動服務是否增加改項
chkconfig --list |grep sshd
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
6.允許root用戶遠程登錄:
cp sshd_config /etc/ssh/sshd_config
vim /etc/ssh/sshd_config 修改 PermitRootLogin yes,並去掉注釋
7.配置允許root用戶遠程登錄:
這一操作很重要!很重要!很重要!重要的事情說三遍,因為openssh安裝好默認是不執行sshd_config文件的,所以即使在sshd_config中配置允許root用戶遠程登錄,但是不加上這句命令,還是不會生效!
vim /etc/init.d/sshd
在 ‘$SSHD $OPTIONS && success || failure’這一行【51行】上面加上一行 :
OPTIONS="-f /etc/ssh/sshd_config"
保存退出
執行:service sshd start,出現綠色的OK,
執行:service sshd status,此時出現了以下錯誤信息:
error: sshd dead but subsys locked
解決方法:
rm -rf /dev/null
mknod /dev/null c 1 3
再次啟動后,出現了sshd is running,端口和進程都出現了:
service sshd start
service sshd status
sshd is running......
端口和進程都出現了:
ps aux|grep ssh
netstat -atnlp|grep ssh
搞定。
總結:故障原因未知。用yum方式安裝的ssh服務啟動不成功,可能是某些與SSH相關聯的進程需要通過重啟系統才生效(生產環境不允許隨便重啟系統)。而編譯方式安裝的ssh,與之前的進程沒有關聯,所以能啟動成功。
至於為何ssh服務會出現故障,暫無法查出原因。編譯openssh的時候,可以根據需要添加參數(--with-pam,--with-tcp-wrappers等)。
參考資料:http://www.cnblogs.com/wuling129/p/5072965.html