十年匠心定制 · 商业建站与技术教学双线并行 咨询热线:400-886-1026 service@lmnt.cn
ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Linux命令:firewall

Linux命令:firewall firewall-cmd 命令基本介绍firewall-cmd是firewalld动态防火墙管理工具的命令行接口广泛应用于 CentOS/RHEL 7、Fedora 等发行版。它基于区域Zone和服务Service的概念支持运行时与永久配置的动态管理无需重启防火墙即可生效。相比传统的iptables脚本firewall-cmd提供了更友好、更直观的管理方式是现代 Linux 系统默认的防火墙解决方案资料合集https://pan.quark.cn/s/6fe3007c3e95、https://pan.quark.cn/s/561de99256a5、https://pan.quark.cn/s/985f55b13d94、https://pan.quark.cn/s/d0fb20abd19a核心概念区域Zone区域是网络连接的信任级别不同区域对应不同的规则集区域默认策略说明trusted允许所有完全信任不拦截任何流量public拒绝除指定外的入站默认区域公共网络使用home拒绝除指定外的入站家庭网络信任内部主机internal拒绝除指定外的入站内部网络work拒绝除指定外的入站工作场所网络dmz拒绝除指定外的入站非军事区仅开放选定端口block拒绝所有入站拒绝所有入站仅允许出站drop丢弃所有入站丢弃所有入站包不响应external拒绝除指定外的入站外部网络支持 IPv4 伪装运行时 vs 永久配置运行时Runtime立即生效重启 firewalld 后丢失默认行为永久Permanent写入配置文件需--reload后生效推荐流程先添加永久规则 → reload 使其同时生效语法firewall-cmd [选项] [参数]常用选项通用选项--permanent将规则写入永久配置不会立即生效需 reload--zoneZONE指定操作的区域未指定时使用默认区域--timeoutTIME设置规则生效时长如60s、5m仅运行时有效--state查看 firewalld 运行状态--reload重新加载防火墙配置保留运行时状态--complete-reload完全重载断开现有连接--panic-on/--panic-off开启/关闭恐慌模式拒绝所有流量使用示例一、状态与服务管理1. 查看防火墙状态firewall-cmd--state# 输出running 或 not running2. 启动/停止/开机自启 firewalldsystemctl start firewalld# 启动systemctl stop firewalld# 停止systemctlenablefirewalld# 开机自启systemctl restart firewalld# 重启systemctl status firewalld# 查看详细状态3. 重新加载配置firewall-cmd--reload# 重新加载保留状态信息firewall-cmd --complete-reload# 完全重载断开所有连接二、区域管理4. 查看所有区域firewall-cmd --get-zones# block dmz drop external home internal public trusted work5. 查看默认区域firewall-cmd --get-default-zone# public6. 设置默认区域firewall-cmd --set-default-zonehome7. 查看活动区域及网卡绑定firewall-cmd --get-active-zones# public# interfaces: eth0 eth18. 查看指定区域的完整配置firewall-cmd--zonepublic --list-all9. 将网卡添加/移出到区域firewall-cmd--zonepublic --add-interfaceeth0 firewall-cmd--zonepublic --change-interfaceeth0# 修改所属区域firewall-cmd--zonepublic --remove-interfaceeth0三、服务管理推荐方式10. 查看所有可用服务firewall-cmd --get-services# RH-Satellite-6 amanda-client amanda-k5-client ... http https ...11. 查看当前区域已开放的服务firewall-cmd--zonepublic --list-services# dhcpv6-client ssh12. 开放 HTTP 服务# 永久开放firewall-cmd--permanent--zonepublic --add-servicehttp# 立即生效firewall-cmd--reload13. 批量开放服务firewall-cmd--permanent--zonepublic --add-servicehttp firewall-cmd--permanent--zonepublic --add-servicehttps firewall-cmd--permanent--zonepublic --add-servicedns firewall-cmd--reload14. 移除服务firewall-cmd--permanent--zonepublic --remove-servicehttp firewall-cmd--reload15. 临时开放服务5 分钟后自动失效firewall-cmd--zonepublic --add-serviceftp--timeout5m四、端口管理16. 开放单个端口# 永久开放 TCP 8080firewall-cmd--permanent--zonepublic --add-port8080/tcp firewall-cmd--reload17. 开放端口范围firewall-cmd--permanent--zonepublic --add-port3000-3100/tcp firewall-cmd--permanent--zonepublic --add-port3000-3100/udp firewall-cmd--reload18. 查看已开放端口firewall-cmd--zonepublic --list-ports# 8080/tcp 3000-3100/tcp 3000-3100/udp19. 关闭端口firewall-cmd--permanent--zonepublic --remove-port8080/tcp firewall-cmd--reload五、IP 伪装与转发20. 开启 IP 伪装NAT 转发firewall-cmd--permanent--zonepublic --add-masquerade firewall-cmd--reload21. 端口转发本地# 将 80 端口流量转发到本机 8080firewall-cmd--permanent--zonepublic --add-forward-portport80:prototcp:toport8080firewall-cmd--reload22. 端口转发远程主机# 需先开启 masqueradefirewall-cmd--permanent--zonepublic --add-masquerade# 将 80 转发到 192.168.1.100:8080firewall-cmd--permanent--zonepublic --add-forward-portport80:prototcp:toaddr192.168.1.100:toport8080firewall-cmd--reload23. 查看转发规则firewall-cmd--zonepublic --list-forward-ports六、富规则Rich Rules富规则提供更精细的控制支持源 IP、动作、日志等复杂条件。24. 允许指定 IP 访问指定端口firewall-cmd--permanent--zonepublic\--add-rich-rulerule familyipv4 source address192.168.1.100 port port22 protocoltcp acceptfirewall-cmd--reload25. 拒绝某 IP 段的所有流量firewall-cmd--permanent--zonepublic\--add-rich-rulerule familyipv4 source address10.0.0.0/8 rejectfirewall-cmd--reload26. 限制某 IP 的访问频率防爆破firewall-cmd--permanent--zonepublic\--add-rich-rulerule service namessh limit value3/m acceptfirewall-cmd--reload每分钟最多接受 3 次 SSH 连接。27. 丢弃某 IP 的所有流量firewall-cmd--permanent--zonepublic\--add-rich-rulerule familyipv4 source address1.2.3.4 drop28. 查看所有富规则firewall-cmd--zonepublic --list-rich-rules29. 移除富规则firewall-cmd--permanent--zonepublic\--remove-rich-rulerule familyipv4 source address192.168.1.100 port port22 protocoltcp accept七、直接规则Direct Rules兼容 iptables 语法30. 使用 iptables 语法添加规则firewall-cmd--permanent--direct--add-rule ipv4 filter INPUT0\-s192.168.1.0/24-ptcp--dport3306-jACCEPT firewall-cmd--reload八、ICMP 管理31. 查看 ICMP 类型firewall-cmd --get-icmptypes32. 禁止 pingfirewall-cmd--permanent--zonepublic --add-icmp-blockecho-request firewall-cmd--reload33. 允许特定 ICMP 类型firewall-cmd--permanent--zonepublic --remove-icmp-blockecho-request九、配置文件与备份34. 查看永久配置不依赖 firewalld 运行firewall-cmd--permanent--zonepublic --list-all35. 配置文件位置ls/etc/firewalld/zones/# 区域配置文件ls/etc/firewalld/services/# 自定义服务配置ls/usr/lib/firewalld/services/# 系统默认服务定义36. 自定义服务XML 定义cat/etc/firewalld/services/myapp.xml# ?xml version1.0 encodingutf-8?# service# shortMyApp/short# descriptionMy custom application/description# port protocoltcp port9000/# /service然后即可--add-servicemyapp使用。十、应急操作37. 开启恐慌模式拒绝所有流量紧急情况使用firewall-cmd --panic-on firewall-cmd --query-panic# 查询状态firewall-cmd --panic-off# 关闭恐慌模式实战场景场景 1部署 Web 服务器标准配置# 开放必要服务firewall-cmd--permanent--zonepublic --add-servicehttp firewall-cmd--permanent--zonepublic --add-servicehttps firewall-cmd--permanent--zonepublic --add-servicessh# 仅允许管理 IP 访问 SSH更安全的做法firewall-cmd--permanent--zonepublic --remove-servicessh firewall-cmd--permanent--zonepublic\--add-rich-rulerule familyipv4 source address203.0.113.10 service namessh acceptfirewall-cmd--reload场景 2完整备份与恢复防火墙规则# 备份firewall-cmd--zonepublic --list-allfirewall_backup_$(date%F).txtcp-r/etc/firewalld /etc/firewalld.bak# 恢复cp-r/etc/firewalld.bak/* /etc/firewalld/ firewall-cmd--reload场景 3限制 SSH 防止暴力破解firewall-cmd--permanent--zonepublic\--add-rich-rulerule service namessh limit value3/m acceptfirewall-cmd--permanent--zonepublic\--add-rich-rulerule service namessh limit value3/m log prefixssh_deny levelnotice dropfirewall-cmd--reload注意事项--permanent不会立即生效必须配合--reload才能让永久规则同时应用到运行时。推荐先永久后重载firewall-cmd--permanent--add-servicexxx firewall-cmd--reload--complete-reload会断开连接远程 SSH 操作时慎用可能导致连接中断。区域优先级一个网卡只能属于一个区域若网卡未指定区域则使用默认区域。服务优先于端口能用--add-service就不要手动--add-port服务定义更规范、更易维护。排查问题firewall-cmd--state# 是否运行firewall-cmd--zonepublic --list-all# 查看完整规则journalctl-ufirewalld-f# 查看日志firewall-cmd--permanent--zonepublic --list-all# 查看永久配置与 iptables/nftables 共存firewalld 底层使用 nftablesCentOS 8或 iptablesCentOS 7不建议同时手动修改 iptables 规则避免冲突。panic 模式谨慎使用开启后所有网络流量被拒绝可能锁定 SSH 会话。相关命令iptables传统防火墙规则管理工具底层nftables新一代 Linux 防火墙框架firewalld 后端ufwUbuntu/Debian 系的简化防火墙工具iptables-save/iptables-restoreiptables 规则备份与恢复systemctl管理 firewalld 服务nmcliNetworkManager配合管理网卡所属区域与其他防火墙工具对比特性firewall-cmd (firewalld)iptablesufwnftables适用发行版RHEL/CentOS/Fedora所有 LinuxUbuntu/Debian现代发行版易用性高区域服务低需懂链/表高极简语法中动态重载支持保留连接不支持部分支持支持区域概念是否否否永久/运行时分离是否需脚本否否底层实现iptables/nftablesnetfilteriptablesnetfilter学习曲线中等陡峭平缓陡峭总结firewall-cmd是现代 RHEL 系 Linux 系统的核心防火墙管理工具通过区域服务抽象层大幅简化了防火墙配置。掌握其运行时 vs 永久的双层配置模型、服务优于端口的管理原则、以及富规则实现精细控制即可应对绝大多数服务器安全需求。日常运维建议遵循永久写入 reload 生效的工作流并善用--list-all进行规则验证。
返回列表