centos7升级openssh版本到9.9p1

默认centos7的openssh最多升级到7.4p1,但是最近系统的安全漏洞扫描,发现openssh的很多漏洞,需要升级openssh才能解决问题。通过yum升级无法得到目的,只能通过手动方式升级。结合其他同学总结的文章,以及自己实操,总结如下:

一、安装 Telnet 服务
因为升级openssh需要重启openssh服务,如果配置错误导致openssh服务无法启动,本地就无法通过ssh远程连接到服务器,到时就抓瞎了。

  1. CentOS 7 默认不安装 Telnet 服务,你需要手动安装:

yum install telnet-server xinetd -y

  1. 手动创建 /etc/xinetd.d/telnet 文件
    执行以下命令:

cat > /etc/xinetd.d/telnet <<EOF
service telnet
{
flags = REUSE
socket_type = stream
wait = no
user = root
server = /usr/sbin/in.telnetd
log_on_failure += USERID
disable = no
}
EOF

  1. 启动 xinetd(telnet 由它管理)

systemctl start xinetd
systemctl enable xinetd

  1. 在 /etc/securetty 末尾加上以下内容以允许通过 Telnet 登录:

pts/0
pts/1
pts/2
pts/3
然后重启 xinetd:
systemctl restart xinetd

  1. 测试 Telnet 是否正常
    从本机或其他主机上测试:
    telnet <你的IP地址>

  2. Telnet登录的账号和密码跟ssh一致,但如果你不知道ssh密码该怎么办?
    你可以通过当前的 SSH 登录会话重设 root 密码,执行:

passwd

它会提示你输入新密码两次即可。

二、升级openssh

  1. 安装gcc等工具
    执行:

yum install gcc -y
yum groupinstall "Development Tools" -y
yum install pam-devel zlib-devel openssl-devel -y

否则,编译openssh时,会出现“configure: error: no acceptable C compiler found in $PATH”错误。

  1. 备份老版本openssh

tar cvf /root/sshd20250421.tar /usr/lib/systemd/system/sshd*
mv /usr/lib/systemd/system/sshd-keygen.target{,.ori}
mv /usr/lib/systemd/system/sshd.service{,.ori}
mv /usr/lib/systemd/system/sshd.socket{,.ori}
mv /usr/lib/systemd/system/sshd-keygen@.service{,.ori}
mv /usr/lib/systemd/system/sshd@.service{,.ori}
mv /etc/ssh

  1. 编译安装 OpenSSL 新版本(1.1.1w)
    安装OpenSSL新版本的目的是因为编译openssh 9.9p1版本时,至少要1.1.1版本,会提示:checking OpenSSL library version... configure: error: OpenSSL >= 1.1.1 required (have "100020bf (OpenSSL 1.0.2k-fips 26 Jan 2017)")
    执行:

cd usr/local/src
wget https://www.openssl.org/source/openssl-1.1.1w.tar.gz
tar zxvf openssl-1.1.1w.tar.gz
cd openssl-1.1.1w
./config --prefix=/usr/local/openssl-1.1.1 --openssldir=/usr/local/openssl-1.1.1 shared zlib
make -j$(nproc)
make install

  1. 指定 OpenSSL 路径
    以上步骤执行后,发现ldconfig中没有指定新的openssh版本,会指导启动新的openssh时报错:code=exited, status=127。执行以下命令使新版本openssl在系统中生效:

echo "/usr/local/openssl-1.1.1/lib" > /etc/ld.so.conf.d/openssl.conf
ldconfig

验证 新版OpenSSL 是否生效 ,如果有openssl-1.1.1说明生效

ldconfig -p | grep ssl

  1. 设置环境变量让 OpenSSH 使用新 OpenSSL
    编辑 ~/.bashrc,增加以下配置:

export PATH=/usr/local/openssl-1.1.1/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/openssl-1.1.1/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/usr/local/openssl-1.1.1/lib/pkgconfig:$PKG_CONFIG_PATH

然后刷新环境变量:

source ~/.bashrc

  1. 编译安装 OpenSSH,指定 OpenSSL 路径

wget https://mirrors.aliyun.com/pub/OpenBSD/OpenSSH/portable/openssh-9.9p1.tar.gz
tar xvfz openssh-*.tar.gz
cd /root/openssh-9.9p1
./configure --prefix=/usr/local
--sysconfdir=/etc/ssh
--with-pam
--with-zlib
--with-ssl-dir=/usr/local/openssl-1.1.1
make
make install

三、配置新的sshd系统服务

  1. 生成新的服务配置文件,3个

cat > /usr/lib/systemd/system/sshd-keygen.service << EOF
[Unit]
Description=OpenSSH Server Key Generation
ConditionPathExists=|!/etc/ssh/ssh_host_rsa_key
ConditionPathExists=|!/etc/ssh/ssh_host_ecdsa_key
ConditionPathExists=|!/etc/ssh/ssh_host_ed25519_key
ConditionPathExists=|!/etc/ssh/ssh_host_dsa_key
PartOf=sshd.service sshd.socket

[Service]
ExecStart=/usr/local/bin/ssh-keygen
Type=oneshot
RemainAfterExit=yes
EOF


cat > /usr/lib/systemd/system/sshd.service << EOF
[Unit]
Description=OpenSSH server daemon
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/local/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
EOF


cat > /usr/lib/systemd/system/sshd.socket << EOF
[Unit]
Description=OpenSSH Server Socket
Conflicts=sshd.service

[Socket]
ListenStream=22
Accept=yes

[Install]
WantedBy=sockets.target
EOF

  1. 将3个服务配置文件属性改为644

chmod 644 /usr/lib/systemd/system/sshd*

  1. ssh客户端程序更新

mv /usr/bin/ssh /usr/bin/ssh.old
ln -s /usr/local/bin/ssh /usr/bin/ssh

  1. 修改sshd配置
    openssh编译安装时会自动生成新sshd配置文件,所以参考原sshd配置/etc/ssh/sshd_config,修改新sshd配置
    笔者的主要修改的配置如下,个人根据实际情况调整。
    修改/etc/ssh/sshd_config配置:

UsePAM yes
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
PermitRootLogin yes
PasswordAuthentication yes

  1. 重启sshd服务

systemctl daemon-reload
systemctl restart sshd
systemctl enable sshd

  1. 查看sshd服务启动状态

systemctl status sshd

  1. 重新远程ssh
    断开当前已连接的ssh,重新连接,如果能正常连接,说明sshd服务配置正确。

  2. 查看sshd版本

ssh -V

如果是OpenSSH_9.9p1, OpenSSL 1.1.1w 11 Sep 2023,说明升级成功

三、关闭telnet服务
如果升级openssh成功,则可以关闭telnet服务。
因为我们使用的是 xinetd 管理 Telnet 服务,因此使用xinetd来关闭telnet服务。

  1. 检查是否通过 xinetd 启动

systemctl status xinetd

  1. 关闭 Telnet 服务
    编辑配置文件/etc/xinetd.d/telnet,禁用 telnet:
    vi /etc/xinetd.d/telnet
    将:
    disable = no
    修改为:
    disable = yes
    保存后,重启 xinetd:

systemctl restart xinetd

参考链接:https://developer.aliyun.com/article/1654590

posted @ 2025-04-21 18:21  星辰下的键盘  阅读(1826)  评论(0)    收藏  举报