Linux防火墙设置需先确认使用iptables或firewalld(二者不兼容),再按各自规则管理:iptables清空、设默认策略、放行基础及业务端口并持久化;firewalld则通过zone、service/port、rich rule配置并重载生效。

执行以下命令快速判断:
查看 firewalld 状态:systemctl is-active firewalld(返回 active 表示启用)
查看 iptables 服务状态:systemctl is-active iptables(常见于 CentOS 6/7 早期;RHEL 8+ 默认已弃用)
检查规则是否加载:iptables -L -n 或 firewall-cmd --list-all
⚠️ 注意:若 firewalld 正在运行,iptables 命令可能显示空规则或仅含默认 ACCEPT,因为 firewalld 会接管 netfilter 规则并隐藏底层细节。
适用于 CentOS 6/7、Debian/Ubuntu(未启用 ufw 或 nftables 的旧环境)。
iptables -F(清空 filter 表所有链)iptables -X(删除用户自定义链)iptables -t nat -F(如需清空地址转换规则)iptables -P INPUT DROPiptables -P FORWARD DROPiptables -P OUTPUT ACCEPT
iptables -A INPUT -i lo -j ACCEPTiptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPTiptables -A INPUT -p icmp -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPTiptables -A INPUT -p tcp --dport 80 -j ACCEPT,iptables -A INPUT -p tcp --dport 443 -j ACCEPTservice iptables save 或 iptables-save > /etc/sysconfig/iptablesiptables-save > /etc/iptables/rules.v4,再配置开机加载适用于 RHEL 8+/CentOS 8+、Fedora、多数新版发行版,默认启用且更易维护。
systemctl start firewalldsystemctl enable firewalld
firewall-cmd --set-default-zone=public(常用)firewall-cmd --get-active-zones 查看当前生效 zonefirewall-cmd --permanent --add-port=8080/tcpfirewall-cmd --permanent --add-service=http--reload,永久生效必须加 --permanent
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" reject'
firewall-cmd --reload(立即生效,不中断已有连接)规则顺序决定匹配结果——iptables 从上到下逐条匹配,一旦命中即停止;firewalld 的 rich rule 也遵循类似逻辑。
不要在远程 SSH 会话中直接设 iptables -P INPUT DROP 后忘记放行 22 端口,否则会锁死连接。
firewalld 不是 iptables 的图形界面,它是独立服务,底层可能使用 nftables(RHEL 8+ 默认),但对用户屏蔽了复杂性。
生产环境建议先测试规则,再保存/重载;可搭配 tcpdump 或 journalctl -u firewalld 排查异常。
基本上就这些。
以上就是Linux防火墙如何设置_iptables与firewalld规则管理详细步骤讲解【指导】的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号