ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Linux 裸奔不可取!手把手给服务器穿上防弹衣

2026/8/5 8:28:04 拓冰建站 浏览量
Linux 裸奔不可取!手把手给服务器穿上防弹衣 文章目录一、防火墙核心概述二、防火墙分类体系1. 按保护范围划分2. 按实现方式划分3. 按网络协议划分网络层防火墙包过滤防火墙应用层防火墙代理服务器/WAF三、Linux 内核网络过滤子系统1. Netfilter 子系统2. Nftables 子系统四、静态防火墙与动态防火墙1. 静态防火墙2. 动态防火墙五、Firewalld 防火墙深度解析1. Firewalld 核心介绍2. Firewalld 区域Zone体系常用区域及功能说明数据包与区域匹配规则3. Firewalld 规则优先级4. Firewalld 配置方式六、Firewalld 实战配置指南1. 核心概念运行时与永久配置2. 区域Zone管理3. 源地址Source管理4. 服务Service管理5. 端口Port管理6. 地址伪装Masquerade管理7. 端口转发Forward\-port管理8. ICMP 管控Ping 限制9. 富规则Rich Rule管理10. 直接规则Direct Rule管理11. panic 模式紧急防护七、总结一、防火墙核心概述防火墙FireWall是部署在网络或主机边缘的安全组件核心功能是基于预设规则对进出网络/主机的数据包进行检测与管控默认遵循“关闭所有访问、仅开放授权策略”的安全原则通常将外网可访问的主机部署在 DMZ非军事区中实现网络隔离与安全防护。二、防火墙分类体系1. 按保护范围划分主机防火墙仅服务于单台主机管控该主机的所有网络流量网络防火墙覆盖防火墙一侧的局域网实现整个网段的流量管控2. 按实现方式划分硬件防火墙基于专用硬件实现防护功能如华为、华三、天融信等商用设备软件防火墙运行于通用硬件平台的应用软件如 iptables、firewalld3. 按网络协议划分网络层防火墙包过滤防火墙工作在 OSI 模型第四层通过访问控制列表ACL过滤数据包依据源地址、目的地址、端口号、协议状态等维度判断是否允许数据包通过。优点对用户透明处理速度快维护成本低缺点无法检测应用层数据如病毒、恶意脚本等应用层防火墙代理服务器/WAF工作在 OSI 模型第七层可作为代理网关实现深度防护核心防护能力包括异常检测识别不符合 HTTP 标准的请求拦截非法访问输入验证防范网页篡改、信息泄露、木马植入等攻击漏洞补丁快速屏蔽已知 Web 安全漏洞降低攻击风险状态管理追踪用户访问行为识别暴力破解等异常操作其他防护隐藏表单域、抗入侵规避、响应监视等优点提供应用层深度防护针对性强缺点处理速度相对较慢资源消耗较高三、Linux 内核网络过滤子系统1. Netfilter 子系统Netfilter 是 Linux 内核内置的强大网络过滤子系统工作在内核态可对进出系统的数据包进行检查、修改、丢弃或路由调度。用户通过以下工具配置规则管控数据包流向iptables过滤 IPv4 协议数据包是 Linux 网络管控的核心工具ip6tables过滤 IPv6 协议数据包功能与 iptables 一致arptables过滤 ARP 协议数据包防范 ARP 欺骗与恶意攻击ebtables过滤数据链路层数据包多用于桥模式场景如 VLAN 管控[rootcentos7 ~]# ls -1 /sbin/*tables-rwxr-xr-x1root root5987211月62016/sbin/arptables -rwxr-xr-x.1root root70164月112018/sbin/ebtables lrwxrwxrwx.1root root138月115:45 /sbin/ip6tables -xtables-multi lrwxrwxrwx.1root root138月115:45 /sbin/iptables -xtables-multi2. Nftables 子系统Nftables 是 Netfilter 的增强版保留原有架构的同时优化了性能与易用性核心优势包括数据包处理速度更快规则集更新效率更高通过单一 nft 工具管理所有协议消除多工具混用的冲突问题四、静态防火墙与动态防火墙1. 静态防火墙规则修改后需重新加载全部规则即使仅调整一条规则也会导致网络连接中断如 iptables。执行service iptables reload会将规则保存至配置文件并重启服务影响业务连续性。2. 动态防火墙仅更新修改的规则无需重载全部规则不影响现有网络连接如 firewalld。该模式兼顾了规则灵活性与业务稳定性更适配生产环境。五、Firewalld 防火墙深度解析1. Firewalld 核心介绍Firewalld 是 Red Hat 公司开发的防火墙软件默认后端为 Nftables在 CentOS 7 及以上版本中取代 iptables 成为默认防火墙。其核心特点是支持动态加载规则同时提供命令行与图形界面配置工具底层仍依赖 Netfilter 实现数据包管控仅优化了规则管理方式。注意iptables 与 firewalld 不可同时混用建议关闭其中一个避免规则冲突。2. Firewalld 区域Zone体系Firewalld 将网络流量划分为多个区域每个区域对应一套独立的过滤规则简化了复杂场景的管控难度。默认区域为 publiclo 回环接口默认映射至 trusted 区域。常用区域及功能说明trusted信任区域允许所有传入流量无任何限制public公共区域仅允许 SSH、dhcpv6-client 等预定义服务的流量其余拒绝默认区域external外部区域仅允许 SSH 流量默认开启 IPv4 地址伪装适用于路由器场景home家庭区域允许 SSH、mdn3、samba-client 等服务流量其余拒绝internal内部区域默认与 home 区域规则一致work工作区域允许 SSH、dhcpv6-client 等服务流量其余拒绝dmz隔离区域仅允许 SSH 流量其余拒绝用于部署外网可访问的服务block限制区域拒绝所有传入流量返回 ICMP 错误响应drop丢弃区域丢弃所有传入流量不返回任何响应数据包与区域匹配规则优先匹配源地址对应的区域规则未匹配源地址时匹配传入网络接口对应的区域规则均未匹配时使用默认区域public规则3. Firewalld 规则优先级同一区域内规则优先级一致匹配顺序为端口转发/伪装规则 → 允许规则 → 拒绝规则富规则优先级高于区域内其他规则所有规则均不匹配时默认拒绝数据包trusted 区域除外。4. Firewalld 配置方式直接编辑配置文件路径为 /etc/firewalld/自定义配置、/usr/lib/firewalld/默认配置不建议修改命令行工具firewall-cmd核心配置工具图形工具firewall-config可视化配置适合新手提示我们不建议大家通过修改 /etc/firewalld/ 目录配置防火墙。六、Firewalld 实战配置指南1. 核心概念运行时与永久配置runtime运行时默认状态配置立即生效重启后失效permanent永久需添加 --permanent 选项配置写入文件需 reload 后生效建议配置时同时执行两条命令永久配置临时生效避免重启后规则丢失。2. 区域Zone管理# 查看所有区域[rootcentos7 ~]# firewall-cmd --get-zonesblock dmz drop external home internal public trusted work# 查看激活的区域绑定接口/源地址[rootcentos7 ~]# firewall-cmd --get-active-zonespublic interfaces: ens32# 查看默认区域[rootcentos7 ~]# firewall-cmd --get-default-zonepublic# 设置默认区域永久生效[rootcentos7 ~]# firewall-cmd --set-default-zonetrusted[rootcentos7 ~]# firewall-cmd --set-default-zonepublic # 恢复默认# 查看区域规则[rootcentos7 ~]# firewall-cmd --list-all # 默认区域[rootcentos7 ~]# firewall-cmd --list-all --zonehome # 指定区域3. 源地址Source管理# 将指定网段绑定到 home 区域[rootcentos7 ~]# firewall-cmd --add-source10.1.1.0/24 --zonehome# 查看区域内源地址列表[rootcentos7 ~]# firewall-cmd --list-sources --zonehome# 查看源地址所属区域[rootcentos7 ~]# firewall-cmd --get-zone-of-source10.1.1.0/24# 变更源地址所属区域[rootcentos7 ~]# firewall-cmd --change-source10.1.1.0/24 --zonepublic# 删除区域内源地址[rootcentos7 ~]# firewall-cmd --remove-source10.1.1.0/24 --zonepublic4. 服务Service管理# 安装 Nginx 服务示例[rootcentos7 ~]# yum install -y nginx systemctl start nginx# 查看预定义服务列表[rootcentos7 ~]# firewall-cmd --get-services# 放行 HTTP 服务临时生效[rootcentos7 ~]# firewall-cmd --add-servicehttp# 查看放行服务列表[rootcentos7 ~]# firewall-cmd --list-services# 验证服务是否放行[rootcentos7 ~]# firewall-cmd --query-servicehttp# 永久放行 HTTP 服务[rootcentos7 ~]# firewall-cmd --add-servicehttp --permanent# 删除放行服务[rootcentos7 ~]# firewall-cmd --remove-servicehttp --permanentfirewall-cmd 命令行还可以定义新的服务。常用选项如下--permanent--new-serviceservice Add a new permanent and empty service.--permanent--delete-serviceservice Delete an existing permanent service.--permanent--serviceservice --set-descriptiondescription Set new description toservice--permanent--serviceservice --get-description Print descriptionforservice--permanent--serviceservice --set-shortdescription Set short description toservice--permanent--serviceservice --get-short Print short descriptionforservice--permanent--serviceservice --add-portportid[-portid]/protocol Add a new port to the permanent service.--permanent--serviceservice --remove-portportid[-portid]/protocol Remove a port from the permanent service.--permanent--serviceservice --get-ports List ports added to the permanent service.--permanent--serviceservice --add-protocolprotocol Add a new protocol to the permanent service.--permanent--serviceservice --remove-protocolprotocol Remove a protocol from the permanent service.--permanent--serviceservice --get-protocols List protocols added to the permanent service.--permanent--serviceservice --add-source-portportid[-portid]/protocol Add a newsourceport to the permanent service.--permanent--serviceservice --remove-source-portportid[-portid]/protocol Remove asourceport from the permanent service.--permanent--serviceservice --get-source-ports Listsourceports added to the permanent service.5. 端口Port管理# 放行指定端口TCP 协议[rootcentos7 ~]# firewall-cmd --add-port5900/tcp# 查看放行端口列表[rootcentos7 ~]# firewall-cmd --list-ports# 验证端口是否放行[rootcentos7 ~]# firewall-cmd --query-port5900/tcp# 永久放行端口[rootcentos7 ~]# firewall-cmd --add-port5900/tcp --permanent# 删除放行端口[rootcentos7 ~]# firewall-cmd --remove-port5900/tcp --permanent6. 地址伪装Masquerade管理用于实现内网主机通过防火墙访问公网需先开启 masquerade 功能。# 查看是否开启地址伪装[rootcentos7 ~]# firewall-cmd --query-masquerade# 开启地址伪装临时生效[rootcentos7 ~]# firewall-cmd --add-masquerade# 永久开启地址伪装[rootcentos7 ~]# firewall-cmd --add-masquerade --permanent# 关闭地址伪装[rootcentos7 ~]# firewall-cmd --remove-masquerade --permanent7. 端口转发Forward-port管理需先开启对应区域的 masquerade 功能语法--add-forward-portport源端口:proto协议:toport目标端口:toaddr目标地址。# 开启地址伪装[rootcentos7 ~]# firewall-cmd --add-masquerade --permanent# 本机8000端口转发至本机80端口[rootcentos7 ~]# firewall-cmd --add-forward-portport8000:prototcp:toport80 --permanent# 本机1022端口转发至10.1.1.11的22端口[rootcentos7 ~]# firewall-cmd --add-forward-portport1022:prototcp:toport22:toaddr10.1.1.11 --permanent# 查看端口转发规则[rootcentos7 ~]# firewall-cmd --list-forward-ports# 删除端口转发规则[rootcentos7 ~]# firewall-cmd --remove-forward-portport8000:prototcp:toport80 --permanent8. ICMP 管控Ping 限制# 禁止所有主机 ping 本机临时生效[rootcentos7 ~]# firewall-cmd --add-icmp-block echo-request# 永久禁止 ping[rootcentos7 ~]# firewall-cmd --add-icmp-block echo-request --permanent# 查看 ICMP 禁止列表[rootcentos7 ~]# firewall-cmd --list-icmp-blocks# 恢复 ping 功能[rootcentos7 ~]# firewall-cmd --remove-icmp-block echo-request --permanent# 启用 ICMP 反向管控仅放行指定主机 ping[rootcentos7 ~]# firewall-cmd --add-icmp-block-inversion --permanent9. 富规则Rich Rule管理富规则可实现精细化管控如限制 IP 访问、速率限制、日志审计等优先级高于区域规则。# 示例1禁止所有主机 ping 本机[rootcentos7 ~]# firewall-cmd --add-rich-rulerule protocol valueicmp drop --permanent# 示例2仅允许10.1.8.10 ping 本机[rootcentos7 ~]# firewall-cmd --add-rich-rulerule familyipv4 source address10.1.8.10 icmp-type nameecho-request accept --permanent# 示例3限制10.1.8.0/24网段访问 HTTP 服务每秒最多3次记录日志[rootcentos7 ~]# firewall-cmd --add-rich-rule rule familyipv4 source address10.1.8.0/24 service namehttp log prefixhttp-error levelerror limit value3/s accept --permanent# 查看富规则列表[rootcentos7 ~]# firewall-cmd --list-rich-rules# 删除富规则[rootcentos7 ~]# firewall-cmd --remove-rich-rulerule protocol valueicmp drop --permanent10. 直接规则Direct Rule管理直接规则优先级最高语法类似 iptables用于补充 firewalld 规则适用于复杂场景。# 示例将192.168.0.0/24列入黑名单限制并发连接[rootcentos7 ~]# firewall-cmd --direct --permanent --add-chain ipv4 raw blacklist[rootcentos7 ~]# firewall-cmd --direct --permanent --add-rule ipv4 raw PREROUTING 0 -s 192.168.0.0/24 -j blacklist[rootcentos7 ~]# firewall-cmd --direct --permanent --add-rule ipv4 raw blacklist 0 -m limit --limit 1/min -j LOG --log-prefix blacklisted [rootcentos7 ~]# firewall-cmd --direct --permanent --add-rule ipv4 raw blacklist 1 -j DROP[rootcentos7 ~]# firewall-cmd --reload# 清理直接规则[rootcentos7 ~]# firewall-cmd --permanent --direct --remove-rules ipv4 raw blacklist[rootcentos7 ~]# firewall-cmd --direct --remove-chain ipv4 raw blacklist[rootcentos7 ~]# firewall-cmd --reload11. panic 模式紧急防护遭遇黑客攻击时可启用 panic 模式丢弃所有进出流量快速切断网络连接。# 启用 panic 模式[rootcentos7 ~]# firewall-cmd --panic-on# 查看 panic 模式状态[rootcentos7 ~]# firewall-cmd --query-panic# 关闭 panic 模式[rootcentos7 ~]# firewall-cmd --panic-off七、总结防火墙是 Linux 网络安全的核心防线Firewalld 凭借动态规则加载、区域化管控、精细化配置等优势成为 CentOS/RHEL 系列发行版的首选防火墙工具。本文从防火墙基础原理出发全面覆盖 Firewalld 的区域管理、规则配置、高级功能及实战场景涵盖企业级部署的核心要点。核心运维建议遵循“最小权限”原则配置规则优先使用富规则实现精细化管控重要规则务必添加 --permanent 实现持久化避免重启后失效调试规则时可使用 --timeout 临时生效降低业务风险定期检查防火墙规则及时清理无效配置保障网络安全与性能。