CentOS 7.x 及以下自带的 openSSH 版本基本都在7.x 以下。而 openSSH 7.3 以下的版本均存在高危漏洞。所以服务器升级 openSSH 就成了一件必做的事情。
1、查看当前系统信息
1.1、查看当前系统版本
1 2 3 4 5 6 7 8 | [root@localhost src] # cat /etc/redhat-release CentOS Linux release 7.0.1406 (Core) [root@localhost src] # uname -m x86_64 [root@localhost src] # uname -r 3.10.0-693.el7.x86_64 |
1.2、SSL 版本信息
1 2 3 | [root@localhost src] # openssl version OpenSSL 1.0.2k-fips 26 Jan 2017 |
2、更新/安装 zlib
2.1、下载最新版本 Zlib
Zlib 官方网站:http://www.zlib.net/
1 2 | cd /usr/src/ wget http: //www .zlib.net /zlib-1 .2.11. tar .gz |
2.2、编译安装 Zlib
1 2 3 4 5 | tar xzvf zlib-1.2.11. tar .gz cd zlib-1.2.11 . /configure --prefix= /usr/local/zlib make make install |
这样,就把 zlib 编译安装在 /usr/local/zlib
中了。
3、安装 pam-devel
3.1 下载最新版本pam-devel
pam-devel官方下载网站: https://pkgs.org/download/pam-devel
1 2 | cd /usr/src/ wget http: //ftp .altlinux.org /pub/distributions/ALTLinux/Sisyphus/x86_64/RPMS .classic //libpam0-devel-1 .3.1.0.5.955b-alt2.x86_64.rpm |
3.2 rpm 安装 pam-devel
1 | rpm -ivh libpam0-devel-1.3.1.0.5.955b-alt2.x86_64.rpm |
4、安装 OpenSSL
4.1、下载最新版本的 openssl 和 openssl-fips
打开 OpenSSL 的官方网站下载:https://www.openssl.org/source/
1 2 3 | cd /usr/src/ wget https: //www .openssl.org /source/openssl-1 .0.2o. tar .gz wget https: //www .openssl.org /source/openssl-fips-2 .0.16. tar .gz |
4.2、编译安装 openssl-fips
1 2 3 | [root@localhost src] # tar -xzvf openssl-fips-2.0.16.tar.gz [root@localhost src] # cd openssl-fips-2.0.16 [root@localhost openssl-fips-2.0.16] # ./config && make && make install |
默认安装到 /usr/local/ssl/fips-2.0
4.3、编译安装 openssl
1 2 3 | [root@localhost src] # tar -xzvf openssl-1.0.2o.tar.gz [root@localhost openssl-1.0.2o] # cd openssl-1.0.2o [root@localhost openssl-1.0.2o] # ./config --prefix=/usr/ --shared zlib |
或者
1 | [root@localhost openssl-1.0.2o] # ./config --prefix=/usr/ --shared fips |
注意:必须加上–shared,否则编译时会找不到新安装的 openssl 的库而报错
1 2 | [root@localhost openssl-1.0.2o] # make [root@localhost openssl-1.0.2o] # make test |
提示: 这一步很重要!是进行 SSL加密协议的完整测试,如果出现错误就要一定先找出哪里的原因,否则一味继续可能导致最终 SSH 不能使用,后果很严重!
1 | [root@localhost openssl-1.0.2o] # make install |
或者安装升级 openssl 的方式采用如下步骤:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ tar xf openssl-1.0.1o. tar .gz $ cd openssl-1.0.1o $ . /config shared zlib $ make && make install # 备份旧的 openssl $ mv /usr/bin/openssl /usr/bin/openssl .bak $ mv /usr/include/openssl /usr/include/openssl .bak # 软链接 $ ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl $ ln -s /usr/local/ssl/include/openssl /usr/include/openssl $ echo "/usr/local/ssl/lib" >> /etc/ld .so.conf $ ldconfig - v |
4.4、查看是否升级成功
1 2 | $ openssl version OpenSSL 1.0.2o 27 Mar 2018 |
5、安装 OpenSSH
5.1、下载最新软件包源码
官方下载地址 http://www.openssh.com/portable.html#http
1 2 | cd /usr/src wget http: //mirrors .evowise.com /pub/OpenBSD/OpenSSH/portable/openssh-7 .7p1. tar .gz |
5.2、备份当前 openssh
1 | [root@localhost src] # mv /etc/ssh /etc/ssh.old |
5.3、卸载当前 openssh
1 2 3 4 5 6 7 8 9 10 | [root@localhost src] # rpm -qa | grep openssh openssh-clients-6.4p1-8.el7.x86_64 openssh-server-6.4p1-8.el7.x86_64 openssh-6.4p1-8.el7.x86_64 [root@localhost src] # rpm -e --nodeps openssh-clients-6.4p1-8.el7.x86_64 [root@localhost src] # rpm -e --nodeps openssh-server-6.4p1-8.el7.x86_64 [root@localhost src] # rpm -e --nodeps openssh-6.4p1-8.el7.x86_64 [root@localhost src] # rpm -qa | grep openssh |
5.4、安装 openssh
1 2 3 4 5 6 7 | [root@localhost src] # tar -xzvf openssh-7.7p1.tar.gz [root@localhost src] # cd openssh-7.7p1 编译(同时兼容ssh1协议) [root@localhost openssh-7.7p1] # ./configure --prefix=/usr --sysconfdir=/etc/ssh --with-md5-passwords --with-pam --with-zlib --with-ssh1 [root@localhost openssh-7.7p1] # make && mak install |
5.5、安装后环境配置
1 2 3 | [root@localhost openssh-7.7p1] # cp ./contrib/redhat/sshd.init /etc/init.d/sshd [root@localhost openssh-7.7p1] # chmod +x /etc/init.d/sshd [root@localhost openssh-7.7p1] # chkconfig --add sshd |
修改/etc/ssh/sshd_config
文件,添加 PermitRootLogin yes
到文件/etc/ssh/sshd_config
的末尾:
1 2 | $ echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config $ cat /etc/ssh/sshd_config |
5.6、启动 ssh 服务
1 | service sshd start |
5.7、验证是否升级成功
1 2 | $ ssh -V OpenSSH_7.7p1, OpenSSL 1.0.2m 2 Nov 2017 (升级成功) |
注意:
- 记得关闭防火墙:
systemctl stop firewalld
- 记得关闭
SELinux
:
1、临时关闭(不用重启机器)
1 2 | setenforce 0 ##设置SELinux 成为permissive模式 ##setenforce 1 设置SELinux 成为enforcing模式 |
2、修改配置文件需要重启机器
修改 /etc/selinux/config
文件,将SELINUX=enforcing
改为 SELINUX=disabled
5.8、修改 ssh 默认 22 端口
ssh 默认的端口为 22,有很多脚本小子喜欢用一些 hack 工具去扫描,一不小心可能就因为新的漏洞爆出或巧合,给你的机器权限拿走了。我们可以修改默认端口。步骤参考如下。
首先修改配置文件
1 | vi /etc/ssh/sshd_config |
找到#Port 22
一段,这里是标识默认使用22端口,修改为如下:
1 2 | Port 22 Port 50000 |
然后保存退出。50000 为修改后的端口,可自定义一个,一定要记住,否则后续连接不上就麻烦了。
重启 ssh。执行:
1 | /etc/init .d /sshd restart |
此时 SSH 端口将同时工作于 22 和 50000 上
编辑防火墙配置,启用50000端口:
1 2 3 | vi /etc/sysconfig/iptables # 在末尾添加如下内容 -A INPUT -p tcp -m tcp --dport 50000 -j ACCEPT |
再次重启 ssh:
1 | /etc/init .d /iptables restart |
现在请使用ssh工具连接 50000 端口,来测试是否成功。如果连接成功了,则再次编辑 sshd_config
的设置,将里边的 Port 22
那一行注释或删除,然后再重启一下 ssh 即可。
注意:
如果此时 ssh 工具连接提示 找不到匹配的host key算法
而无法连接,可尝试如下方法(删除 ssh_host 前缀的文件,然后重启 ssh 生成新的):
1 2 3 | mkdir /etc/ssh/ssh_host_bak mv /etc/ssh/ssh_host_ * /etc/ssh/ssh_host_bak /etc/init .d /sshd restart |
相关链接
- https://blog.csdn.net/oujuan9812/article/details/80416392
- http://www.zlib.net/
- https://pkgs.org/download/pam-devel
- https://www.openssl.org/source/
编译过程还是挺繁琐的
你好,我在更新zlib得时候,发现安装完之后,还是原先得版本1.2.7.。。。我的系统版本是CentOS7.5
zlib 的默认位置为
/usr/local/zlib
,仔细找找哪里有不同已经升级好了,openssh升级到了7.9,openssl(1.0.2k)和zlib(1.2.7)使用的是系统自带的,升级完之后,系统漏洞扫描还是有错误,CVE-2006-0705,这种漏洞不是只要升级一下ssh的版本就好了吗?