安装编译依赖

dnf -y install gcc make zlib-devel openssl-devel pam-devel libselinux-devel

下载并解压 OpenSSH 9.8p1

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

配置编译

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

编译 & 安装

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

准备新 sshd 的配置 & hostkey

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

Port 2222
PermitRootLogin yes
PasswordAuthentication yes
UsePAM yes

创建 systemd 服务(并行跑)

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 服务

systemctl daemon-reload
systemctl enable --now sshd-98

放行测试端口

firewall-cmd --add-port=2222/tcp --permanent
firewall-cmd --reload

测试新版本

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

切换新版本

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

从外部测试 22 端口

ssh -p 22 root@你的IP

若 22 端口连不上 立马回滚

systemctl stop sshd-98
systemctl start sshd

若 22 端口连上 禁用旧服务

systemctl disable sshd

扫描 ssh 版本

ssh-keyscan -p 22 你的IP
文章作者: Captain
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 One piece
教程
喜欢就支持一下吧