Centos7与之前的版本最大的不同,在于Centos6和之前的版本使用的iptables,而Centos7版本以及未来以后的版本则默认使用 FirewallD。
鉴于Centos7的趋势化,收集并学习如何在Centos7下更改SSH默认22端口。
FirewallD 是 iptables 的前端控制器,用于实现持久的网络流量规则。它提供命令行和图形界面,在大多数 Linux 发行版的仓库中都有。与直接控制 iptables 相比,使用 FirewallD 有两个主要区别:
更多了解可以去Linux中国网站查看:https://linux.cn/
vi etc/ssh/sshd_config
在增加Port端口1024保存之后
systemctl restart sshd
如果看不太懂这里,可以看我这篇文章:防止暴力破解,请更换SSH默认端口 ## 增加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
添加新端口1024
semanage port -a -t ssh_port_t -p tcp 1024
检测是否成功
semanage port -l | grep ssh
当返回值出现1024和22即为成功。 ## 配置防火墙FirewallD 首先检测防火墙是否已经启用,启用返回值runing,反之,为not running
firewall-cmd --state
若没有启用,需要启用
systemctl start firewalld
systemctl enable firewalld
若已经启用,则进行下一步
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=1024/tcp
防火墙重载
firewall-cmd --reload
查看已暴露端口
firewall-cmd --permanent --list-port
firewall-cmd --zone=public --list-all
systemctl restart sshd.service
之后用Putty、Xshell之类的软件换成1024端口登录,看能否成功登录。 ## 禁用22端口 首先,删除ssh运行端口
vi 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
若移除,则成功。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有