CentOS 禁用端口指南
在CentOS系统中,禁用不必要的端口可以增强系统的安全性,防止恶意攻击,本文将详细介绍如何在CentOS中禁用端口,并提供一些实用的技巧。

CentOS禁用端口的方法
使用iptables
iptables是Linux系统中常用的防火墙工具,可以用来设置端口过滤规则,以下是如何使用iptables禁用端口的步骤:
(1)确保iptables服务正在运行:
systemctl status iptables
(2)创建一个新的规则来禁用指定端口:
iptables -A INPUT -p tcp --dport 端口号 -j DROP
禁用80端口:
iptables -A INPUT -p tcp --dport 80 -j DROP
(3)保存iptables规则:
service iptables save
(4)重启iptables服务:
systemctl restart iptables
使用firewalld

firewalld是CentOS 7及以上版本中推荐的防火墙工具,以下是如何使用firewalld禁用端口的步骤:
(1)确保firewalld服务正在运行:
systemctl status firewalld
(2)禁用指定端口:
firewall-cmd --permanent --zone=public --add-port=端口号/tcp
禁用80端口:
firewall-cmd --permanent --zone=public --add-port=80/tcp
(3)重启firewalld服务:
systemctl restart firewalld
注意事项
- 禁用端口前,请确保该端口不是系统正常运行所必需的。
- 禁用端口后,相关服务可能会受到影响,请提前做好备份和测试。
- 禁用端口后,可以通过以下命令查看已禁用的端口:
iptables -L
或
firewall-cmd --get-active-zones
FAQs
Q1:如何查看CentOS系统中已禁用的端口?

A1:可以使用以下命令查看iptables中已禁用的端口:
iptables -L
或
使用以下命令查看firewalld中已禁用的端口:
firewall-cmd --get-active-zones
Q2:如何永久禁用端口,即使重启系统也不会被恢复?
A2:在iptables中,使用-A选项添加规则是临时禁用端口,要永久禁用端口,可以使用-I选项替换规则。
iptables -I INPUT -p tcp --dport 端口号 -j DROP
在firewalld中,使用--permanent选项可以永久禁用端口。
firewall-cmd --permanent --zone=public --add-port=端口号/tcp
