当前位置: 首页 > news >正文

在CentOS 7.9上升级OpenSSH到9.9p2

要升级OpenSSH就需要升级OpenSSL

一.升级OpenSSL

在CentOS 7.9上升级OpenSSL到3.0.18需要从源码编译安装。以下是详细步骤:

  1. 准备工作
# 更新系统
yum update -y# 安装编译依赖
yum install -y gcc gcc-c++ make perl perl-core zlib-devel wget# 创建工作目录
mkdir -p /usr/local/openssl_src
cd /usr/local/openssl_src
  1. 备份现有OpenSSL
# 备份现有openssl
cp -r /usr/bin/openssl /usr/bin/openssl.bak
cp -r /usr/include/openssl /usr/include/openssl.bak
cp -r /usr/lib64/openssl /usr/lib64/openssl.bak
cp -r /usr/lib64/libssl* /usr/lib64/libssl.bak/
cp -r /usr/lib64/libcrypto* /usr/lib64/libcrypto.bak/
  1. 下载并编译OpenSSL 3.0.18
# 下载源码
wget https://www.openssl.org/source/openssl-3.0.18.tar.gz
tar -xzf openssl-3.0.18.tar.gz
cd openssl-3.0.18# 配置编译选项
./config --prefix=/usr/local/openssl --openssldir=/usr/local/openssl shared zlib# 编译并安装
make -j$(nproc)
make install
  1. 创建符号链接和配置系统
# 备份原有命令
mv /usr/bin/openssl /usr/bin/openssl.old
mv /usr/include/openssl /usr/include/openssl.old# 创建新版本符号链接
ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
ln -sf /usr/local/openssl/include/openssl /usr/include/openssl# 更新库文件
echo "/usr/local/openssl/lib64" > /etc/ld.so.conf.d/openssl-3.0.18.conf
ldconfig
  1. 验证安装
# 检查版本
openssl version -a# 测试基本功能
openssl version
openssl genrsa -out test.key 2048
openssl req -new -key test.key -out test.csr
  1. 配置环境变量
# 添加到profile
echo 'export PATH=/usr/local/openssl/bin:$PATH' >> /etc/profile
echo 'export LD_LIBRARY_PATH=/usr/local/openssl/lib64:$LD_LIBRARY_PATH' >> /etc/profile# 重新加载环境变量
source /etc/profile
  1. 验证依赖关系
# 检查重要系统工具是否正常工作
openssl version
curl --version
wget --version# 检查动态库依赖
ldd /usr/local/openssl/bin/openssl
  1. 重要注意事项
    系统兼容性检查
# 检查哪些包依赖openssl
rpm -qa --whatrequires openssl# 检查系统关键服务
systemctl status sshd
systemctl status httpd

回滚方案(如果需要)

# 恢复备份文件
mv /usr/bin/openssl.old /usr/bin/openssl
mv /usr/include/openssl.old /usr/include/openssl
rm -f /etc/ld.so.conf.d/openssl-3.0.18.conf
ldconfig
  1. 测试完整功能
# 创建测试证书
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=test.example.com"# 验证证书
openssl x509 -in cert.pem -text -noout# 测试加密解密
echo "Hello OpenSSL 3.0" | openssl enc -aes-256-cbc -salt -pass pass:test

可能遇到的问题和解决方案
依赖问题:如果系统工具出现问题,可能需要重新编译相关工具

库冲突:确保 /usr/local/openssl/lib64 在库搜索路径中优先级最高

符号链接:确认所有符号链接正确指向新版本

安全建议
升级后建议:

重新生成所有SSL证书和密钥

更新相关服务的配置文件

进行全面的安全测试

监控系统日志确保服务正常运行

这个升级过程会将OpenSSL从系统默认的1.0.2版本升级到3.0.18,请确保在测试环境中验证后再在生产环境使用。

二.升级OpenSSH

  1. 下载和编译
cd /usr/src
wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.9p2.tar.gz
tar -xzf openssh-9.9p2.tar.gz
cd openssh-9.9p2# 配置编译选项
./configure \--prefix=/usr \--sysconfdir=/etc/ssh \--with-pam \--with-md5-passwords \--with-tcp-wrappers \--with-ssl-dir=/usr/local/openssl \--with-privsep-path=/var/empty/sshd \--with-selinux

报错:
configure: error: PAM headers not found
解决:
sudo yum install pam-devel # 对于使用 yum 的系统
或者
sudo dnf install pam-devel # 对于使用 dnf 的系统

编译和安装

make
  1. 停止服务并备份旧版本
systemctl stop sshd
cp /usr/sbin/sshd /usr/sbin/sshd.old
cp /usr/bin/ssh /usr/bin/ssh.old
  1. 安装新版本
make install

错误1:
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0640 for ‘/etc/ssh/ssh_host_rsa_key’ are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Unable to load host key “/etc/ssh/ssh_host_rsa_key”: bad permissions
Unable to load host key: /etc/ssh/ssh_host_rsa_key
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0640 for ‘/etc/ssh/ssh_host_ecdsa_key’ are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Unable to load host key “/etc/ssh/ssh_host_ecdsa_key”: bad permissions
Unable to load host key: /etc/ssh/ssh_host_ecdsa_key
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0640 for ‘/etc/ssh/ssh_host_ed25519_key’ are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Unable to load host key “/etc/ssh/ssh_host_ed25519_key”: bad permissions
Unable to load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available – exiting.
make: [check-config] Error 1 (ignored)
解决:
sudo chmod 600 /etc/ssh/ssh_host_key
sudo chmod 644 /etc/ssh/ssh_host
_key.pub
错误2:
/etc/ssh/sshd_config line 79: Unsupported option GSSAPIAuthentication
/etc/ssh/sshd_config line 80: Unsupported option GSSAPICleanupCredentials
解决
vi /etc/ssh/sshd_config
#GSSAPIAuthentication no
#GSSAPICleanupCredentials no

  1. 配置更新
    更新sshd_config
# 备份原配置
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak# 创建优化配置
cat > /etc/ssh/sshd_config << 'EOF'
# OpenSSH 9.9p2 Configuration
Port 22
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_keySyslogFacility AUTHPRIV
LogLevel INFO# Authentication
PermitRootLogin yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication yes
PermitEmptyPasswords no
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes
UsePAM yes# Security
X11Forwarding yes
PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
ClientAliveInterval 300
ClientAliveCountMax 2
MaxAuthTries 3
MaxSessions 10
UseDNS no# Ciphers and Kex (updated for OpenSSH 9.9)
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group-exchange-sha256
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com,aes256-ctr,aes192-ctr,aes128-ctr
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com,umac-128-etm@openssh.com,hmac-sha2-512,hmac-sha2-256AcceptEnv LANG LC_*
Subsystem sftp /usr/libexec/sftp-server
EOF

生成新的主机密钥

ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key -N ""
ssh-keygen -t ecdsa -b 521 -f /etc/ssh/ssh_host_ecdsa_key -N ""
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N ""
  1. 更新PAM配置
cat > /etc/pam.d/sshd << 'EOF'
#%PAM-1.0
auth       required     pam_sepermit.so
auth       substack     password-auth
auth       include      postlogin
account    required     pam_nologin.so
account    include      password-auth
password   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
  1. 创建systemd服务
cat > /usr/lib/systemd/system/sshd.service << 'EOF'
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service[Service]
Type=notify
EnvironmentFile=-/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s[Install]
WantedBy=multi-user.target
EOF
  1. 设置权限和启动
# 设置权限
chmod 600 /etc/ssh/ssh_host_*_key
chmod 644 /etc/ssh/ssh_host_*_key.pub# 创建必要的目录
mkdir -p /var/empty/sshd
chmod 711 /var/empty/sshd# 重新加载systemd并启动服务
systemctl daemon-reload
systemctl enable sshd
systemctl start sshd

验证安装

  1. 检查版本
    bash
ssh -V
应该输出:OpenSSH_9.9p2, OpenSSL 1.0.2k-fips 26 Jan 2017
  1. 检查服务状态
systemctl status sshd
ss -tlnp | grep :22
  1. 测试连接
    从另一台机器测试连接:
ssh -o BatchMode=yes -o ConnectTimeout=5 username@server-ip echo "Connection successful"

故障排除
常见问题解决:连接被拒绝

# 检查防火墙
firewall-cmd --list-all
firewall-cmd --permanent --add-service=ssh
firewall-cmd --reload# 检查SELinux
setenforce 0
# 或修复上下文
restorecon -Rv /etc/ssh /usr/sbin/sshd /usr/bin/ssh

权限问题


chmod 600 /etc/ssh/ssh_host_*_key
chown root:root /etc/ssh/ssh_host_*_key

查看日志

journalctl -u sshd -f
tail -f /var/log/secure

回滚方案
如果升级失败,可以回滚到原版本:

systemctl stop sshd# 如果是源码安装
mv /usr/sbin/sshd.old /usr/sbin/sshd
mv /usr/bin/ssh.old /usr/bin/ssh# 如果是RPM安装
yum downgrade openssh openssh-server openssh-clients# 恢复配置
cp -r /etc/ssh_backup_* /etc/ssh/
cp /etc/pam.d/sshd_backup_* /etc/pam.d/sshdsystemctl start sshd

安全建议
升级完成后建议:

更新加密算法配置以适应新版本

考虑禁用不安全的算法

启用双因子认证(如需要)

定期检查安全更新

这个升级过程相对安全,但务必在测试环境验证后再在生产环境操作。

捡有用的看吧!

http://www.dtcms.com/a/540376.html

相关文章:

  • asp 网站支持多语言想建立一个网站
  • Spring Boot3零基础教程,Spring Security 简介,笔记80
  • 调试技巧:从 IDE 调试到生产环境定位问题,提升调试效率的全方位指南
  • 服务器和docker容器时间不一致相关问题
  • Vue+Element Plus 表格工具栏组件:动态按钮 + 搜索控制的优雅实现​
  • 上海网站建设平台什么是seo标题优化
  • 网络编程之WebSocket(1)
  • Electron_Vue3 自定义系统托盘及退出二次确认
  • 为什么 Electron 项目推荐使用 Monorepo 架构 [特殊字符][特殊字符][特殊字符]
  • BLIP2 工业实战(一):从零实现 LAVIS 跌倒检测 (微调与“踩坑”指南)
  • NPM下载和安装图文教程(附安装包)
  • 2025 年台湾 5 大 CDP 平台推荐比较
  • 【数据结构】栈(Stack)详解——数据结构的“后进先出”
  • Java 大视界 -- Java 大数据在智能金融理财产品风险评估与个性化配置中的应用
  • Bootstrap4 安装使用指南
  • 怎么建设购物网站免费入驻的网站设计平台
  • vue2 将接口返回数据导出为 excel 文件
  • Java 使用 Spire.XLS 库合并 Excel 文件实践
  • Vultr × Caddy 多站点反向代理 + 负载均衡网关系统实战
  • 【数据结构】(C++数据结构)查找算法与排序算法详解
  • @pytest.fixture函数怎么传变量参数
  • Excel高性能异步导出完整方案!
  • 网站正在建设 敬请期待免费的cms模板
  • 输电线路绝缘子缺陷检测图像数据集VOC+YOLO格式1578张3类别
  • 跨文化理解的困境与AI大模型作为“超级第三方“的桥梁作用
  • JDK版本管理工具JVMS
  • 【JUnit实战3_18】第十章:用 Maven 3 运行 JUnit 测试(上)
  • SQLite 核心知识点讲解
  • JAiRouter v1.1.0 发布:把“API 调没调通”从 10 分钟压缩到 10 秒
  • 自建网站如何赚钱c2c模式为消费者提供了便利和实惠