在CentOS 7.9上升级OpenSSH到9.9p2
要升级OpenSSH就需要升级OpenSSL
一.升级OpenSSL
在CentOS 7.9上升级OpenSSL到3.0.18需要从源码编译安装。以下是详细步骤:
- 准备工作
# 更新系统
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
- 备份现有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/
- 下载并编译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
- 创建符号链接和配置系统
# 备份原有命令
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
- 验证安装
# 检查版本
openssl version -a# 测试基本功能
openssl version
openssl genrsa -out test.key 2048
openssl req -new -key test.key -out test.csr
- 配置环境变量
# 添加到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
- 验证依赖关系
# 检查重要系统工具是否正常工作
openssl version
curl --version
wget --version# 检查动态库依赖
ldd /usr/local/openssl/bin/openssl
- 重要注意事项
系统兼容性检查
# 检查哪些包依赖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
- 测试完整功能
# 创建测试证书
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
- 下载和编译
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
- 停止服务并备份旧版本
systemctl stop sshd
cp /usr/sbin/sshd /usr/sbin/sshd.old
cp /usr/bin/ssh /usr/bin/ssh.old
- 安装新版本
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
- 配置更新
更新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 ""
- 更新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
- 创建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
- 设置权限和启动
# 设置权限
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
验证安装
- 检查版本
bash
ssh -V
应该输出:OpenSSH_9.9p2, OpenSSL 1.0.2k-fips 26 Jan 2017
- 检查服务状态
systemctl status sshd
ss -tlnp | grep :22
- 测试连接
从另一台机器测试连接:
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
安全建议
升级完成后建议:
更新加密算法配置以适应新版本
考虑禁用不安全的算法
启用双因子认证(如需要)
定期检查安全更新
这个升级过程相对安全,但务必在测试环境验证后再在生产环境操作。
