随着OpenSSH更新,之前的版本也存在了一些已知漏洞,给系统带来安全风险,OpenSSH 8.5p1版本已经发布!
一、升级说明
1、升级OpenSSH后,原有公钥失效,信任关系需要重新配置;
2、升级过程需要停止sshd服务,会导致ssh、sftp、scp无法使用;
3、升级需要关闭防火墙服务,升级完成后根据业务需要再选择打开或关闭防火墙;
4、升级需要关闭selinux服务;
5、升级前需要开启telnet,防止升级失败,系统无法登录,对应的防火墙需要开启23端口,安装需要telnet相关依赖包
6、升级过程中需要刷新lib库:ldconfig -v;
7、升级顺序:顺序是“zlib库-> openssl -> openssh”;
8、升级需要gcc、make、perl、zlib、zlib-devel、pam、pam-devel等依赖包;
二、升级准备
1、确保YUM功能可用,如果服务器无法上网,可本地挂载对应操作系统版本的ISO镜像作为本地;
2、下载下面的安装包上传到系统;
zlib-1.2.11.tar.gz openssl-1.1.1k.tar.gz openssh-8.5p1.tar.gz
三、升级操作
按下面步骤进行操作,考虑到不同系统配置环境不通,建议预先进行测试。请务必开启telnet,以免升级过程中断导致无法连接系统。
1、为了方便升级操作,我们先刷新YUM缓存,顺便可以检测YUM能不能正常使用
执行命令,等待缓存刷新完毕,如果报错,则说明YUM不能用,请解决后再进行升级操作
yum makecache
2、安装启动telnet服务端,以便升级失败时能通过telnet协议登录系统
执行命令,启动telnet服务,如果未安装服务,则通过YUM进行安装
cp /etc/securetty /etc/securetty.bak echo "pts/0" >> /etc/securetty echo "pts/1" >> /etc/securetty systemctl restart telnet.socket systemctl restart xinetd systemctl enable telnet.socket systemctl enable xinetd //未安装服务或不能正常启动时,执行下面命令进行安装和配置 yum -y install xinetd telnet-server cp /etc/securetty /etc/securetty.bak echo "pts/0" >> /etc/securetty echo "pts/1" >> /etc/securetty systemctl restart telnet.socket systemctl restart xinetd systemctl enable telnet.socket systemctl enable xinetd
3、为避免防火墙和selinux造成影响,在升级过程中,建议将其关闭
执行下面的命令关闭selinux和firewall防火墙,使用iptables的系统建议也将其关闭
setenforce 0 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config systemctl stop firewalld.service systemctl disable firewalld.service
4、安装依赖包组件,等待安装完…… ps:我这里已经提前安装过了,第一次升级的,需耐心等待安装完毕。
yum -y install gcc gcc-c++ make pam pam-devel openssl-devel pcre-devel perl zlib-devel
5、停止并卸载系统自带ssh组件(之前编译升级过的系统执行后会报错,可忽略)
systemctl stop sshd cp -r /etc/ssh /etc/ssh.old cp /etc/init.d/ssh /etc/init.d/ssh.old rpm -qa | grep openssh rpm -e `rpm -qa | grep openssh` --nodeps rpm -qa | grep openssh //正常卸载自带ssh后,执行此条命令,没有结果返回
6、安装和配置zlib
tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure --prefix=/usr/local/zlib make && make install ls -l /usr/local/zlib grep "/usr/local/zlib/lib" /etc/ld.so.conf.d/zlib.conf || echo '/usr/local/zlib/lib' >> /etc/ld.so.conf.d/zlib.conf ldconfig -v
7、安装和配置openssl(过程较长,耐心等待,切勿打断)
mv -f /usr/bin/openssl /usr/bin/openssl.old mv -f /usr/include/openssl /usr/include/openssl.old mv -f /usr/lib64/openssl /usr/lib64/openssl.old rm -rf /usr/local/ssl tar -zxvf openssl-1.1.1k.tar.gz cd openssl-1.1.1k ./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib && make && make install ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl ln -s /usr/local/ssl/include/openssl /usr/include/openssl ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1 ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 grep "/usr/local/ssl/lib" /etc/ld.so.conf.d/ssl.conf || echo '/usr/local/ssl/lib' >> /etc/ld.so.conf.d/ssl.conf grep "/usr/local/lib" /etc/ld.so.conf.d/ssl.conf || echo '/usr/local/lib' >> /etc/ld.so.conf.d/ssl.conf ldconfig -v openssl version -a
8、安装和配置openssh8.5(过程较长,耐心等待,切勿打断)
rm -rf /etc/ssh tar -zxvf openssh-8.5p1.tar.gz cd openssh-8.5p1 ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-openssl-includes=/usr/local/ssl/include --with-ssl-dir=/usr/local/ssl --with-zlib --with-md5-passwords make && make install echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config echo "PermitRootLogin yes" >> /etc/ssh/sshd_config cd .. //退出刚才解压后的openssh-8.5p1目录 cp -p openssh-8.5p1/contrib/redhat/sshd.init /etc/init.d/sshd chmod +x /etc/init.d/sshd chmod 600 /etc/ssh/ssh_host_rsa_key chmod 600 /etc/ssh/ssh_host_ecdsa_key chmod 600 /etc/ssh/ssh_host_ed25519_key chkconfig --add sshd chkconfig sshd on systemctl restart sshd systemctl status sshd ssh -V
9、第8步执行完毕后,如果ssh服务正常运行,且版本号正确,即升级完成,接下来就关闭telnet服务,根据业务需要选择是否还原防火墙等安全策略配置;如果执行失败,请排除错误后再完成升级。
systemctl stop telnet.socket && systemctl stop xinetd systemctl disable telnet.socket && systemctl disable xinetd systemctl start firewalld.service //根据业务需要选择是否还原防火墙等安全策略配置 systemctl enable firewalld.service //根据业务需要选择是否还原防火墙等安全策略配置
ps:注意,由于升级后验证密钥发生变化,重新连接ssh会提示错误,这个时候根据提示找到客户端系统的known_hosts 文件中与所连接的ip相关的内容进行删除,重新连接ssh会出来提示,输入yes即可正常连接!
也可以执行下面的脚本(保存为*.sh文件,并赋予执行权限,与上面三个压缩包放在同一个目录下)进行升级,建议按照上面的方法测试没问题后,使用下面的脚本升级~
#!/bin/bash clear echo ------------------------------------------ echo CentOS7 openssh升级到8.5p1 echo By feichai echo 生产环境使用前请做好测试 echo ------------------------------------------ sleep 3s clear echo 安装进程开始 3 sleep 1s clear echo 安装进程开始 3 2 sleep 1s clear echo 安装进程开始 3 2 1 sleep 1s clear echo 刷新yum元数据缓存 sleep 2s #rm -rf /etc/yum.repos.d/* #cp CentOS7.repo /etc/yum.repos.d/ yum makecache sleep 1s clear echo 检测安装telnet服务 sleep 1s echo 尝试启动telnet服务 sleep 1s cp /etc/securetty /etc/securetty.bak grep "pts/0" /etc/securetty || echo 'pts/0' >> /etc/securetty grep "pts/1" /etc/securetty || echo 'pts/1' >> /etc/securetty systemctl restart telnet.socket && systemctl restart xinetd ps -ef |grep xinetd | egrep -v grep > /dev/null if [ $? -eq 0 ] then echo 检测到telnet服务已启动…… systemctl enable telnet.socket systemctl enable xinetd sleep 2s else echo 未检测到telnet服务,开始安装服务…… sleep 2s yum -y install xinetd telnet-server sleep 2s clear echo 安装telnet服务结束,启动服务…… systemctl restart telnet.socket && systemctl restart xinetd systemctl enable telnet.socket systemctl enable xinetd sleep 1s fi clear echo 关闭SElinux及防火墙并禁用…… sleep 2s setenforce 0 sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config cat /etc/selinux/config systemctl stop firewalld.service systemctl disable firewalld.service sleep 2s clear echo 安装程序依赖包…… sleep 2s yum -y install gcc gcc-c++ make pam pam-devel openssl-devel pcre-devel perl zlib-devel sleep 1s clear echo 停止并卸载原有ssh sleep 2s systemctl stop sshd cp -r /etc/ssh /etc/ssh.old cp /etc/init.d/ssh /etc/init.d/ssh.old rpm -qa | grep openssh sleep 1s rpm -e `rpm -qa | grep openssh` --nodeps rpm -qa | grep openssh sleep 1s clear echo 安装zlib sleep 2s tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure --prefix=/usr/local/zlib && make && make install ls -l /usr/local/zlib cd .. sleep 1s clear echo 配置zlib grep "/usr/local/zlib/lib" /etc/ld.so.conf.d/zlib.conf || echo '/usr/local/zlib/lib' >> /etc/ld.so.conf.d/zlib.conf ldconfig -v sleep 1s clear echo 安装openssl sleep 2s mv -f /usr/bin/openssl /usr/bin/openssl.old mv -f /usr/include/openssl /usr/include/openssl.old mv -f /usr/lib64/openssl /usr/lib64/openssl.old rm -rf /usr/local/ssl tar -zxvf openssl-1.1.1k.tar.gz cd openssl-1.1.1k ./config --prefix=/usr/local/ssl --openssldir=/usr/local/ssl shared zlib && make && make install cd .. sleep 1s clear echo 配置openssl sleep 2s ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl ln -s /usr/local/ssl/include/openssl /usr/include/openssl ln -s /usr/local/lib64/libssl.so.1.1 /usr/lib64/libssl.so.1.1 ln -s /usr/local/lib64/libcrypto.so.1.1 /usr/lib64/libcrypto.so.1.1 grep "/usr/local/ssl/lib" /etc/ld.so.conf.d/ssl.conf || echo '/usr/local/ssl/lib' >> /etc/ld.so.conf.d/ssl.conf grep "/usr/local/lib" /etc/ld.so.conf.d/ssl.conf || echo '/usr/local/lib' >> /etc/ld.so.conf.d/ssl.conf ldconfig -v openssl version -a sleep 1s clear echo 安装openssh sleep 2s rm -rf /etc/ssh tar -zxvf openssh-8.5p1.tar.gz cd openssh-8.5p1 ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-openssl-includes=/usr/local/ssl/include --with-ssl-dir=/usr/local/ssl --with-zlib --with-md5-passwords make sleep 1s chmod 600 /etc/ssh/ssh_host_rsa_key chmod 600 /etc/ssh/ssh_host_ecdsa_key chmod 600 /etc/ssh/ssh_host_ed25519_key make install cd .. sleep 1s clear echo 配置openssh sleep 2s echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config echo "PermitRootLogin yes" >> /etc/ssh/sshd_config echo 'Banner /etc/issue' >> /etc/ssh/sshd_config cp -p openssh-8.5p1/contrib/redhat/sshd.init /etc/init.d/sshd chmod +x /etc/init.d/sshd chmod 600 /etc/ssh/ssh_host_rsa_key chmod 600 /etc/ssh/ssh_host_ecdsa_key chmod 600 /etc/ssh/ssh_host_ed25519_key chkconfig --add sshd chkconfig sshd on systemctl restart sshd sleep 1s clear systemctl status sshd if [ $? -eq 0 ] then clear echo SSH安装并运行成功,开始关闭并禁用telnet sleep 1s systemctl stop telnet.socket && systemctl stop xinetd systemctl disable telnet.socket && systemctl disable xinetd sleep 1s echo 升级完成,安装ssh2扩展支持 sleep 1s yum install libssh2 -y clear echo 安装进程结束 sleep 2s else echo SSH未成功安装或配置,安装进程即将退出,请检查…… sleep 2s fi
注意:如果脚本执行完毕后,ssh进程未正常启动或安装报错,请根据错误信息及时排错。“坑一直有,多踩几次就好了!”
升级完成后SSH运行状态应该显示running,如下图所示:
- 本文固定链接: https://www.fcblog.cn/post/66.html
- 转载请注明: feichai 于 废柴博客 发表
楼主强!一直在找这个,按照步骤,完美升级~~支持一个~
@gg525感谢支持