Linux防火墙配置文件路径取决于所使用的防火墙工具,不同发行版的默认工具及路径有所差异,以下是常见场景的详细说明:
iptables是Linux内核层的包过滤工具,其配置文件路径随发行版版本变化:
/etc/sysconfig/iptables
,保存IPv4规则;IPv6规则默认保存在/etc/sysconfig/ip6tables
。/etc/sysconfig/iptables
(需手动安装iptables-services
包),但部分系统可能将规则保存在/etc/iptables/rules.v4
(IPv4)和/etc/iptables/rules.v6
(IPv6),尤其是Debian/Ubuntu系发行版。service iptables save
(CentOS 6)或iptables-save > /etc/sysconfig/iptables
(CentOS 7+)保存配置,否则重启后失效。firewalld是CentOS 7+、Fedora等系统的默认防火墙,采用“区域+服务”模型,配置文件分为系统默认配置(勿修改)和用户自定义配置(可修改):
/usr/lib/firewalld/
,包含预定义的zone(如public.xml
、home.xml
)、service(如http.xml
、ssh.xml
)等文件,修改此目录下的文件会被后续更新覆盖。/etc/firewalld/
,用户修改的配置(如新增端口、修改zone规则)会保存至此,优先级高于系统默认配置。常见文件包括:
/etc/firewalld/zones/
(如public.xml
,对应默认的public区域);/etc/firewalld/services/
(如自定义的myapp.xml
);/etc/firewalld/firewalld.conf
(全局设置,如默认区域、日志级别)。firewall-cmd --reload
重新加载(不中断现有连接),或firewall-cmd --complete-reload
完全重载(中断连接)。ufw(Uncomplicated Firewall)是Ubuntu的默认防火墙工具,旨在简化iptables的使用,配置文件位于/etc/ufw/
目录:
/etc/ufw/ufw.conf
(设置默认策略、日志级别等全局选项);/etc/ufw/before.rules
(在主要规则前加载,用于特殊配置,如NAT);/etc/ufw/user.rules
(IPv4用户规则)、/etc/ufw/user6.rules
(IPv6用户规则)。ufw enable
启用防火墙,ufw reload
重新加载规则。若不确定系统使用的防火墙工具或配置文件位置,可通过以下命令辅助判断:
sudo find /etc -name 'iptables*'
或 sudo find /etc -name 'sysconfig/iptables'
;sudo find /etc -name 'ufw.conf'
;sudo find /etc -name 'firewalld.conf'
或 ls /etc/firewalld/zones/
(查看区域配置)。注:不同发行版、不同版本的Linux系统可能存在差异,建议通过systemctl status firewalld
(查看firewalld状态)、ufw status
(查看ufw状态)或iptables -L
(查看iptables规则)确认当前使用的防火墙工具,再对应查找配置文件。