CentOS 7 升级openssl和openssh
1.升级环境
OS版本:Centos 7.8
openssh和openssl 版本:
[root@localhost media]# ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
升级目标:
openssl 1.0.2k --> openssl 1.1.1k
openssh 7.4p1 --> openssh 8.6p1
备注:本文也适用于升级到openssh 9.6和9.7版本。
以下所有操作均在root用户下完成。
2.安装telnet
防止openssh升级出现问题,连不上服务器,建议开启telnet服务。
# 查看是否安装telnet服务
rpm -qa | grep telnet
rpm -qa | grep xinetd
# 若没有安装,使用yum安装
yum -y install telnet*
yum -y install xinetd
#使服务开机自启
systemctl enable xinetd.service
systemctl enable telnet.socket
#开启服务
systemctl start telnet.socket
systemctl start xinetd
vi /etc/securetty
在最后添加两行
pts/0
pts/1
systemctl restart xinetd
# 确认下是否启动成功
netstat -plnt |grep 23
3.升级OpenSSl
3.1 查看原版本
[root@localhost media]# openssl version -a
3.2 解压安装
安装系统包
[root@localhost media]# yum -y install net-tools perl gcc gcc++ make
解压安装
[root@localhost media]# tar zxvf openssl-1.1.1k-20210525.tar.gz
[root@localhost media]# cd openssl-1.1.1k/
[root@localhost openssl-1.1.1k]# ./config --prefix=/usr/local/openssl #检查环境
[root@localhost openssl-1.1.1k]# ./config -t
[root@localhost openssl-1.1.1k]# make && make install #编译安装
3.3 检查更新函数库
cd /usr/local
ldd /usr/local/openssl/bin/openssl
1 [root@localhost openssl-1.1.1k]# cd /usr/local/ 2 [root@localhost local]# ldd /usr/local/openssl/bin/openssl 3 linux-vdso.so.1 => (0x00007ffe03d81000) 4 libssl.so.1.1 => not found 5 libcrypto.so.1.1 => not found 6 libdl.so.2 => /lib64/libdl.so.2 (0x00007faf28080000) 7 libpthread.so.0 => /lib64/libpthread.so.0 (0x00007faf27e64000) 8 libc.so.6 => /lib64/libc.so.6 (0x00007faf27a96000) 9 /lib64/ld-linux-x86-64.so.2 (0x00007faf28284000)
[root@localhost local]# echo "/usr/local/openssl/lib" >> /etc/ld.so.conf #添加所缺函数库
[root@localhost local]# ldconfig -v #更新函数库
[root@localhost local]# openssl/bin/openssl version #查看新安装的版本
3.4 增加openssl软连接
[root@localhost local]# which openssl #查看旧版本openssl命令在哪里
[root@localhost local]# mv /bin/openssl /usr/bin/openssl.old #将旧版本openssl移除
[root@localhost local]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl #新版本制作软链接
[root@localhost local]# openssl version 最后查看版本,更新完毕
1 [root@localhost local]# openssl version 2 OpenSSL 1.0.2k-fips 26 Jan 2017
3.5 删除旧版openssl
#检查系统中旧版openssl软件
1 [root@localhost local]# rpm -qa |grep openssl 2 openssl098e-0.9.8e-29.el7.centos.3.x86_64 3 openssl-libs-1.0.2k-19.el7.x86_64 4 openssl-1.0.2k-19.el7.x86_64 5 xmlsec1-openssl-1.2.20-7.el7_4.x86_64
我们需要卸载 openssl-1.0.2k-19.el7.x86_64
[root@localhost local]# rpm -e --nodeps openssl-1.0.2k-19.el7.x86_64
卸载旧版本后,需要重新为新版做软连接:
[root@localhost local]# ln -s /usr/local/openssl/bin/openssl /usr/bin/openssl
4.升级openssh
4.1 解压安装包
[root@localhost media]# tar zxvf openssh-8.6p1-20210419.tar.gz
4.2 移除旧版本
[root@localhost media]# mv /etc/ssh /etc/ssh.old
注意:移除之后,不能退出当前会话;若退出,只能通过telnet登录了。
4.3 安装系统包
[root@localhost media]# yum -y install zlib-devel pam-devel openssl-devel
4.4 编译安装
[root@localhost media]# cd openssh-8.6p1/
[root@localhost openssh-8.6p1]#./configure --prefix=/usr/local/openssh --sysconfdir=/etc/ssh --with-pam --with-ssl-dir=/usr/local/openssl --with-md5-passwords --mandir=/usr/share/man --with-zlib=/usr/local/zlib --without-hardening
config过程中遇到的错误:
报错1:configure: error: *** zlib.h missing
需安装:
yum -y install zlib-devel
报错2:configure: error: PAM headers not found
需安装:
yum -y install pam-devel
报错3:find openssl head...ERROR
原因是旧版本的openssl在系统里,需要卸载掉,需要卸载openssl。
处理过程:
参考步骤3.5.
开始编译安装:
[root@localhost openssh-8.6p1]# make && make install
4.5 修改启动脚本
# 拷贝启动脚本
[root@localhost openssh-8.6p1]# cp ./contrib/redhat/sshd.init /etc/init.d/sshd
# 修改启动脚本
[root@localhost openssh-8.6p1]# vi /etc/init.d/sshd
# 按下图修改,需要注意,此路径是你安装新版本openssh的路径,根据你的实际情况修改:
SSHD=/usr/local/openssh/sbin/sshd
sed -i 's#/usr/sbin/sshd#/usr/local/openssh/sbin/sshd#' /etc/init.d/sshd
#配置sshd开机启动
[root@localhost openssh-8.6p1]# chkconfig --add sshd
[root@localhost openssh-8.6p1]# chkconfig --level 2345 sshd on
[root@localhost openssh-8.6p1]# chkconfig --list
4.6 修改sshd配置文件/etc/ssh/sshd_config
[root@localhost openssh-8.6p1]# echo "PermitRootLogin yes" >> /etc/ssh/sshd_config #直接用root登录终端
[root@localhost openssh-8.6p1]# echo 'X11Forwarding yes' >> /etc/ssh/sshd_config #设置是否允许X11转发
[root@localhost openssh-8.6p1]# echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config #是否允许密码验证
4.7 卸载原有ssh
for i in $(rpm -qa |grep openssh);do rpm -e $i --nodeps;done
提示警告:
[root@localhost openssh-8.6p1]# for i in $(rpm -qa |grep openssh);do rpm -e $i --nodeps;done
warning: /etc/ssh/moduli saved as /etc/ssh/moduli.rpmsave
warning: /etc/ssh/sshd_config saved as /etc/ssh/sshd_config.rpmsave
warning: /etc/ssh/ssh_config saved as /etc/ssh/ssh_config.rpmsave
注意:如果卸载旧版本过程中,配置文件没有自动备份,需要从前面的/etc/ssh.old中把对应的文件cp到原来的位置。同时注意当前ssh端口是否为默认端口。
将警告中被修改的文件名字再改回来
[root@localhost openssh-8.6p1]# mv /etc/ssh/sshd_config.rpmsave /etc/ssh/sshd_config
[root@localhost openssh-8.6p1]# mv /etc/ssh/ssh_config.rpmsave /etc/ssh/ssh_config
[root@localhost openssh-8.6p1]# mv /etc/ssh/moduli.rpmsave /etc/ssh/moduli
4.8 替换相关命令,并重启sshd服务
[root@localhost openssh-8.6p1]# cp -arp /usr/local/openssh/bin/* /usr/bin/ #替换相关命令
[root@localhost openssh-8.6p1]# service sshd restart #重启sshd服务
验证升级后版本
1 [root@localhost openssh-8.6p1]# ssh -V 2 OpenSSH_8.6p1, OpenSSL 1.1.1k 25 Mar 2021
4.9 关闭telnet
#关闭telnet服务
systemctl stop telnet.socket
systemctl stop xinetd
#关闭telnet服务开机自启
systemctl disable xinetd.service
systemctl disable telnet.socket