安装编译依赖

shell复制
dnf -y install gcc make zlib-devel openssl-devel pam-devel libselinux-devel

下载并解压 OpenSSH 9.8p1

shell复制
cd /usr/local/src
curl -LO https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.8p1.tar.gz
tar xf openssh-9.8p1.tar.gz
cd openssh-9.8p1

配置编译

shell复制
./configure \
  --prefix=/usr/local/openssh-9.8p1 \
  --sysconfdir=/etc/ssh-9.8p1 \
  --with-pam \
  --with-privsep-path=/var/lib/sshd \
  --with-ssl-engine

编译 & 安装

shell复制
make -j$(nproc)
make install
/usr/local/openssh-9.8p1/bin/ssh -V

准备新 sshd 的配置 & hostkey

shell复制
mkdir -p /etc/ssh-9.8p1
cp /etc/ssh/sshd_config /etc/ssh-9.8p1/sshd_config
/usr/local/openssh-9.8p1/bin/ssh-keygen -A -f /etc/ssh-9.8p1

修改新 sshd 的监听端口(先用 2222)

vi /etc/ssh-9.8p1/sshd_config

shell复制
Port 2222
PermitRootLogin yes
PasswordAuthentication yes
UsePAM yes

创建 systemd 服务(并行跑)

shell复制
cat >/etc/systemd/system/sshd-98.service <<'EOF'
[Unit]
Description=OpenSSH 9.8p1 Server
After=network.target

[Service]
ExecStart=/usr/local/openssh-9.8p1/sbin/sshd -D -f /etc/ssh-9.8p1/sshd_config
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

启动 sshd 服务

shell复制
systemctl daemon-reload
systemctl enable --now sshd-98

放行测试端口

shell复制
firewall-cmd --add-port=2222/tcp --permanent
firewall-cmd --reload

测试新版本

shell复制
/usr/local/openssh-9.8p1/bin/ssh -p 2222 root@127.0.0.1
# 最好从外部测试
ssh -p 2222 root@你的IP

切换新版本

shell复制
systemctl stop sshd
vi /etc/ssh-9.8p1/sshd_config  # 把 Port 2222 改成 22
systemctl restart sshd-98

从外部测试 22 端口

shell复制
ssh -p 22 root@你的IP

若 22 端口连不上 立马回滚

shell复制
systemctl stop sshd-98
systemctl start sshd

若 22 端口连上 禁用旧服务

shell复制
systemctl disable sshd

扫描 ssh 版本

shell复制
ssh-keyscan -p 22 你的IP