Loading
0

Ubuntu解决OpenSSH安全漏洞之升级OpenSSH最新版方法

技术小学生微信公众号
腾讯云服务器大促销。
华为服务器
前言:低版本的OpenSSH会存在安全漏洞, CVE-2021-41617 /CVE-2017-15906 网上有很多类似教程,但测试很多升级失败,通过大量查找及测试终于成功,本文介绍在Ubuntu系统中如何升级OpenSSH版本方法来解决安全隐患,
本文在Ubuntu16和Ubuntu18中测试通过,其他Ubuntu版本理论上也可以
同时也支持Centos及RedHat系统,之前也写过一篇Centos升级OpenSSH的方法,可参考:https://blog.tag.gg/showinfo-3-36233-0.html
说明:数据无价,若是生产环境,请在升级前做好数据备份或快照!!!!
升级前准备:
查看下初始OpenSSH版本


升级OpenSSH的过程需要依赖ZLIB、Perl、OpenSSL以及PAM,所以需先将升级包下载到服务器
安装顺序也有讲究,请参考如下步骤操作
zlib-1.2.12.tar.gz,perl-5.16.1.tar.gz,Linux-PAM-1.3.1.tar.xz,openssl-1.1.1q.tar.gz,openssh-8.8p1.tar.gz。
安装包下载(也可以下载到本地电脑在上传到服务器)

wget http://www.zlib.net/zlib-1.2.12.tar.gz
wget http://www.cpan.org/src/5.0/perl-5.16.1.tar.gz
wget https://github.com/linux-pam/linux-pam/releases/download/v1.3.1/Linux-PAM-1.3.1.tar.xz
wget https://www.openssl.org/source/openssl-1.1.1q.tar.gz
wget http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.8p1.tar.gz

若部分下载地址失效,请重新打开官网获取,也可以下载其他版本。
下载后如图:

开始安装:
1、升级或安装Zlib

tar -xzvf zlib-1.2.12.tar.gz
cd zlib-1.2.12
./configure --prefix=/usr/local/zlib
make && make install

若报错:Compiler error reporting is too harsh for ./configure (perhaps remove -Werror).请先执行如下命令后在编译

sudo apt-get install build-essential

2、安装或升级 Perl

tar -xzf perl-5.16.1.tar.gz
cd perl-5.16.1
./Configure -de
make && make install
perl -v  #查看版本

3、安装pam-devel

sudo tar -xf Linux-PAM-1.3.1.tar.xz
cd Linux-PAM-1.3.1
./configure
make && make install

4、安装或升级 OpenSSL

tar -zxvf openssl-1.1.1q.tar.gz
cd openssl-1.1.1q
./config --prefix=/usr/local/ssl -d shared
make && make install
echo '/usr/local/ssl/lib' >> /etc/ld.so.conf
ldconfig -v

5、 安装或升级 OpenSSH

tar -zxvf openssh-8.8p1.tar.gz
cd openssh-8.8p1
sudo ./configure --prefix=/usr/local/openssh --with-zlib=/usr/local/zlib --with-ssl-dir=/usr/local/ssl
sudo make clean && sudo make && sudo make install

6、配置sshd_config文件:

echo 'PermitRootLogin yes' >>/usr/local/openssh/etc/sshd_config
echo 'PubkeyAuthentication yes' >>/usr/local/openssh/etc/sshd_config
echo 'PasswordAuthentication yes' >>/usr/local/openssh/etc/sshd_config

7、备份原来sshd相应配置文件并复制新文件到指定目录

mv /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
cp /usr/local/openssh/etc/sshd_config /etc/ssh/sshd_config
mv /usr/sbin/sshd /usr/sbin/sshd.bak
cp /usr/local/openssh/sbin/sshd /usr/sbin/sshd
mv /usr/bin/ssh /usr/bin/ssh.bak
cp /usr/local/openssh/bin/ssh /usr/bin/ssh
mv /usr/bin/ssh-keygen /usr/bin/ssh-keygen.bak
cp /usr/local/openssh/bin/ssh-keygen /usr/bin/ssh-keygen
mv /etc/ssh/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub.bak
cp /usr/local/openssh/etc/ssh_host_ecdsa_key.pub /etc/ssh/ssh_host_ecdsa_key.pub

8、验证是否升级成功

ssh -V

9、重启下sshd服务即可

sudo /etc/init.d/ssh restart

其他系统可以执行启动sshd

systemctl restart sshd.service

systemctl restart sshd

若启动sshd服务后能正常连接,则说明升级成功了,若没运行业务,也可以重启服务器试下。
放行22端口:

ufw allow 22/tcp

若这篇文章解决了您的问题,请留言评论下吧。
可能出现的报错:
1、错误:configure: error: *** zlib.h missing - please install first or check config.log ***
原因是缺少 zlib ,可以参考第一步骤重新安装zlib

2、报错:bin/openssl: relocation error: bin/openssl: symbol EVP_mdc2 version OPENSSL_1_1_0 not defined in file libcrypto.so.1.1 with link time reference
    解决方案:
        在/etc/ld.so.conf.d/libc.conf 文件中添加一行 /usr/lib
        运行 ldconfig 命令

echo '/usr/lib ' >> /etc/ld.so.conf.d/libc.conf
ldconfig

3、报错:make && make install出现recipe for target ‘install-binPROGRAMS’ failed
原因:权限不足
解决方法是将存放安装包的目录设置为777权限再试下(如下是我的存放目录,请更换为自己的目录

sudo chmod -R 777 /root/blog-tag-gg/

4、运行make命令的时候报错:Makefile:638: recipe for target ‘xxxxxxx’ failed make: *** [xxxxxx] Error 1
    原因:./configure之后,由于运行make时候的权限或者其他原因,导致了make或make install 失败,需要重新操作。
    解决方案:
        执行 make clean 清除上一次生成的可执行文件,再次执行 make 等命令,大概率可以解决
5、报错: configure: error: PAM headers not found
    原因是缺少PAM,或者 pam安装失败
    解决方案:参考上面方法重新安装pam。
技术小学生微信公众号
华为服务器
腾讯云服务器大促销。

声明:站长码字很辛苦啊,转载时请保留本声明及附带文章链接:https://blog.tag.gg/showinfo-3-36235-0.html
亲爱的:若该文章解决了您的问题,可否收藏+评论+分享呢?
上一篇:Ubuntu和Centos下使用Telnet方式替代SSH远程登陆管理服务器
下一篇:将本地Docker镜像发布上传到阿里云仓库方法