centos

centos iptables端口如何关闭

小樊
62
2025-03-13 10:08:32
栏目: 智能运维

在 CentOS 系统中,使用 iptables 命令可以管理防火墙规则,包括关闭特定端口。以下是一些常用的命令示例,帮助你关闭特定端口:

关闭特定端口

假设你想关闭 TCP 端口 8080,可以使用以下命令:

sudo iptables -A INPUT -p tcp --dport 8080 -j DROP

这条命令的意思是:

保存规则

在 CentOS 7 及更高版本中,iptables 规则在重启后不会自动保存。你可以使用 firewall-cmd 命令来保存规则:

sudo firewall-cmd --runtime-to-permanent

关闭所有端口(仅限测试)

如果你想关闭所有端口以进行测试,可以使用以下命令:

sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT

这条命令的意思是:

恢复默认规则

如果你想恢复默认规则,可以使用以下命令:

sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
sudo iptables -t mangle -F
sudo iptables -t mangle -X
sudo iptables -P INPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -P OUTPUT ACCEPT

这条命令的意思是:

使用 firewalld

CentOS 7 及更高版本推荐使用 firewalld 来管理防火墙规则。你可以使用以下命令来关闭特定端口:

sudo firewall-cmd --permanent --zone=public --remove-port=8080/tcp
sudo firewall-cmd --reload

这条命令的意思是:

希望这些命令能帮助你成功关闭 CentOS 系统中的特定端口。

0
看了该问题的人还看了