centos7升级ssh9.3+sftp配置
问题原因:
centos7默认的ssh版本为7.X版本,该版本存在漏洞需要更新到ssh9.X版本。
解决方式:
升级ssh9.3,并配置相应的sftp(需要配置sftp。否则无法使用sftp传输文件)
操作步骤:
确定centos版本以及ssh版本
首先了解centos的版本以及openssh的版本
cat /etc/centos-release
[root@8w7mcbn3fm4plaxg /]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
[root@8w7mcbn3fm4plaxg /]# ssh -V
OpenSSH_7.4p1, OpenSSL 1.0.2k-fips 26 Jan 2017
yum进行升级,发现最新版本还是上述7.4版本
[root@8w7mcbn3fm4plaxg /]#yum update -y openssh
进行手动升级
注意:手动升级前,为了避免升级过程中出现的意外导致服务器无法进行连接,建议对重要的内容先进行备份,如果是服务器在主机服务商那里为主机创建快照,防止出现问题
备份配置文件
备份 /etc/pam.d/sshd 文件
[root@8w7mcbn3fm4plaxg /]#cp /etc/pam.d/sshd /etc/pam.d/sshd-bak
[root@8w7mcbn3fm4plaxg /]#ls -l /etc/pam.d/sshd*
安装telnet
升级过程中会卸载旧版本 ssh 导致远程无法连接,所以先安装一个 telnet
[root@8w7mcbn3fm4plaxg /]#rpm -q telnet-server
#检查是否安装了telnet服务端
[root@8w7mcbn3fm4plaxg /]#rpm -q telnet
#检查是否安装了telnet客户端
提示package telnet-server is not installed
表示未安装
[root@8w7mcbn3fm4plaxg /]#yum install telnet-server -y
#安装telnet服务端
[root@8w7mcbn3fm4plaxg /]#yum install telnet -y
#安装telnet客户端
启动telnet
[root@8w7mcbn3fm4plaxg /]#systemctl enable telnet.socket
#设置开机启动该
[root@8w7mcbn3fm4plaxg /]#systemctl start telnet.socket
#打开服务
防火墙开放23端口,使用telnet进行连接
发现连接不了,此时需要允许root登录,默认系统不允许root用户使用telnet远程登陆
[root@8w7mcbn3fm4plaxg /]#echo 'pts/0' >>/etc/securetty
[root@8w7mcbn3fm4plaxg /]#echo 'pts/1' >>/etc/securetty
输入正确的密码还是不能登录,(可能还需要添加下 pts/2 和 pts/3等),查看日志,可以发现提示,各台机器不相同
[root@8w7mcbn3fm4plaxg /]#tail -f /var/log/secure
发现access denied: tty 'pts/13' is not secure !
那么我们需要加pts到13(这台服务器太多了)
echo 'pts/3' >>/etc/securetty
echo 'pts/4' >>/etc/securetty
echo 'pts/5' >>/etc/securetty
echo 'pts/6' >>/etc/securetty
echo 'pts/7' >>/etc/securetty
echo 'pts/8' >>/etc/securetty
echo 'pts/9' >>/etc/securetty
echo 'pts/10' >>/etc/securetty
echo 'pts/11' >>/etc/securetty
echo 'pts/12' >>/etc/securetty
echo 'pts/13' >>/etc/securetty
添加完成后,telnet登录,可以登录,接下来就可以直接使用telnet操作,防止ssh卸载造成连接不上
备份和telnet安装完成后,进行更新ssh
下载更新ssh(telent登录)
执行前,确认telnet外部连接正常
下载升级包:
可在以下链接中查找对应版本
http://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/
http://ftp.openbsd.org/pub/OpenBSD/OpenSSH/portable/
本次更新使用的是openssh-9.3p1.tar.gz
可以使用本地网络下载压缩包后上传到服务器opt
目录,也可以在服务器内执行命令下载,注意在telnet登陆的状态下进行
[root@8w7mcbn3fm4plaxg /]#cd /opt/
[root@8w7mcbn3fm4plaxg /]#wget http://cdn.openbsd.org/pub/OpenBSD/OpenSSH/portable/openssh-9.3p1.tar.gz
下载完成后解压到该目录下
[root@8w7mcbn3fm4plaxg /]#tar -zxvf openssh-9.3p1.tar.gz
[root@8w7mcbn3fm4plaxg /]#cd openssh-9.3p1
安装ssh(telnet中操作)
注意务必确认有备份,确认已配置登录telnet
备份配置文件
[root@8w7mcbn3fm4plaxg /]#cp /etc/ssh/sshd_config sshd_config.backup
[root@8w7mcbn3fm4plaxg /]#cp /etc/pam.d/sshd sshd.backup
卸载旧版本
[root@8w7mcbn3fm4plaxg /]#rpm -e --nodeps `rpm -qa | grep openssh`
安装依赖
[root@8w7mcbn3fm4plaxg /]#yum -y install gcc pam-devel zlib-devel openssl-devel
编译配置
[root@8w7mcbn3fm4plaxg /]#./configure --prefix=/usr --sysconfdir=/etc/ssh --with-zlib --with-md5-passwords --with-pam
编译安装
[root@8w7mcbn3fm4plaxg /]#make && make install
出现以下内容说明安装完成
调整文件权限
[root@8w7mcbn3fm4plaxg /]#chmod 600 /etc/ssh/ssh_host_rsa_key /etc/ssh/ssh_host_ecdsa_key /etc/ssh/ssh_host_ed25519_key
复制配置文件
[root@8w7mcbn3fm4plaxg /]#cp -a contrib/redhat/sshd.init /etc/init.d/sshd
[root@8w7mcbn3fm4plaxg /]#chmod u+x /etc/init.d/sshd
还原配置文件
[root@8w7mcbn3fm4plaxg /]#mv sshd.backup /etc/pam.d/sshd
[root@8w7mcbn3fm4plaxg /]#mv sshd_config.backup /etc/ssh/sshd_config
添加开机启动
[root@8w7mcbn3fm4plaxg /]#chkconfig --add sshd
[root@8w7mcbn3fm4plaxg /]#chkconfig sshd on
重启sshd
#重启
[root@8w7mcbn3fm4plaxg /]#systemctl restart sshd
#查看版本
[root@8w7mcbn3fm4plaxg /]#ssh -V
使用ssh连接
然后回到ssh连接工具
使用ssh连接登录,可能出现,死活登陆不上,此时telnet登录的情况下,只要到/etc/ssh/sshd_config位置,即将文件中的PermitRootLogin yes
配置成功即可(注意前面有#号需要把#号去掉)
修改完配置文件,使用命令重启ssh
[root@8w7mcbn3fm4plaxg /]#systemctl restart sshd
此时ssh更新完成。
配置sftp使用
在使用测试时,发现更新后无法使用sftp,认证失败
此时打开/etc/ssh/sshd_config
配置文件,检索sftp
[root@8w7mcbn3fm4plaxg /]#vim /etc/ssh/sshd_config
在vim中输入/sftp,找到sftp
将这行注释掉,在其下方添加如下配置
Subsystem sftp internal-sftp
添加完成后保存:wq
,重启ssh服务
[root@8w7mcbn3fm4plaxg /]#systemctl restart sshd
重新连接,发现可以链接使用
卸载telnet(选做)
建议卸载,23端口暴露存在隐患
删除telnet启动项
[root@8w7mcbn3fm4plaxg /]#systemctl disable telnet.socket
#删除开机启动
yum remove telnet
[root@8w7mcbn3fm4plaxg /]#yum remove telnet-server -y
#卸载telnet服务端
[root@8w7mcbn3fm4plaxg /]#yum remove telnet -y
#卸载telnet客户端
删除原先设置的/etc/securetty
[root@8w7mcbn3fm4plaxg /]#vi /etc/securetty
终端验证
安装完成后一定要看!
最后注意:要对yum源中的openssh排除更新,否则yum update时就会恢复到7.4,具体做法参照下图
参考链接:
https://blog.csdn.net/wanglong126/article/details/130291188
https://www.cnblogs.com/dream397/p/13086679.html