第一步:查看当前系统 OpenSSH 版本
打开终端,输入以下命令,查看现在的ssh版本:
ssh -V
查看当前 OpenSSH 版本。如果版本低于 8.8(建议 9.x 以上),就需要升级。
第二步:下载最新的 OpenSSH 源码包
前往官方 OpenSSH 官网(或使用 wget)下载最新版本的源码包:
官方网站:https://www.openssh.com/portable.html
下载 9.7p1 版本并解压:
cd /mnt/sdb/qyy/ #切记,系统盘小,下载 安装软件都要装在数据盘,后期扩容方便,系统盘扩容很麻烦 wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.7p1.tar.gz tar -zxvf openssh-9.7p1.tar.gz cd openssh-9.7p1
第三步:安装编译依赖
yum install -y gcc make zlib-devel openssl-devel pam-devel
第四步:配置、编译、安装到 /opt/openssh-9.7
在源码目录下,运行:
./configure \ --prefix=/opt/openssh-9.7 \ --sysconfdir=/opt/openssh-9.7/etc \ --with-pam \ --with-ssl-engine make && make install
说明:
--prefix=/usr/local/openssh:指定安装路径,避免覆盖系统自带版本。--sysconfdir=/etc/ssh:使用系统原有的配置文件路径。--with-pam:支持 PAM 身份验证。--with-ssl-engine:启用 OpenSSL 引擎支持。
第五步:并行 SSH(18081 端口)配置
mkdir -p /opt/openssh-9.7/etc cp /etc/ssh/sshd_config /opt/openssh-9.7/etc/sshd_config vim /opt/openssh-9.7/etc/sshd_config
将
Port改为18081# NotifyHostKeys yes 注释这个
注释掉所有不兼容选项,如
RSAAuthentication、RhostsRSAAuthentication、GSSAPI*等修改 Subsystem:Subsystem sftp /opt/openssh-9.7/libexec/sftp-server 设置 UsePAM no
第六步:生成 HostKey并创建沙盒目录
/opt/openssh-9.7/bin/ssh-keygen -A chmod 600 /opt/openssh-9.7/etc/ssh_host_* #创建沙盒目录 mkdir -p /var/empty/sshd chown root:root /var/empty/sshd chmod 755 /var/empty/sshd
第七步:编写并行 SSH systemd 服务
cat > /etc/systemd/system/sshd-18081.service << 'EOF' [Unit] Description=OpenSSH 9.7p1 Server on port 18081 After=network.target [Service] ExecStart=/opt/openssh-9.7/sbin/sshd -D -f /opt/openssh-9.7/etc/sshd_config -p 18081 ExecReload=/bin/kill -HUP $MAINPID Restart=always RestartSec=5s [Install] WantedBy=multi-user.target EOF
第八步:启动并设置开机自启
systemctl daemon-reload systemctl enable sshd-18081 systemctl start sshd-18081 iptables -I INPUT -p tcp --dport 18081 -j ACCEPT service iptables save service iptables restart systemctl status sshd-18081 #确认并行ssh正常运行中 ############################################## #再通过远程shell运行:ssh -p 18081 root@127.0.0.1 确认一切正常
第九步:升级主 SSH(堡垒机访问端口 XXXX)
备份原版 ssh 与 sshd
cp -a /usr/bin/ssh /usr/bin/ssh.bak cp -a /usr/sbin/sshd /usr/sbin/sshd.bak 2>/dev/null || true
用新版替换系统可执行
ln -sf /opt/openssh-9.7/bin/ssh /usr/bin/ssh ln -sf /opt/openssh-9.7/sbin/sshd /usr/sbin/sshd
调整主配置文件监听端口
vim /etc/ssh/sshd_config
将 Port 设置为 XXXX
确认:
Subsystem sftp /opt/openssh-9.7/libexec/sftp-server UsePAM no
注释所有老旧选项
修改 systemd 主 SSH 服务为 simple 类型
vim /usr/lib/systemd/system/sshd.service
将 [Service] 段修改为:
Type=simple ExecStart=/usr/sbin/sshd -D -f /etc/ssh/sshd_config ExecReload=/bin/kill -HUP $MAINPID Restart=always RestartSec=5s
重新加载并启动主 SSH
systemctl daemon-reload systemctl restart sshd
验证主 SSH
ss -tlnp | grep :端口号 ssh -p 端口号 root@localhost ssh -V
第十步:防火墙与安全检查
开放端口
iptables -I INPUT -p tcp --dport 18081 -j ACCEPT iptables -I INPUT -p tcp --dport 62012 -j ACCEPT service iptables save service iptables restart
确认监听
ss -tlnp | grep -E ':18081|:端口号'
测试访问
并行后门:
ssh -p 18081 root@服务器IP
主服务:
堡垒机直接访问
第十一步:回滚与应急
如主服务故障,可通过并行后门回滚:
ssh -p 18081 root@服务器IP
恢复原版二进制
mv /usr/bin/ssh.bak /usr/bin/ssh mv /usr/sbin/sshd.bak /usr/sbin/sshd
恢复 systemd 配置
vim /usr/lib/systemd/system/sshd.service # 恢复原有内容 systemctl daemon-reload systemctl restart sshd
PonyTechLab