linux

Linux Trigger:如何管理防火墙规则

小樊
51
2025-07-16 13:42:14
栏目: 智能运维

在Linux系统中,管理防火墙规则通常涉及到使用iptables或firewalld这样的工具。以下是关于如何使用这些工具来管理防火墙规则的基本指南:

iptables

iptables是Linux系统上常用的命令行工具,用于配置内核防火墙(netfilter)的规则。

查看规则

sudo iptables -L -n -v

添加规则

# 允许来自特定IP的TCP连接
sudo iptables -A INPUT -p tcp -s 192.168.1.100 --dport 80 -j ACCEPT

# 允许所有出站连接
sudo iptables -P OUTPUT ACCEPT

# 拒绝所有来自特定IP的连接
sudo iptables -A INPUT -s 192.168.1.100 -j DROP

删除规则

# 删除特定规则
sudo iptables -D INPUT -p tcp -s 192.168.1.100 --dport 80 -j ACCEPT

# 清空所有规则
sudo iptables -F

保存规则

# 保存规则到文件
sudo iptables-save > /etc/iptables/rules.v4

# 从文件加载规则
sudo iptables-restore < /etc/iptables/rules.v4

firewalld

firewalld是另一个流行的防火墙管理工具,它提供了一个动态管理防火墙的方式,支持区域和服务的概念。

查看状态

sudo firewall-cmd --state

查看活动区域

sudo firewall-cmd --get-active-zones

查看特定区域的详细信息

sudo firewall-cmd --zone=public --list-all

添加规则

# 允许特定服务
sudo firewall-cmd --zone=public --add-service=http --permanent

# 允许特定端口
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent

# 重新加载配置以应用更改
sudo firewall-cmd --reload

删除规则

# 删除特定服务
sudo firewall-cmd --zone=public --remove-service=http --permanent

# 删除特定端口
sudo firewall-cmd --zone=public --remove-port=8080/tcp --permanent

# 重新加载配置以应用更改
sudo firewall-cmd --reload

注意事项

根据你的Linux发行版和需求,你可能需要使用iptablesfirewalld中的一个来管理防火墙规则。有些现代的Linux发行版可能默认使用firewalld,而一些较旧的版本可能使用iptables

0
看了该问题的人还看了