本文适用于 CentOS 7.x 全系列生产环境,全程手动编译无第三方脚本,所有步骤经过生产验证,包含完整的风险控制和回滚机制。

一、升级前准备

1.1 环境准备与备份

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查看当前系统版本
cat /etc/redhat-release

# 临时关闭防火墙(升级完成后恢复)
systemctl stop firewalld

# 临时关闭SELinux
setenforce 0

# 查看当前OpenSSH和OpenSSL版本
ssh -V
openssl version

# 备份并替换为阿里云yum源
mkdir /etc/yum.repos.d/yum.bak
mv /etc/yum.repos.d/* yum.bak/
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

# 备份所有关键文件(升级失败可直接回滚)
cp /usr/bin/ssh /usr/bin/ssh.bak
cp /usr/sbin/sshd /usr/sbin/sshd.bak
cp -r /etc/ssh /etc/ssh.bak
cp /etc/pam.d/sshd /etc/pam.d/sshd.bak

1.2 安装备用连接(防止 SSH 中断)

绝对不能跳过这一步! 这是防止升级过程中 SSH 连接断开导致服务器失联的唯一保障

1
2
3
4
5
6
7
8
9
10
11
12
13
# 安装telnet作为临时备用登录方式
yum install -y telnet-server telnet xinetd

# 启动并启用telnet服务
systemctl start xinetd
systemctl enable xinetd
systemctl start telnet.socket
systemctl enable telnet.socket
systemctl status telnet.socket

# 临时允许root通过telnet登录(升级完成后立即关闭)
echo "pts/0" >> /etc/securetty
echo "pts/1" >> /etc/securetty

1.3 安装编译依赖

1
yum install -y gcc gcc-c++ glibc make autoconf openssl-devel pcre-devel pam-devel zlib-devel tcp_wrappers-devel libedit-devel perl-IPC-Cmd

二、升级 OpenSSL 到 1.1.1w

注意:OpenSSH 9.9p2 要求 OpenSSL 版本 ≥ 1.1.1,而 CentOS 7.9 自带的 OpenSSL 1.0.2k 不满足要求。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 进入源码目录
cd /usr/local/src

# 提前上传以下两个文件到该目录
# openssl-1.1.1w.tar.gz
# openssh-9.9p2.tar.gz

# 解压并进入OpenSSL目录
tar xf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w

# 配置编译选项
./config --prefix=/usr/local/openssl-1.1.1w shared zlib

# 编译安装(使用所有CPU核心加速)
make -j $(nproc)
make install

# 更新动态链接库配置
echo '/usr/local/openssl-1.1.1w/lib' > /etc/ld.so.conf.d/openssl.conf
ldconfig

# 验证新版本
/usr/local/openssl-1.1.1w/bin/openssl version

验证输出必须为:

1
OpenSSL 1.1.1w  11 Sep 2023

确认无误后再进行下一步操作。

三、编译安装 OpenSSH 9.9p2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cd /usr/local/src/

# 解压并进入OpenSSH目录
tar xf openssh-9.9p2.tar.gz
cd openssh-9.9p2

# 配置编译选项(关键参数不能错)
./configure \
--prefix=/usr \
--sysconfdir=/etc/ssh \
--with-md5-passwords \
--with-privsep-path=/var/lib/sshd \
--with-ssl-dir=/usr/local/openssl-1.1.1w \
--with-pam \
--with-zlib \
--with-ldflags="-Wl,-rpath=/usr/local/openssl-1.1.1w/lib"

# 编译安装
make -j $(nproc)
make install

四、配置与权限修复

4.1 复制新的 PAM 配置文件

1
cp contrib/redhat/sshd.pam /etc/pam.d/sshd

4.2 检查并修复 sshd_config 兼容性

1
2
# 检查配置文件语法
sshd -t

如果出现以下错误,需要注释掉对应的行:

1
2
3
Unsupported option GSSAPIAuthentication
Unsupported option GSSAPICleanupCredentials
Unsupported option UsePAM(如果使用了--with-pam则不会出现)
1
2
3
4
5
6
7
8
9
10
11
# 备份配置文件
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak

# 注释掉不再支持的选项
sed -i '/GSSAPIAuthentication yes/s/^/#/' /etc/ssh/sshd_config
sed -i '/GSSAPICleanupCredentials/s/^/#/' /etc/ssh/sshd_config

# 确保以下关键配置正确(根据你的需求调整)
sed -i '/^#PermitRootLogin/s/^#//' /etc/ssh/sshd_config
sed -i '/^#PasswordAuthentication/s/^#//' /etc/ssh/sshd_config
sed -i '/^#PubkeyAuthentication/s/^#//' /etc/ssh/sshd_config

4.3 修复密钥文件权限

重要:OpenSSH 对密钥文件权限极其敏感,权限不正确会导致服务无法启动

1
2
3
chmod 600 /etc/ssh/ssh_host_*_key
chmod 644 /etc/ssh/ssh_host_*_key.pub
chmod 755 /etc/ssh

五、重启服务并验证

5.1 重启 SSH 服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 覆盖修复PAM配置文件(解决CentOS 7兼容性问题)
cat > /etc/pam.d/sshd << 'EOF'
#%PAM-1.0
auth required pam_sepermit.so
auth substack password-auth
auth include postlogin
# Used with polkit to reauthorize users in remote sessions
-auth optional pam_reauthorize.so prepare
account required pam_nologin.so
account include password-auth
password include password-auth
# pam_selinux.so close should be the first session rule
session required pam_selinux.so close
session required pam_loginuid.so
# pam_selinux.so open should only be followed by sessions to be executed in the user context
session required pam_selinux.so open env_params
session required pam_namespace.so
session optional pam_keyinit.so force revoke
session include password-auth
session include postlogin
# Used with polkit to reauthorize users in remote sessions
-session optional pam_reauthorize.so prepare
EOF

# 重新加载systemd配置
systemctl daemon-reload

# 重启sshd服务
systemctl restart sshd

# 设置开机自启
systemctl enable sshd

5.2 验证升级结果

1
2
# 查看新版本
ssh -V

预期输出:

1
OpenSSH_9.9p2, OpenSSL 1.1.1w  11 Sep 2023
1
2
# 检查服务状态
systemctl status sshd

5.3 测试连接

极其重要:绝对不要关闭当前 SSH 窗口! 新开一个终端窗口测试连接:

1
ssh root@你的服务器IP

六、清理与安全加固

6.1 关闭并卸载 telnet 服务

1
2
3
4
5
6
7
systemctl stop xinetd
systemctl disable xinetd
yum remove -y telnet-server telnet xinetd

# 恢复securetty文件
sed -i '/pts\/0/d' /etc/securetty
sed -i '/pts\/1/d' /etc/securetty

七、回滚方案(如果升级失败)

如果升级后 SSH 服务无法启动且无法修复,可以通过 Telnet 登录执行以下命令快速回退:

1
2
3
4
5
6
7
8
9
10
11
# 恢复备份的二进制文件
cp /usr/bin/ssh.bak /usr/bin/ssh
cp /usr/sbin/sshd.bak /usr/sbin/sshd

# 恢复备份的配置文件
rm -rf /etc/ssh
cp -r /etc/ssh.bak /etc/ssh
cp /etc/pam.d/sshd.bak /etc/pam.d/sshd

# 重启服务
systemctl restart sshd