centos7修改ssh端口

ssh配置文件和iptables中端口开放配置调整时,原先的先不要移除,显式的同时打开22和22345端口,待配置完成确认22345端口访问正常后再逐个移除22端口的配置,防止配置过程中出现差错导致服务器访问不了。
一、SELinux配置修改
1.先检查SELinux状态,如果已关闭则无需相关修改
[[email protected] ~]# sestatus
SELinux status: enabled
SELinuxfs mount: /sys/fs/selinux
SELinux root directory: /etc/selinux
Loaded policy name: targeted
Current mode: enforcing
Mode from config file: enforcing
Policy MLS status: enabled
Policy deny_unknown status: allowed
Max kernel policy version: 28
2.semanage命令安装(如果已安装,则无需此步骤)
[[email protected] ~]# yum -y install semanage
[[email protected] ~]# yum provides semanage
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.cn99.com
* updates: mirrors.cn99.com
base/7/x86_64/filelists_db
| 6.9 MB 00:00:11
extras/7/x86_64/filelists_db
| 369 kB 00:00:00
updates/7/x86_64/filelists_db
| 636 kB 00:00:00
policycoreutils-python-2.5-22.el7.x86_64 : SELinux policy core python utilities
源 :base
匹配来源:
文件名 :/usr/sbin/semanage
[[email protected] ~]# yum -y install policycoreutils-python-2.5-22.el7.x86_64
3.SELinux中SSH端口开放查询和配置
[[email protected] ~]# semanage port -l|grep ssh ssh_port_t tcp 22
[[email protected] ~]# semanage port -a -t ssh_port_t -p tcp 22345
[[email protected] ~]# semanage port -l|grep ssh ssh_port_t tcp 22345, 22
4、SSH端口配置修改
修改ssh配置文件sshd_config
[[email protected] ~]# vi /etc/ssh/sshd_config
Port 22
Port 22345
5、防火墙放行
[[email protected] ~]# firewall-cmd –zone=public –add-port=22345/tcp –permanent
[[email protected]~]# firewall-cmd –reload
6、查询当前 ssh 服务端口:
[[email protected]~]# semanage port -l | grep ssh
ssh_port_t       tcp      22
向 SELinux 中添加 ssh 端口:
[[email protected]~]# semanage port -a -t ssh_port_t -p tcp 22345
[[email protected]~]# semanage port -l | grep ssh
ssh_port_t       tcp      22,22345
 重启 ssh 服务:
[[email protected]~]# systemctl restart sshd.service
测试成功后,把22端口注释掉即可

原创文章,作者:赛福,如若转载,请注明出处:https://www.safecdn.cn/linux/2020/03/centos7-ssh-port.html

发表评论

邮箱地址不会被公开。