Centos7修改22端口
Centos7与之前的版本最大的不同,在于Centos6和之前的版本使用的iptables,而Centos7版本以及未来以后的版本则默认使用 FirewallD。
鉴于Centos7的趋势化,收集并学习如何在Centos7下更改SSH默认22端口。
FirewallD 简介
FirewallD 是 iptables 的前端控制器,用于实现持久的网络流量规则。它提供命令行和图形界面,在大多数 Linux 发行版的仓库中都有。与直接控制 iptables 相比,使用 FirewallD 有两个主要区别:
- FirewallD 使用区域和服务而不是链式规则。
- 它动态管理规则集,允许更新规则而不破坏现有会话和连接。
更多了解可以去Linux中国网站查看:https://linux.cn/
修改shhd_config
vi etc/ssh/sshd_config
在增加Port端口5654保存之后
Port 22 增加 5654端口 Port 5654 systemctl restart sshd
增加SElinux端口
在Centos7系统更改shhd_config的过程中,你会看到这段注释:
# If you want to change the port on a SELinux system, you have to tell # SELinux about this change. # semanage port -a -t ssh_port_t -p tcp #PORTNUMBER
所以,下一步就是告诉SElinux这步操作,我们需要用到semanage
首先,我们安装下semanage
yum provides semanage
yum -y install policycoreutils-python
添加新端口5654
semanage port -a -t ssh_port_t -p tcp 5654
检测是否成功
semanage port -l | grep ssh
当返回值出现5654和22即为成功。
配置防火墙FirewallD
首先检测防火墙是否已经启用,启用返回值runing,反之,为not running
firewall-cmd --state
若没有启用,需要启用
systemctl start firewalld
systemctl enable firewalld
若已经启用,则进行下一步
查看防火墙的默认、活跃区域(zones)
firewall-cmd --get-default-zone firewall-cmd --get-active-zones
看两条命令的返回值是否含有public,有则为正确。
端口永久开放
为了防止出错,22端口一同开放
与临时开放的区别在于多了permanent
firewall-cmd --permanent --zone=public --add-port=22/tcp firewall-cmd --permanent --zone=public --add-port=5654/tcp
防火墙重载
firewall-cmd --reload
查看已暴露端口
firewall-cmd --permanent --list-port firewall-cmd --zone=public --list-all
重启SSH
systemctl restart sshd.service
之后用Putty、Xshell之类的软件换成1024端口登录,看能否成功登录。
禁用22端口
首先,删除ssh运行端口
vim etc/ssh/sshd_config
在Port 22前加#成为#Port 22后保存退出即可
在把防火墙中的22端口移除
firewall-cmd --permanent --zone=public --remove-port=22/tcp
重启并查看是否移除
firewall-cmd --reload firewall-cmd --permanent --list-port