From Fedora Project Wiki

在这篇文档里里我们将主要举例介绍三种编辑 iptables 规则的方式:

  • CLI: iptables 命令行接口和系统配置文件 /etc/sysconfig/iptables.
  • TUI 基于文本的接口: setup 或 system-config-firewall-tui
  • GUI: system-config-firewall

需要注意的是本篇文章只是说明了如何编辑已有 iptables 规则,并不包含开始的创建规则链。

CLI(命令行接口)

对 iptables 的更改即时生效

下面的过程可以在防火墙运行的情况下改变防火墙策略。


Stop (medium size).png
警告
不当的 iptables 规则可能会破坏 Fedora 系统的网络连接。

阅读 man 手册中关于 iptables 的部分(man iptables)来获取深入的解释和高级复杂的规则实例。

Important.png
需要超级用户权限
这些命令必须要有超级用户权限才能执行,请使用 sudo 或者 su 来获取超级用户权限。


列出现有规则

使用

iptables -L

命令可以查看当前正在使用的 iptables 规则。

Note.png
端口数值
使用 -L 选项列出的端口数值是其对应的服务名称而不是端口号。如果要看端口号,请包含 -n 选项。
iptables -L -n
Note.png
查看计数器
使用 -L 选项列出的规则并不包含匹配的计数。使用 -v 选项来显示匹配计数。
iptables -L -v

允许已经建立及其相关的连接、icmp 请求、所有本地数据和 ssh 通讯的 iptables 规则例子:


[root@server ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

注意规则是按顺序匹配的,只要有匹配的规则便不会再往下接着检查。因此,举个例子来说,如果一条规则拒绝 ssh 连接,其后又有一条规则允许 ssh,那么就会应用拒绝 ssh 的规则,后面允许 ssh 连接的规则就不会应用,也就无法起到作用。

追加规则

下面在指定的链的最后面添加一条规则:

[root@server ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@server ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

注意 INPUT 链的最后一行。现在该链中已经有 5 条规则了。

删除规则

要删除一条规则就必须要知道该规则在链中的位置。下面的例子删除之前添加的那一条规则(在 INPUT 链的第5条):

[root@server ~]# iptables -D INPUT 5
[root@server ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

插入规则

在顶部(第一条)新建一条规则:

[root@server ~]# iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
[root@server ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:http
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

链后面的数值就是新规则所要插入的位置,即新规则将要在的位置。比如说插入一条规则为第三条,则指定3,之前的第三条及其后面的规则都会被依次下移。

替换规则

也可以指定规则替换链中存在的规则。

在之前的例子中,第一条规则允许任意地址到 http 端口(80)的连接。下面替换该规则,限制只允许地址在 192.168.0.0/24 范围内且到标准 http 端口(80)的连接:

[root@server ~]# iptables -R INPUT 1 -p tcp -s 192.168.0.0/24 --dport 80 -j ACCEPT
[root@server ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.0.0/24       anywhere             tcp dpt:http
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

清空规则

使用--flush-F选项清空 iptables 规则:

iptables -F <chain>

Specifying a <chain> is optional; without a chain specification, all chains are flushed.

Example to flush Rules in the OUTPUT chain :

[root@server ~]# iptables -F OUTPUT
Stop (medium size).png
注意链默认策略
要小心链的默认策略。比如,如果 INPUT 的默认策略是 DROP 或者 REJECT,而链中的规则又被清空了,那么所有进来的数据包要么丢弃要么被拒绝了,这样网络通信就被破坏了。

使规则永久生效

用 CLI 命令改变的规则在系统重启后就会丢失。不过 iptables 提供了两个有哦那个的工具: iptables-save and iptables-restore.

  • iptables-save 打印当前 iptables 规则到 stdout。 当然也可以重定向到指定文件:
[root@server ~]# iptables-save > iptables.dump 
[root@server ~]# cat iptables.dump 
# Generated by iptables-save v1.4.12 on Wed Dec  7 20:10:49 2011
*filter
:INPUT DROP [45:2307]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [1571:4260654]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
COMMIT
# Completed on Wed Dec  7 20:10:49 2011
  • iptables-restore : 恢复 iptables-save 转存的规则。
[root@server ~]# iptables-restore < iptables.dump 
[root@server ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination


在停止服务的时候保存当前的规则到文件,然后再启动服务的时候用于恢复:

  • /etc/sysconfig/iptables
    对应 IPv4
  • /etc/sysconfig/ip6tables
    对应 IPv6

如果喜欢可以直接编辑这些文件,然后重启服务就可以应用更改。格式和 iptables CLI 命令相似 If preferred, these files may be editted directly, and iptables service restarted to commit the changes. The format is similar to that of the iptables CLI commands:

# Generated by iptables-save v1.4.12 on Wed Dec  7 20:22:39 2011
*filter <--------------------------------------------------------- 指出下一条规则的所属表
:INPUT DROP [157:36334] <----------------------------------------- 这是 filter 表的三个链,接着是链默认策略
:FORWARD ACCEPT [0:0] <------------------------------------------- 中括号[<packet-counter>:<byte-counter>]间的数值只是用于
:OUTPUT ACCEPT [48876:76493439] <--------------------------------- 调试或者显示信息。不要修改它们。
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT <--------- 一条规则。
-A INPUT -p icmp -j ACCEPT <-------------------------------------- 只要列出需要的 iptables 命令
-A INPUT -i lo -j ACCEPT <---------------------------------------- 选项和参数。
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
COMMIT <---------------------------------------------------------- 需要在每个表的后面,用于提交规则到该表
# Completed on Wed Dec  7 20:22:39 2011

如果需要,可以使用 -Z, --zero 选项重置包计数和大小计数:

iptables -Z <chain> <rule_number>

可以仅仅重置一条规则的计数。如果要知道一条特定规则捕获了多少包,这可能比较有用

TUI(基于文本的接口)

GUI