重要提醒(必看)
本方案针对 openEuler 22.03 LTS-SP4 系统
- 保持当前 SSH 会话不要关闭,新开一个终端执行脚本,防止升级断连
- 脚本全程自动:备份→装依赖→编译→安装→修复配置→重启→验证
- 兼容系统原生路径,无需手动改任何配置
# 查看当前 ssh 的地址
which ssh
which sshd
which ssh-keygen
cd /usr/sbin
ls ssh*
cd /usr/bin
ls ssh*
==下面命令是 /usr/bin/ssh 有时候可能 安装在 /usr/sbin/ssh ,升级命令需要对应调整==
命令
#!/bin/bash
set -e
DATE=$(date +%Y%m%d)
echo -e "\n===== OpenSSH 9.8p1 → 10.2p1 自动升级脚本 (openEuler 22.03 SP4) ====="
# ===================== 1. 自动备份(防炸必备) =====================
echo -e "\n[1/8] 备份原有SSH配置与二进制文件..."
cp -r /etc/ssh /etc/ssh.bak_${DATE}
cp /usr/sbin/sshd /usr/sbin/sshd.old_${DATE}
cp /usr/bin/ssh /usr/bin/ssh.old_${DATE}
cp /usr/bin/ssh-keygen /usr/bin/ssh-keygen.old_${DATE}
# ===================== 2. 安装 openEuler 编译依赖 =====================
echo -e "\n[2/8] 安装编译依赖..."
dnf groupinstall -y "Development Tools"
dnf install -y zlib-devel openssl-devel pam-devel libselinux-devel krb5-devel libedit-devel systemd-devel
# ===================== 3. 下载官方源码 =====================
echo -e "\n[3/8] 下载 OpenSSH 10.2p1 源码..."
cd /usr/src
rm -rf openssh-10.2p1*
wget -q https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-10.2p1.tar.gz
tar -zxf openssh-10.2p1.tar.gz
cd openssh-10.2p1
# ===================== 4. openEuler 专用编译配置 =====================
echo -e "\n[4/8] 配置编译参数..."
./configure \
--prefix=/usr \
--sysconfdir=/etc/ssh \
--with-pam \
--with-zlib \
--with-ssl-dir=/usr \
--with-privsep-path=/var/lib/sshd \
--with-md5-passwords \
--with-systemd \
--mandir=/usr/share/man
# ===================== 5. 编译安装 =====================
echo -e "\n[5/8] 开始编译安装..."
make -j$(nproc)
make install
# ===================== 6. 修复 PAM / systemd / SELinux =====================
echo -e "\n[6/8] 修复系统服务与权限..."
# 【关键】强制还原 openEuler 官方原版 PAM 配置(你手动能用的版本)
cat > /etc/pam.d/sshd <<'EOF'
#%PAM-1.0
auth substack password-auth
auth include postlogin
account required pam_nologin.so
account include password-auth
session required pam_selinux.so close
session required pam_loginuid.so
session optional pam_keyinit.so force revoke
session include password-auth
session include postlogin
session required pam_selinux.so open
EOF
chmod 644 /etc/pam.d/sshd
systemctl daemon-reexec
# 修复文件标签
restorecon -Rv /etc/ssh /usr/sbin/sshd /usr/bin/ssh
# 去掉报错的无效配置,只保留有效项
setsebool -P use_nfs_home_dirs 1
# ===================== 7. 自动修复登录配置(保证能登录) =====================
echo -e "\n[7/8] 修复登录配置(密码/PAM/root)..."
# 清理所有冲突配置(彻底清空)
sed -i '/^PasswordAuthentication/d' /etc/ssh/sshd_config
sed -i '/^PermitRootLogin/d' /etc/ssh/sshd_config
sed -i '/^UsePAM/d' /etc/ssh/sshd_config
sed -i '/^ChallengeResponseAuthentication/d' /etc/ssh/sshd_config
sed -i '/^UseDNS/d' /etc/ssh/sshd_config
# 注释 GSSAPI 相关(防止登录慢)
sed -i '/^GSSAPIAuthentication/ s/^/#/' /etc/ssh/sshd_config
sed -i '/^GSSAPICleanupCredentials/ s/^/#/' /etc/ssh/sshd_config
sed -i '/^GSSAPIKexAlgorithms/ s/^/#/' /etc/ssh/sshd_config
# 写入正确配置
cat >> /etc/ssh/sshd_config <<EOF
UsePAM yes
PasswordAuthentication yes
PermitRootLogin yes
ChallengeResponseAuthentication yes
UseDNS no
EOF
# 修复密钥权限(必须正确,否则 SSH 启动失败)
chmod 600 /etc/ssh/ssh_host_*key
chown root:root /etc/ssh/ssh_host_*key
# 临时关闭 SELinux 拦截(解决登录拦截)
setenforce 0
# ===================== 8. 重启服务 + 验证版本 =====================
echo -e "\n[8/8] 重启SSH服务并验证..."
systemctl restart sshd
sleep 2
echo -e "\n==================== 升级完成!当前版本 ===================="
ssh -V
sshd -V
echo -e "\n✅ 升级成功!请【新开终端】测试登录,确认正常再关闭旧会话!"
使用步骤
1. 创建脚本
运行
vim openssh_upgrade_102_openeuler.sh
粘贴上面全部脚本 → 按 ESC → 输入 :wq 保存
2. 授权并运行
bash
运行
chmod +x openssh_upgrade_102_openeuler.sh
sh ./openssh_upgrade_102_openeuler.sh
紧急回退脚本(升级失败救砖)
如果出现无法登录 / 启动异常,在原有会话里直接运行即可恢复 9.8p1:
bash
运行
#!/bin/bash
DATE=$(date +%Y%m%d)
echo "===== 紧急回退到旧版本 OpenSSH ====="
cp /usr/sbin/sshd.old_${DATE} /usr/sbin/sshd
cp /usr/bin/ssh.old_${DATE} /usr/bin/ssh
cp /usr/bin/ssh-keygen.old_${DATE} /usr/bin/ssh-keygen
rm -rf /etc/ssh && cp -r /etc/ssh.bak_${DATE} /etc/ssh
systemctl restart sshd
ssh -V
echo "✅ 回退完成!"
例外情况
如果执行有错误
ssh 无法连接
检查下方配置是否注释
# 注释掉报错的 GSSAPI 配置
# 注释 GSSAPIAuthentication
sed -i '/^GSSAPIAuthentication/ s/^/#/' /etc/ssh/sshd_config
# 注释 GSSAPICleanupCredentials
sed -i '/^GSSAPICleanupCredentials/ s/^/#/' /etc/ssh/sshd_config
# 注释 GSSAPIKexAlgorithms
sed -i '/^GSSAPIKexAlgorithms/ s/^/#/' /etc/ssh/sshd_config
# 检查
vim /etc/ssh/sshd_config
新窗口提示密码错误
# 强制还原正确的 PAM 配置(openEuler 官方原版)
cat > /etc/pam.d/sshd <<'EOF'
#%PAM-1.0
auth substack password-auth
auth include postlogin
account required pam_nologin.so
account include password-auth
session required pam_selinux.so close
session required pam_loginuid.so
session optional pam_keyinit.so force revoke
session include password-auth
session include postlogin
session required pam_selinux.so open
EOF
# 清理 sshd_config 所有冲突配置
sed -i '/^PasswordAuthentication/d' /etc/ssh/sshd_config
sed -i '/^PermitRootLogin/d' /etc/ssh/sshd_config
sed -i '/^UsePAM/d' /etc/ssh/sshd_config
sed -i '/^ChallengeResponseAuthentication/d' /etc/ssh/sshd_config
# 注释 GSSAPIAuthentication
sed -i '/^GSSAPIAuthentication/ s/^/#/' /etc/ssh/sshd_config
# 注释 GSSAPICleanupCredentials
sed -i '/^GSSAPICleanupCredentials/ s/^/#/' /etc/ssh/sshd_config
# 注释 GSSAPIKexAlgorithms
sed -i '/^GSSAPIKexAlgorithms/ s/^/#/' /etc/ssh/sshd_config
# 写入正确配置
cat >> /etc/ssh/sshd_config <<EOF
UsePAM yes
PasswordAuthentication yes
PermitRootLogin yes
ChallengeResponseAuthentication yes
UseDNS no
EOF
# 修复权限
chmod 644 /etc/pam.d/sshd
chmod 600 /etc/ssh/ssh_host_*key
chown root:root /etc/ssh/ssh_host_*key
# 临时关闭 SELinux 拦截
setenforce 0
# 重启 SSH
systemctl restart sshd
若遇到,无法粘贴命令行的方式,可使用 wget 命令下载后执行
wget https://seafile.cqlxcnp.com:19999/f/e91efab0313b4b91832f/?dl=1
如果 sshd -v 还是老版本
问题出在 sshd 服务端二进制文件可能没有被正确覆盖,或者 systemd 还在指向旧路径。
第一步:先确认这 3 个关键信息
在服务器上依次执行:
# 1. 看你手动执行 sshd -V 时,用的是哪个路径的文件
which sshd
# 2. 直接看 /usr/sbin/sshd 的版本(脚本里 configure --prefix=/usr 应该装在这里)
/usr/sbin/sshd -V
# 3. 看 systemd 服务文件里到底指向哪里
systemctl cat sshd | grep ExecStart
情况 1:which sshd 显示 /usr/local/sbin/sshd(最常见)
原因:你之前可能通过源码安装过旧版 OpenSSH,装到了 /usr/local 下,而 systemd 服务还在优先用这个路径。
解决:
# 强制把新编译的 sshd 覆盖过去(假设你新的在 /usr/sbin/sshd)
cp -f /usr/sbin/sshd /usr/local/sbin/sshd
# 或者直接修改 systemd 服务文件,把 ExecStart 改成 /usr/sbin/sshd
# 然后重启
systemctl daemon-reload
systemctl restart sshd
# 验证
sshd -V