# 25 个 IPTables 核心规则深度解析与实战配置

> 来源：梦飞idc云平台 | 原文：https://www.mfisp.com/36427.html | 发布：2025-05-29 | 更新：2025-05-29 | 分类：运维技术

作为运维工程师，IPTables 作为 Linux 系统的核心防火墙工具，其规则配置的合理性直接影响服务器安全与网络性能。本文整理 25 个高频场景规则示例，结合深度解析与解决方案，助你构建高效安全的网络防护体系。

 

**一、基础规则配置**

 
 
1. **初始化防火墙** 

 

iptables -F && iptables -X && iptables -Z # 清空所有规则

 

iptables -P INPUT DROP # 设置默认策略为拒绝

 

iptables -P OUTPUT ACCEPT

 

iptables -P FORWARD DROP

 

**解决方案**：初始化规则链并设置严格默认策略，避免配置漏洞。

 
 
1. **允许本地回环通信** 

 

iptables -A INPUT -i lo -j ACCEPT

 

iptables -A OUTPUT -o lo -j ACCEPT

 

**场景**：保障本地进程间通信（如[localhost](https://localhost/)服务调用）。

 
 
1. **阻止特定****IP****访问** 

 

iptables -A INPUT -s 192.168.1.100 -j DROP

 

**解决方案**：快速封禁恶意 IP，结合日志分析工具（如iptables -L -v）定位异常流量。

 

**二、服务访问控制**

 
 
1. **允许****SSH****访问（仅指定网段）** 

 

iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/24 -m state --state NEW,ESTABLISHED -j ACCEPT

 

iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

 

**场景**：限制 SSH 登录权限，仅允许公司内网 IP 访问。

 
 
1. **开放****Web****服务（****HTTP/HTTPS****）** 

 

iptables -A INPUT -p tcp --dport 80 -j ACCEPT

 

iptables -A INPUT -p tcp --dport 443 -j ACCEPT

 

**优化方案**：使用multiport模块合并规则：

 

iptables -A INPUT -p tcp -m multiport --dports 80,443 -j ACCEPT

 
 
1. **允许****DNS****查询** 

 

iptables -A OUTPUT -p udp --dport 53 -j ACCEPT

 

iptables -A INPUT -p udp --sport 53 -j ACCEPT

 

**解决方案**：保障域名解析服务正常运行。

 

**三、网络安全防护**

 
 
1. **防范****DoS****攻击** 

 

iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT

 

**参数解析**：限制每分钟 25 个新连接，突发流量不超过 100 个。

 
 
1. **阻止****ICMP****泛洪** 

 

iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/second -j ACCEPT

 

**场景**：防止 Ping 洪流导致网络阻塞。

 
 
1. **记录丢弃数据包** 

 

iptables -N LOGGING

 

iptables -A INPUT -j LOGGING

 

iptables -A LOGGING -m limit --limit 2/min -j LOG --log-prefix "DROPPED_PACKET: " --log-level 7

 

iptables -A LOGGING -j DROP

 

**解决方案**：通过 syslog 记录丢弃包，便于安全审计。

 

**四、网络地址转换（****NAT****）**

 
 
1. **端口转发（****SSH****映射）** 

 

iptables -t nat -A PREROUTING -p tcp --dport 422 -j DNAT --to-destination 192.168.1.200:22

 

iptables -A FORWARD -p tcp -d 192.168.1.200 --dport 22 -j ACCEPT

 

**场景**：将公网 422 端口映射到内网服务器 22 端口。

 
 
1. **共享上网（****SNAT****）** 

 

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE

 

**解决方案**：内网设备通过防火墙共享公网 IP 访问互联网。

 

**五、高级应用**

 
 
1. **负载均衡（****HTTP****流量分发）** 

 

iptables -t nat -A PREROUTING -p tcp --dport 80 -m nth --counter 0 --every 3 --packet 0 -j DNAT --to-destination 192.168.1.101:80

 

iptables -t nat -A PREROUTING -p tcp --dport 80 -m nth --counter 0 --every 3 --packet 1 -j DNAT --to-destination 192.168.1.102:80

 

**场景**：将 HTTP 流量轮询分发到两台后端服务器。

 
 
1. **基于时间的访问控制** 

 

iptables -A INPUT -p tcp --dport 22 -m time --timestart 08:00 --timestop 18:00 -j ACCEPT

 

**解决方案**：限制 SSH 仅在工作日办公时间可用。

 
 
1. **多网卡策略路由** 

 

iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT # 公网接口开放HTTP

 

iptables -A INPUT -i eth1 -p tcp --dport 8080 -j ACCEPT # 内网接口开放管理端口

 

**场景**：根据流量来源接口实施差异化访问控制。

 

**六、规则管理与优化**

 
 
1. **保存与恢复规则** 

 

iptables-save > /etc/iptables/rules.v4 # 保存规则

 

iptables-restore < /etc/iptables/rules.v4 # 恢复规则

 

**解决方案**：避免重启后规则丢失。

 
 
1. **可视化规则链** 

 

使用iptable_vis工具生成流程图：

 

iptables -L -n --line-numbers | iptable_vis -o iptables.png

 

**工具优势**：直观展示规则链逻辑，快速定位配8置错误。

 

**🔥****解决方案总结**

 

| **场景** | **核心规则示例** | **工具 / 模块** |
|---|---|---|
| 基础防护 | 初始化规则、设置默认策略 | iptables -F, -P |
| 服务访问控制 | 限制 SSH/HTTP 访问源 IP、合并多端口规则 | multiport |
| 安全防护 | DoS 防范、ICMP 限速、日志记录 | limit, LOG |
| NAT 与端口转发 | 端口映射、共享上网 | DNAT, MASQUERADE |
| 高级应用 | 负载均衡、时间控制、多网卡策略 | nth, time |
| 规则管理 | 保存恢复、可视化 | iptables-save, iptable_vis |

---
本页由 梦飞idc云平台（https://www.mfisp.com）提供。站点导航见 https://www.mfisp.com/llms.txt
