[TOC]
reference1: https://blog.csdn.net/qq_33684555/article/details/81872110
reference2: http://www.cnblogs.com/xiaochina/p/7486073.html
reference3: https://blog.csdn.net/bytxl/article/details/46639073
reference4: https://www.cnblogs.com/xshrim/p/6472679.html -> 含有zlib編譯
reference5: https://www.linuxidc.com/Linux/2011-10/45739p2.htm -> 定義了sshd_config文件中的配置
OpenSSH(OpenBSD Secure Shell)是OpenBSD計劃組所維護的一套用于安全訪問遠程計算機的連接工具。該工具是SSH協(xié)議的開源實現(xiàn),支持對所有的傳輸進行加密,可有效阻止竊聽、連接劫持以及其他網(wǎng)絡(luò)級的攻擊。
OpenSSH中的scp client實用程序存在安全漏洞,該漏洞源于程序錯誤的驗證了對象名稱。攻擊者可利用該漏洞覆蓋文件。
最近公司發(fā)現(xiàn)OPENSSH有防護漏洞, 需要給7.9版本的openssh安裝補丁, 故網(wǎng)上收集資料, 融合自己的理解, 寫出此文, 有任何問題請指正.
注意: 編譯安裝openssh新版本之前, 最好先安裝telnet服務(wù), 萬一sshd服務(wù)被你操作掛了, 還有替代的telnet服務(wù)可以提供遠程連接, 筆者就是在安裝過程中遇到過此問題, 最后拖了兩天時間, 各種找領(lǐng)導(dǎo), 然后登錄堡壘機直接操作Centos虛擬機, 才解決問題!!!
yum -y install telnet telnet-server
systemctl start telnet.socket
# 注: 使用telnet命令可直接遠程連接服務(wù)器, 但是telnet不允許root用戶登錄!
1. 基礎(chǔ)環(huán)境準備
首先去openssh官網(wǎng)下載openssh.tar.gz最新版本
wget https://openbsd.hk/pub/OpenBSD/OpenSSH/portable/openssh-8.0p1.tar.gz
注意要下載p1版本, 此版為編譯安裝包!!
在安裝之前先記下sshd.pid路徑, 因為在啟動文件sshd中要更改此路徑
將安裝包先傳入服務(wù)器中
卸載現(xiàn)有版本openssh: rpm -e --nodeps $(rpm -qa | grep openssh)
刪除/etc/ssh/下所有文件, 在卸載完openssh后此路徑下文件不會刪除, 需手動刪除
安裝依賴:zlib-devel、openssl-devel、gcc、gcc-c++、make等, 可直接yum groupinstall "Development Tools", 出現(xiàn)"No packages in any requested group available to install or update"報錯時:
yum -y install openssl-devel
yum -y groupinstall "Development Tools" \
--setopt=group_package_types=mandatory,default,optional
# 報錯原因詳見Redhat官網(wǎng):
# https://access.redhat.com/solutions/1310043
, 唯一需要注意的是openssl-devel這個包必須安裝

2. 編譯安裝openssh
# 先設(shè)置selinux為permissive
vi /etc/sysconfig/selinux
./configure --prefix=/usr/ \
--sysconfdir=/etc/ssh/ \
--with-ssl-dir=/usr/local/ssl \
--with-md5-passwords \
--mandir=/usr/share/man/
make && make install
# 刪除/etc/ssh/下的密鑰對, (只刪除密鑰對即可, 在重啟的時候會重新生成), 此步省略!!!
rm -f /etc/ssh/ssh_host_*
# 復(fù)制啟動文件至/etc/init.d/
cp -a contrib/redhat/sshd.init /etc/init.d/sshd
# 編輯/etc/init.d/sshd文件, 將PID_FILE改為之前記下的sshd.pid路徑
vi /etc/init.d/sshd
# 設(shè)置開機啟動sshd
chkconfig sshd on
chkconfig --list sshd
# 編輯/etc/ssh/sshd_config, 設(shè)置一些常用參數(shù)
vi /etc/ssh/sshd_config
PermitRootLogin yes
PubkeyAuthentication yes
PasswordAuthentication yes
AllowTcpForwarding yes
X11Forwarding yes
PidFile /run/sshd.pid
# 編輯/usr/lib/systemd/system/sshd.service, 添加以下內(nèi)容
vi /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
#After=network.target sshd-keygen.service
#Wants=sshd-keygen.service
After=network.target
[Service]
#Type=notify
#EnvironmentFile=/etc/sysconfig/sshd
#ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecStart=/usr/sbin/sshd
#ExecReload=/bin/kill -HUP $MAINPID
#KillMode=process
#Restart=on-failure
#RestartSec=42s
[Install]
WantedBy=multi-user.target
# 編輯好這個配置文件之后進行以下操作
systemctl enable sshd
systemctl status sshd
systemctl restart sshd
systemctl status sshd
--prefix 安裝目錄
--sysconfdir 配置文件目錄
--with-ssl-dir 指定 OpenSSL 的安裝目錄
--with-privsep-path 非特權(quán)用戶的chroot目錄
--with-privsep-user=sshd 指定非特權(quán)用戶為sshd
--with-zlib 指定zlib庫的安裝目錄
--with-md5-passwords 支持讀取經(jīng)過MD5加密的口令
--with-ssl-engine 啟用OpenSSL的ENGINE支持

至此openssh服務(wù)升級完成, 將sshd服務(wù)添加到開機啟動即可
此版本openssh的配置文件默認是沒有開啟root登錄權(quán)限的, 想要開啟權(quán)限可以修改配置文件/etc/ssh/sshd_config, 配置PermitRootLogin yes, 取消注釋 PasswordAuthentication yes, 配置PID_FILE=/run/sshd.pid

另外, 安裝目錄為/usr/, 因為在啟動文件中有關(guān)于SSHD的路徑, 此安裝目錄默認為redhat啟動文件的路徑

如果變更了安裝路徑, 啟動文件的此路徑也要變更
3. 編譯完成后可能遇到的問題
xshell等連接報錯如下: The SSH server rejected your password. Try again.

或者用journalctl -xn查看后臺日志時發(fā)現(xiàn): error: Could not get shadow information for root

則是由于服務(wù)器上的selinux的問題, 需要將selinux改為permissive
在開啟SSHD服務(wù)時報錯.
sshd re-exec requires execution with an absolute path
用絕對路徑啟動,也報錯如下:
Could not load host key: /etc/ssh/ssh_host_key
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_dsa_key
Disabling protocol version 1. Could not load host key
Disabling protocol version 2. Could not load host key
sshd: no hostkeys available — exiting
解決過程:
#ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
#ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
#/usr/sbin/sshd
如果上述兩個文件存在,仍然出現(xiàn)這個錯誤,那么試試 chmod 600 上述兩個文件。之后應(yīng)該可以解決。
4. 將sshd服務(wù)添加到開機啟動
vi /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
#After=network.target sshd-keygen.service
#Wants=sshd-keygen.service
After=network.target
[Service]
#Type=notify
#EnvironmentFile=/etc/sysconfig/sshd
#ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecStart=/usr/sbin/sshd
#ExecReload=/bin/kill -HUP $MAINPID
#KillMode=process
#Restart=on-failure
#RestartSec=42s
[Install]
WantedBy=multi-user.target
vi sshd@.service # 可省略
[Unit]
Description=OpenSSH per-connection server daemon
Documentation=man:sshd(8) man:sshd_config(5)
Wants=sshd-keygen.service
After=sshd-keygen.service
[Service]
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=-/usr/sbin/sshd -i $OPTIONS
StandardInput=socket
vi sshd.socket # 可省略
[Unit]
Description=OpenSSH Server Socket
Documentation=man:sshd(8) man:sshd_config(5)
Conflicts=sshd.service
[Socket]
ListenStream=22
Accept=yes
[Install]
WantedBy=sockets.target
vi sshd-keygen.service # 可省略
[Unit]
Description=OpenSSH Server Key Generation
ConditionFileNotEmpty=|!/etc/ssh/ssh_host_rsa_key
ConditionFileNotEmpty=|!/etc/ssh/ssh_host_ecdsa_key
ConditionFileNotEmpty=|!/etc/ssh/ssh_host_ed25519_key
PartOf=sshd.service sshd.socket
[Service]
ExecStart=/usr/sbin/sshd-keygen
Type=oneshot
RemainAfterExit=yes
vi /usr/lib/systemd/system/sshd.service # 編輯好這個配置文件之后進行以下操作
systemctl enable sshd
systemctl status sshd
systemctl restart sshd
systemctl status sshd