1. 程式人生 > 實用技巧 >SSH版本7.4升級到8.3版本操作方法

SSH版本7.4升級到8.3版本操作方法

本文章所使用的的相關環境說明:

作業系統:CentOS Linux release 7.5.1804 (Core) (新環境)

第三方yum源:(可百度自行尋找並配置,如:清華大學源,163源,搜狐、阿里雲yum源等國內第三方yum源都可以)

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

通過升級SSH服務修復安全漏洞

SSH服務用於遠端連線伺服器,但是如果ssh服務存在漏洞,黑客就可以通過ssh服務遠端控制伺服器。

例如:以下版本範圍內都存在漏洞。

更新ssh版本修復漏洞

版本升級的兩種方法:(本文章推薦使用第二種編譯安裝方式)

方法一:使用yum升級ssh的版本

如果你的ssh版本過低可以通過yum直接升級sshd服務。如果可以使用yum升級儘量使用yum進行升級,因為yum升級維護起來很方便。

先看一下當前的ssh版本

此方法需要自行尋找一下第三方openssh的最新版本的yum倉庫地址,並在本地配置後方可使用yum直接升級

[root@servera ~]# yum update openssh -y

方法二:原始碼編譯安裝openssh

如果想安裝到最新版本的openssh則需要使用原始碼安裝。

配置備用連線方式telnet,以防止配置失敗不能連線伺服器。

[root@servera ~]# yum install xinetd telnet-server -y

檢查配置檔案,如果存在/etc/xinetd.d/telnet檔案則需要修改配置使root使用者可以通過telnet登陸,如果不存在該檔案則無需配置。

[root@servera ~]# ll /etc/xinetd.d/telnet
ls: cannot access /etc/xinetd.d/telnet: No such file or directory

擴充套件:/etc/xinetd.d/telnet檔案配置
改:
  disable = no
為:
  disable = yes

配置telnet登入的終端型別,新增pst終端,檔案末尾新增即可。

[root@servera ~]# vim /etc/securett
pts/0
pts/1
pts/2
pts/3

配置開機自啟
systemctl enable xinetd && systemctl start xinetd
systemctl start telnet.socket

telnet連線伺服器,xshell協議選擇telnet。

確定-登入連線,輸入root使用者和密碼登入到伺服器

1、先安裝相關的依賴庫:

yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pam-devel libselinux-devel

2、下載openssh的原始碼包

wget -c https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.3p1.tar.gz

3、先備份一下現有的ssh配置檔案

mkdir /opt/sshbak &&  mv /etc/ssh/*  /opt/sshbak/

4、建立新的安裝目錄

mkdir /usr/local/sshd

5、解壓原始碼包,進行編譯安裝

[root@servera ~]# tar xf openssh-8.3p1.tar.gz -C /usr/local/src/  
# 進入到原始碼目錄下,開始檢查環境、編譯、安裝等
[root@servera ~]# cd /usr/local/src/openssh-8.3p1/
[root@servera openssh-8.3p1]# ./configure --with-md5-passwords --with-pam --with-selinux --with-privsep-path=/usr/local/sshd/ --sysconfdir=/etc/ssh && make -j4 && make install

編譯安裝完成!

6、修改sshd服務端的新配置檔案,使root使用者可以遠端登入。(建議先備份一下/etc/ssh/下生成的相關配置檔案,也可以直接把之前的配置檔案copy到此目錄下使用)

備份新生成的檔案
# cp -a /etc/ssh/*  /bak/sshbak-`date +%F`

#修改服務端配置檔案
[root@servera openssh-8.3p1]# vim /etc/ssh/sshd_config
# ①允許root遠端登入
改:
32 #PermitRootLogin prohibit-password
為:
32 PermitRootLogin yes

② 允許使用公鑰認證
改:(去掉註釋即可)
37 #PubkeyAuthentication yes
為:
37 PubkeyAuthentication yes

# 禁止解析
改:(去掉註釋即可,此處是優化項如果使用DNS解析速度會很慢)
98 #UseDNS no
為:
98 UseDNS no 

7、拷貝開機啟動指令碼

拷貝開機啟動指令碼
[root@servera openssh-8.3p1]# cp -a contrib/redhat/sshd.init /etc/init.d/sshd
# 使用sed或手動修改一下啟動指令碼中的路徑資訊
[root@servera openssh-8.3p1]# sed -i 's#SSHD=/usr/sbin/sshd#SSHD=/usr/local/sbin/sshd#g' /etc/init.d/sshd
[root@servera openssh-8.3p1]# sed -i 's#/usr/bin/ssh-keygen#/usr/local/bin/ssh-keygen#g' /etc/init.d/sshd

8、加入到開機啟動服務列表並設定為開機自啟動

chkconfig --add sshd  && chkconfig sshd on

刪除或移走原有的sshd服務啟動指令碼

[root@servera openssh-8.3p1]# mv /usr/lib/systemd/system/sshd.service /opt/sshbak/

服務管理測試

/etc/init.d/sshd restart
netstat -ntup | grep 22


先檢視一下ssh的版本和服務狀態

使用遠端工具連線到伺服器,此時ssh的版本已升級為8.3版本,升級成功

sshd服務成功登入後可以禁用telnet服務

systemctl disable xinetd.service && systemctl stop xinetd.service
systemctl disable telnet.socket && systemctl stop telnet.socket

如果想要隱藏掉遠端登入時的版本資訊內容,可以使用以下方法重新編譯安裝即可

重新編譯安裝ssh,使其連線時遮蔽SSHD版本資訊,提升安全性

[root@servera openssh-8.3p1]# cd /usr/local/src/openssh-8.3p1

[root@servera openssh-8.3p1]# vim version.h
改:第3行內容
 3 #define SSH_VERSION   "OpenSSH_8.3" 

為:修改為
 3 #define SSH_VERSION   "ctyun_cloud"

再次編譯安裝

[root@servera openssh-8.3p1]# ./configure --with-md5-passwords --with-pam --with-selinux --with-privsep-path=/usr/local/sshd/ --sysconfdir=/etc/ssh  && make -j4 && make install

# 重新編譯不會改變配置檔案,直接重新啟動服務使版本資訊生效
systemctl restart sshd

新建選項卡連線sshd,此時ssh的版本資訊已被修改為我們自定義的版本資訊內容