Linux教程网

一定要了解的iptables实用技巧

基本原理介绍

常见的几种数据包的流向:

  1. 进入本机某进程的报文
  2. 本机某进程发出的报文
  3. 由本机转发出去的报文

四张表,优先级顺序 raw ➡️ mangle ➡️ nat ➡️ filter

规则增删改查

一、查看规则

最简单的命令,直接查看 filter 规则。

1
iptables -t filter -nvL

可省略 -t 选项,默认为 filter 表,等价于 iptables -nvL

查看其它表规则内容

1
2
3
iptables -t raw -nvL
iptables -t mangle -nvL
iptables -t nat -nvL

二、增加规则

在 INPUT 链最后增加一个规则

1
iptables -t filter -A INPUT -s 192.168.0.3 -j DROP

在 INPUT 链开头增加一个规则

1
iptables -t filter -I INPUT -s 192.168.0.2 -j DROP

在 INPUT 链的第 2 个位置插入一个规则

1
iptables -t filter -I INPUT 2 -s 192.168.0.2 -j DROP

规则按顺序匹配,只要匹配到就不在往下走了。

iptables -F INPUT

三、删除规则

删除 INPUT 第 3 条规则

1
iptables -t filter -D INPUT 3

删除 INPUT 匹配的规则

1
iptables -t filter -D INPUT -s 192.168.0.2 -j DROP

删除 INPUT 链上所有规则

1
iptables -t filter -F INPUT

四、修改规则

修改 INPUT 链第 1 条规则

1
iptables -t filter -R INPUT 1 -s 192.168.0.2 -j REJECT

设置 FORWARD 默认为 ACCEPT

1
iptables -t filter -P FORWARD ACCEPT

五、保存恢复规则

默认显示所有设置的规则,使用重定向可以保存所有规则

1
2
3
iptables-save

iptables-save > /etc/sysconfig/iptables

加载恢复所有规则(覆盖)

1
iptables-restore < /etc/sysconfig/iptables

常用匹配条件

匹配目标地址

1
iptables -t filter -I INPUT -d 192.168.0.3 -j DROP

可指定匹配多个 IP 地址,多个地址使用逗号分隔

1
iptables -t filter -I INPUT -s 192.168.0.2,192.168.0.3 -j DROP

可指定匹配网段

1
2
3
iptables -t filter -I INPUT -s 192.168.1.0/24 -j DROP
# 取反
iptables -t filter -I INPUT ! -s 192.168.1.0/24 -j DROP

匹配指定地址范围

1
iptables -t filter -I INPUT -m iprange --src-range 192.168.0.2-192.168.0.20 -j DROP

按照协议匹配

1
2
iptables -t filter -I INPUT -p tcp -j REJECT
iptables -t filter -I INPUT -p icmp -j REJECT

指定匹配网口

1
2
iptables -t filter -I INPUT -i eth4 -p icmp -j DROP
iptables -t filter -I OUTPUT -o eth4 -p icmp -j DROP

按照四层端口匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# `--dport` 目的端口 `--sport` 源端口
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 -j REJECT

# 指定端口范围
iptables -t filter -I INPUT -p tcp -m tcp --sport 22:25 -j REJECT

# 匹配 0-22 所有端口
iptables -t filter -I INPUT -p tcp -m tcp --sport :25 -j REJECT

# 匹配 90 以后所有端口
iptables -t filter -I INPUT -p tcp -m tcp --sport 90: -j REJECT

# 匹配多个非连续端口
iptables -t filter -I INPUT -p tcp -m tcp -m multiport --dports 22,35:80 -j DROP

高级匹配条件

按照报文字符串来匹配

1
iptables -t filter -I INPUT -m string --algo bm --string "Hello" -j REJECT

根据时间段匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
iptables -t filter -I INPUT -p tcp --dport 80 -m time \
--timestart 09:00:00 --timestop 18:00:00 -j REJECT

# 周六日不能访问
iptables -t filter -I INPUT -p tcp --dport 80 -m time \
--weekdays 6,7 -j REJECT

# 每月 22,23 日不能访问
iptables -t filter -I INPUT -p tcp --dport 80 -m time \
--monthdays 22,23 -j REJECT

iptables -t filter -I INPUT -p tcp --dport 80 -m time \
--datestart 2017-12-24 --datestop 2017-12-30 -j REJECT

限制请求连接

1
2
3
4
5
# 每个IP只能2个SSH连接
iptables -t filter -I INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT

# 报文速率限制,每分钟只放行10个icmp包
iptables -t filter -I INPUT -p icmp -m limit --limit 10/minute -j ACCEPT

请求匹配 TCP 报文

1
2
3
4
5
6
7
8
9
# 第 1 次握手包丢弃
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 \
--tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN -j REJECT
# 等价于下边的命令
iptables -t filter -I INPUT -p tcp -m tcp --dport 22 --syn -j REJECT

# 第 2 次握手包丢弃
iptables -t filter -I OUTPUT -p tcp -m tcp --sport 22 \
--tcp-flags SYN,ACK,FIN,RST,URG,PSH SYN,ACK -j REJECT

匹配 ICMP 报文,禁止别人 ping 我们的设备

1
2
iptables -t filter -I INPUT -p icmp --icmp-type 8/0 -j REJECT
iptables -t filter -I INPUT -p icmp --icmp-type "echo-request" -j REJECT

自定义链

当默认链中的规则非常多时,不方便我们管理。自定义链并不能直接使用,而是需要被默认链引用才能够使用。

1
2
3
iptables -t filter -N WEB
# 目标端口为 80 的流量交给 WEB 链来处理
iptables -t filter -I INPUT -p tcp --dport 80 -j WEB

修改自定义链名称

1
2
# 修改自定义链名称,引用自定义链处的名称会自动发生改变
iptables -t filter -E WEB HTTP

删除自定义链

1
iptables -t filter -X HTTP

删除自定义链,要满足 2 个条件,一是自定义链没有被任何默认链引用,二是自定义链中没有任何规则。

NAT功能

设置 SNAT 功能(内网主动向外部网络请求)

1
2
3
4
5
# SNAT 指定源地址
iptables -t nat -A POSTROUTING -s 10.1.0.0/16 -j SNAT --to-source 192.168.1.146

# MASQUERADE 自动根据网卡选择源地址
iptables -t nat -I POSTROUTING -s 10.1.0.0/16 -o eth0 -j MASQUERADE

设置 DNAT 功能(外网主动向内部网络请求)

1
2
iptables -t nat -I PREROUTING -d 192.168.1.146 -p tcp --dport 3389 -j DNAT --to-destination 10.1.0.6:3389
iptables -t nat -A POSTROUTING -d 10.1.0.0/16 -j SNAT --to-source 10.1.0.5

将本机的 80 端口映射到本机的 8080 端口上

1
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080

REDIRECT规则只能定义在PREROUTING链或者OUTPUT链中

常用选项参数

  • -t:指定表名称
  • -L:列出规则
  • -v:显示详细信息
  • --line-number:显示行号
  • -X:删除自定义链
  • -I:链的首部插入规则;
  • -A:链的尾部追加规则;

处理动作

  • ACCEPT:接受
  • DROP:丢弃
  • REJECT:拒绝
    • –reject-with:拒绝选项
      • icmp-net-unreachable、icmp-host-unreachable、icmp-port-unreachable(默认)
      • icmp-proto-unreachable、icmp-net-prohibited、icmp-host-pro-hibited、icmp-admin-prohibited
  • SNAT
    • 内网主动访问外部网络
    • 隐藏内网主机 IP、共享公网 IP
  • MASQUERADE:特殊的 SNAT,能自动选择 SNAT 地址
  • DNAT
    • 外网主动访问内部网络
    • 隐藏内网主机 IP
  • REDIRECT
  • LOG
    • --log-devel
      • emerg、alert、crit、error、warning、notice、info、debug
    • --log_prefix

专题:

本文发表于 2025-06-02,最后修改于 2025-10-06。

本站永久域名「 golinuxblog.com 」,也可搜索「 Linux教程网 」找到我。


上一篇 « Linux系统性能分析利器lsof

推荐阅读

Big Image