Centos7升级openssh到8.8版本
前期准备
如果服务器的网不好,先下载下来包。
地址:https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.8p1.tar.gz
传输到:/usr/local/src 目录下
前期准备
1、查看centos的防火墙状态:
systemctl status firewalld
你可以选择执行以下2个命令关闭防火墙(禁止防火墙开机自启动)
systemctl stop firewalld
systemctl disable firewalld
也可以选择执行以下几个命令开放22端口(ssh的默认端口是22)(23端口是telnet)
firewall-cmd --zone=public --add-port=22/tcp --permanent
firewall-cmd --zone=public --add-port=23/tcp --permanent
firewall-cmd --reload
温馨提示:为了防止后续安装失败导致无法连接到服务器,最好是先开启telnet连接功能,一旦openssh安装失败还可以用telnet连接服务器。
CentOS7开启telnet服务端,配合进行ssh升级
- 查找telnet安装包
前置条件:服务器可访问互联网
安装之前首先查找telnet的安装包
[root@localhost ~]# yum list | grep telnet-server
telnet-server.x86_64 1:0.17-66.el7 updates
[root@localhost ~]# yum list | grep xinetd
xinetd.x86_64 2:2.3.15-14.el7 base
- 执行安装(yum)
使用yum -y install telnet-server.x86_64
命令安装telnet服务器
[root@localhost ~]# yum -y install telnet-server.x86_64
Loaded plugins: fastestmirror
········(过程省略)
Installed:
telnet-server.x86_64 1:0.17-66.el7
Complete!
使用yum -y install xinetd.x86_64
命令安装xinetd守护进程
[root@localhost ~]# yum -y install xinetd.x86_64
Loaded plugins: fastestmirror
········(过程省略)
Installed:
xinetd.x86_64 2:2.3.15-14.el7
Complete!
- 配置并启动
配置开机启动
使用systemctl enable xinetd.service
与systemctl enable telnet.socket
命令将telnet服务设置为开机自动启动。
[root@localhost ~]# systemctl enable xinetd.service
[root@localhost ~]# systemctl enable telnet.socket
Created symlink from /etc/systemd/system/sockets.target.wants/telnet.socket to /usr/lib/systemd/system/telnet.socket.
- 启动服务
使用systemctl start telnet.socket
与systemctl start xinetd
命令启动telnet服务
[root@localhost ~]# systemctl start telnet.socket
[root@localhost ~]# systemctl start xinetd
使用netstat -ntlp
命令查看运行中的端口(如果提示command not found,可运行yum -y install net-tools
命令安装网络工具包。)
[root@localhost ~]# yum -y install net-tools #安装网络工具包
Loaded plugins: fastestmirror
········(过程省略)
Installed:
net-tools.x86_64 0:2.0-0.25.20131004git.el7
Complete!
[root@localhost ~]# netstat -ntlp #查看端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 987/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1146/master
tcp6 0 0 :::22 :::* LISTEN 987/sshd
tcp6 0 0 :::23 :::* LISTEN 1/systemd
tcp6 0 0 ::1:25 :::* LISTEN 1146/master
注意:完成上述步骤后,还不能通过telnet进行登录,因为centos初始安装后默认开启了firewalld防火墙服务,且默认只打开了22端口。还需要进行防火墙配置。
我的到这里就可以登录了,不行的话就看下面
- 配置防火墙规则
通过firewall-cmd --list-all
命令查看防火墙配置
[root@localhost ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
添加23端口,并重启防火墙。配置防火墙有两种方式可选:
- 直接对外开放23端口
- 只对特定地址的终端开启23端口(推荐)
第一种方式比较简单,安全性低,操作如下:
[root@localhost ~]# firewall-cmd --add-port=23/tcp --permanent #永久开启23端口,--permanent 为永久开启,不加此参数重启防火墙后规则不保存
success
[root@localhost ~]# firewall-cmd --reload #重启防火墙
success
[root@localhost ~]# firewall-cmd --list-all #查看规则
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports: 23/tcp
第二种方式安全性高,甚至如果只对某一台终端开启23端口,后续可以不卸载(当然建议还是卸载了吧),操作如下:
[root@localhost ~]# firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.12.1 port protocol=tcp port=23 accept' #--permanent 为永久开启,不加此参数重启防火墙后规则不保存
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all #查看规则
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
······(省略)
rich rules:
rule family="ipv4" source address="192.168.12.1" port port="23" protocol="tcp" accept
其中source address就是需要访问服务器的终端的ip地址。第二种防火墙配置非常实用,在服务器安全中,强烈推荐多实用第二种防火墙配置方式,比如数据库服务器配置只允许特定的应用服务器访问,能大幅提高服务器环境安全。
- 安全配置
此时还无法使用root登录,会提示登录错误。因为默认root无法远程访问。
Kernel 3.10.0-1127.el7.x86_64 on an x86_64
localhost login: root
Password:
Login incorrect
还需要修改/etc/securetty文件
[root@localhost ~]# vim /etc/securetty
在末尾添加以下内容:
pts/0
pts/1
最终内容如下:
console
·······(省略)
xvc0
pts/0
pts/1
~
- 测试验证
在windows上测试通过telnet登录linux
C:\Users\11>telnet 192.168.12.101
Kernel 3.10.0-1127.el7.x86_64 on an x86_64
localhost login:
(输入账户密码)
Last failed login: Wed Mar 30 22:45:11 CST 2022 from ::ffff:192.168.12.1 on pts/0
There was 1 failed login attempt since the last successful login.
Last login: Wed Mar 30 19:04:51 from 192.168.12.1
如果telnet命令不能用的话,需要开启Telnet客户端,需要重启一下电脑。
- 关闭telnet(稍等,我们的正事儿还没办,要升级ssh,先别关telnet连接方式)
关闭telnet很简单,有很多种方式可以关闭。
— 去除防火墙配置
— 停止telnet服务并取消开机启动
— 彻底移除telnet安装包 - 去除防火墙配置
去除防火墙配置根据不容的配置方式,使用不同的命令进行移除。核心实际上就是将添加防火墙配置的命令中的add替换为remove即可。
[root@localhost ~]# firewall-cmd --remove-port=23/tcp --permanent #针对直接对外开放23端口的配置的移除
success
[root@localhost ~]# firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address=192.168.12.1 port protocol=tcp port=23 accept' #针对第二种方式的防火墙配置的移除
success
[root@localhost ~]# firewall-cmd --reload #重启防火墙
success
[root@localhost ~]# firewall-cmd --list-all #查看配置
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: dhcpv6-client ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
- 停止telnet服务并取消开机启动
停止服务与开启服务命令类似,将开启服务的start换为stop即可。
[root@localhost ~]# systemctl stop telnet.socket
[root@localhost ~]# systemctl stop xinetd
取消开机启动原理一样,将enable替换为disable即可。
[root@localhost ~]# systemctl disable xinetd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/xinetd.service.
[root@localhost ~]# systemctl disable telnet.socket
Removed symlink /etc/systemd/system/sockets.target.wants/telnet.socket.
- 彻底移除telnet安装包
移除安装包与安装安装包类似,将install替换为remove即可。(是不是发现了什么新大陆)
[root@localhost ~]# yum -y remove telnet-server.x86_64
Loaded plugins: fastestmirror
········(过程省略)
Installed:
telnet-server.x86_64 1:0.17-66.el7
Complete!
[root@localhost ~]# yum -y remove xinetd.x86_64
Loaded plugins: fastestmirror
········(过程省略)
Installed:
xinetd.x86_64 2:2.3.15-14.el7
Complete!
至此,关于在CentOS7上安装telnet、卸载telnet的方法就完全介绍完了。
openssh7.4升级到8.8
首先确保有wget,如果有可以跳过了
- 检查wget是否安装?执行以下命令检查:
which wget
- 安装wget
sudo yum install wget
- 确认wget是否安装成功?再次运行which wget 命令来确认
正式开始安装
1、查看当前版本
[root@localhost ~]# ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
2、安装依赖
yum install -y gcc make zlib-devel openssl-devel pam-devel
3、卸载openssh
rpm -qa|grep openssh
rpm -e openssh-server --nodeps
rpm -e openssh-clients --nodeps
rpm -e openssh
4、下载openssh8.8
cd /usr/local/src
wget https://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-8.8p1.tar.gz
tar -zxvf openssh-8.8p1.tar.gz
cd openssh-8.8p1
5、备份原来的配置
mv /etc/ssh /etc/ssh.bak
mv /usr/lib/systemd/system/sshd.service /usr/lib/systemd/system/sshd.service.bak
注意第二行不知道为什么没有,返回执行就行了,不会影响后面的流程
6、编译并安装OpenSSH 8.8
./configure --prefix=/usr/ --sysconfdir=/etc/ssh --with-openssl-includes=/usr/local/ssl/include --with-ssl-dir=/usr/local/ssl --with-zlib --with-md5-passwords --with-pam
make && make install
7、替换配置
cd /usr/local/src/openssh-8.8p1
cp -a ./contrib/redhat/sshd.init /etc/init.d/sshd
chmod +x /etc/init.d/sshd
chkconfig --add sshd
chkconfig sshd on
systemctl daemon-reload
systemctl restart sshd
8、配置还原
手动更新/etc/ssh/sshd_config
(最后会有怎么操作,别着急)
9、重启sshd服务
systemctl restart sshd
10、查看版本
[root@localhost openssh-8.8p1]# ssh -V
OpenSSH_8.8p1, OpenSSL 1.0.2k-fips 26 Jan 2017
11、两个配置注意点
1)默认是UsePAM yes,现在变更成#UsePAM no,所以取消注释,然后修改为yes就可以了。
2)默认是#PermitRootLogin yes,现在变更成#PermitRootLogin prohibit-password,
当然这也是为了安全考虑,若要求root直接远程登录可以修改为PermitRootLogin yes就可以了。
配置还原,配置root用户远程登录
使用vi命令编辑/etc/ssh/sshd_config文件,取消Port 22的注释,允许root用户登录,设置为yes,参考如下图:
!!!!别忘记关闭telnet
!!!!别忘记关闭telnet
!!!!别忘记关闭telnet
!!!!别忘记关闭telnet
!!!!别忘记关闭telnet
!!!!别忘记关闭telnet
!!!!别忘记关闭telnet