目录

iptables学习

概述

如果你是运维或者搞 Kubernetes 的工程师,一定要掌握好 iptables。iptables 的命令多且复杂,如果不想每次都打开 Google 一顿查,建议好好看一下 iptables 的 help 或者 man,这样查的时候只需要对着 help 或者 man 查就好了,要想成为 iptables 的高手,一定要可以脱离 Google 工作,下面的内容摘自 iptables 1.4.21 的操作和帮助手册。

help信息

help 里主要是用法,没有关于 iptables 原理的解释,如果仅仅是忘记命令的选项,看 help 信息就够了。

man信息

man 手册里的信息就非常多了,足够大家理解 iptables 了,基本上手册读完了,也就理解了 iptables 的原理了,所以非常建议大家读一读,还有一些 iptables 的扩展,也需要阅读一下 iptables-apply, iptables-save, iptables-restore, iptables-extensions。

iptables的组成

iptables 会定义 table 也就是表,每个表会包含预设的以及自定义的 chains 也就是链,每条链会有一些 rule 也就是规则,符合规则的包就会执行 target。所以可以从上到下将 iptables 理解成四个部分组成。

  1. table
  2. chains
  3. target
  4. rule

tables

下面是 iptables 内的表,从表的角度看每个类型的表有什么样的链。

  1. filter: 默认的表,包含的链是input/forward/output
  2. nat: 包含的链是prerouting/output/postrouting
  3. mangle: 包含的链是input/forward/postrouting
  4. raw: 包含的链是prerouting/output
  5. security: 包含的链是input/output/forward

chains

下面是 iptables 内链的类型,从链的角度看看每个类型的链会出现在什么样的表中。

  1. input: filter/mangle/security
  2. output: filter/nat/mangle/raw/security
  3. forward: filter/mangle/security
  4. prerouting: nat/mangle/raw
  5. postrouting: nat/mangle

下面是具体表中链的定义。

filter 表的链。

  1. input: for packets destined to local sockets
  2. forward: for packets being routed through the box
  3. output: for locally-generated packets

nat 表的链。

  1. prerouting: for altering packets as soon as they come in
  2. output: for altering locally-generated packets before routing
  3. postrouting: for altering packets as they are about to go out

mangle 表的链。

  1. input: for packets coming into the box itself
  2. forward: for altering packets being routed through the box
  3. postrouting: for altering packets as they are about to go out
  4. output: for altering locally-generated packets before routing
  5. prerouting: for altering incoming packets before routing

raw 表的链。

  1. prerouting: for packets arriving via any network interface
  2. output: for packets generated by local processes

security 表的链。

  1. input: for packets coming into the box itself
  2. output: for altering locally-generated packets before routing
  3. forward: for altering packets being routed through the box

target

下面是 target 的分类。

  1. ACCEPT
  2. DROP
  3. RETURN

选项

下面是一些常见选项的含义。

  1. -F: 清除所有链规则,比较危险

基本语法

通过 -t 来指定表是基本操作,因为你需要告诉 iptables 操作哪张表。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
iptables [-t table] {-A|-C|-D} chain rule-specification
-A: append
-C: check
-D: delete

iptables [-t table] -I chain [rulenum] rule-specification
-I: insert

iptables [-t table] -R chain rulenum rule-specification
-R: replace

iptables [-t table] -D chain rulenum
-D: delete

iptables [-t table] -S [chain [rulenum]]
-S: --list-rules

iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
-F: flush
-L: list
-Z: zero

iptables [-t table] -N chain
-N: --new-chain

iptables [-t table] -X [chain]
-X: --delete-chain

iptables [-t table] -P chain target
-P: --policy

iptables [-t table] -E old-chain-name new-chain-name
-E: --rename-chain

rule-specification = [matches...] [target]

match = -m matchname [per-match-options]

target = -j targetname [per-target-options]

查看所有的规则

默认情况下,iptables 命令会显示 filter 表的所有规则,这是最常用的表,用于基本的网络过滤。你可以使用以下命令查看所有的规则:

1
iptables -L

这将显示 filter 表的所有链(INPUT,FORWARD,OUTPUT)及其规则。

查看特定表的规则

如果你想查看其他表的规则,例如 nat 表或 mangle 表,你可以使用 -t 选项指定表名:

1
2
iptables -t nat -L
iptables -t mangle -L

查看特定链的规则

如果你只想查看特定链的规则,你可以在 -L 选项后面指定链名:

1
2
3
iptables -L INPUT
iptables -L FORWARD
iptables -L OUTPUT

请注意,以上命令显示的规则可能不包含包计数和字节计数,这些计数对于分析和调试网络流量可能很有用。如果你想查看这些计数,你可以添加 -v 选项:

1
iptables -v -L

另外,以上命令显示的规则可能是主机名,而不是 IP 地址,这可能会导致命令执行缓慢(由于需要进行 DNS 查询)。如果你想查看 IP 地址,而不是主机名,你可以添加 -n 选项:

1
iptables -n -L

这些命令应该可以满足大多数查看 iptables 规则的需求。如果你需要进行更复杂的操作,你可能需要查阅 iptables 的详细文档或者教程。

如何构建rule-specification

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
-p: 指定协议,可以写tcp/udp等
-s: source
-d: destination
-m: match
-j: jump可以指定rule的target
-g: goto指定下一个chain
-i: interface指定网络接口
-o: out interface
-f: fragment
-c: set counter初始化rule的包统计器

iptables-apply

有些版本竟然找不到这个命令。

iptables-save

通过执行 man 可以输出文档。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
       iptables-save and ip6tables-save are used to dump the contents of IP or IPv6 Table in easily parseable format to STDOUT. Use I/O-redirection provided by your shell to write to a file.

       -M,--modprobe modprobe_program
              Specify the path to the modprobe program. By default, iptables-save will inspect /proc/sys/kernel/modprobe  to determine the executable's path.

       -c, --counters
              include the current values of all packet and byte counters in the output

       -t, --table tablename
              restrict output to only one table. If the kernel is configured with automatic module loading, an attempt will be made to load the appropriate module for that table if it is not already there. If not specified, output includes all available tables.

iptables-save 命令的输出是一种特殊的格式,它包含了 iptables 规则集的完整表示。以下是一些关键点帮助你理解这个输出。

iptables 的规则被组织成几个不同的表,每个表有其特定的用途。例如,filter 表用于基本的网络过滤,nat 表用于网络地址转换。每个表的开始和结束分别由*表名和 COMMIT 标记。

1
2
3
4
5
*filter
...规则...
...规则...
...规则...
COMMIT

每个表包含几个预定义的链(PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING等),以及可能的自定义链。链是规则的集合,这些规则按顺序执行。

1
2
3
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]

在上面的例子中,INPUT, FORWARD, OUTPUT 是链的名称,ACCEPT 是默认的策略(如果没有规则匹配,就使用这个策略),[0:0] 是该链的包计数和字节计数。

规则

规则定义了如何处理匹配特定条件的数据包。规则的格式可以有很多种,但一般包括一个或多个匹配条件,以及一个决定如何处理数据包的目标。

1
-A INPUT -i lo -j ACCEPT

在上面的例子中,-A INPUT 表示在 INPUT 链中添加一个新的规则,-i lo 是一个匹配条件(匹配所有从lo接口来的数据包),-j ACCEPT 是目标(对于匹配的数据包,接受它们)。

这只是一种非常基础的解释,实际上 iptables-save 的输出可以包含更多复杂的元素,包括更复杂的匹配条件、目标,以及自定义的链和表等。你可能需要查阅更详细的文档或者教程来完全理解它们。

iptables-restore

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
DESCRIPTION
       iptables-restore  and ip6tables-restore are used to restore IP and IPv6 Tables from data specified on STDIN. Use I/O redirection provided by your shell to read from a file

       -c, --counters
              restore the values of all packet and byte counters

       -h, --help
              Print a short option summary.

       -n, --noflush
              don't flush the previous contents of the table. If not specified, both commands flush (delete) all previous contents of  the  respective table.

       -t, --test
              Only parse and construct the ruleset, but do not commit it.

       -v, --verbose
              Print additional debug info during ruleset processing.

       -V, --version
              Print the program version number.

       -w, --wait [seconds]
              Wait for the xtables lock. To prevent multiple instances of the program from running concurrently, an attempt will be made to obtain an exclusive lock at launch.  By default, the program will exit if the lock cannot be obtained. This option will make the program wait (indefinitely or for optional seconds) until the exclusive lock can be obtained.

       -W, --wait-interval microseconds
              Interval  to  wait  per each iteration.  When running latency sensitive applications, waiting for the xtables lock for extended durations may not be acceptable. This option will make each iteration take the amount of time specified. The default interval is  1  second. This option only works with -w.

       -M, --modprobe modprobe_program
              Specify  the  path to the modprobe program. By default, iptables-restore will inspect /proc/sys/kernel/modprobe to determine the executable's path.

       -T, --table name
              Restore only the named table even if the input stream contains other ones.

iptables-extensions

iptables-extensions 这个手册里的东西就博大精深了,内容很多。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
7    MASQUERADE  all  --  0.0.0.0/0            0.0.0.0/0            vaddr 10.255.250.21
8    MASQUERADE  all  --  0.0.0.0/0            0.0.0.0/0            vaddr 10.255.50.229
9    MASQUERADE  all  --  0.0.0.0/0            0.0.0.0/0            vaddr 10.255.110.226
10   MASQUERADE  all  --  0.0.0.0/0            0.0.0.0/0            vaddr 10.255.161.85
11   MASQUERADE  all  --  0.0.0.0/0            0.0.0.0/0            vaddr 10.255.238.113

ipvs
Match IPVS connection properties.

       [!] --ipvs
              packet belongs to an IPVS connection

       Any of the following options implies --ipvs (even negated)

       [!] --vproto protocol
              VIP protocol to match; by number or name, e.g. "tcp"

       [!] --vaddr address[/mask]
              VIP address to match

       [!] --vport port
              VIP port to match; by number or name, e.g. "http"

       --vdir {ORIGINAL|REPLY}
              flow direction of packet

       [!] --vmethod {GATE|IPIP|MASQ}
              IPVS forwarding method used

       [!] --vportctl port
              VIP port of the controlling connection to match, e.g. 21 for FTP

参考资料

  1. iptables man手册
警告
本文最后更新于 2017年2月1日,文中内容可能已过时,请谨慎参考。