iptables 用法

来源:互联网 发布:iphone 贵吗 知乎 编辑:程序博客网 时间:2024/06/15 03:01
iptables用于设置、维护和检查Linux内核中IP包过滤规则的表。

# 目标(targets)
防火墙规则指定数据包的标准和目标。
(1)ACCEPT:意味着让数据包(packets)通过
(2)DROP:意味着数据包在这一层丢掉
(3)QUEUE:意味着将包传送到用户层
(4)RETURN:意味着停止遍历此链,并在前一个调用链的下一条规则中恢复。

# 表(tables)
目前有三个独立的表,哪个表在什么时候出现依赖于内核配置和哪个模块被加载。
-t table :如果内核配置了自动加载模块,但不在该表中,则尝试加载相应的模块。
(1)filter是默认的表,包含三个内建的chains:
INPUT(对于指定到本地套接字的包)
FORWARD(对于通过盒子路由的包)
OUTPUT(对于本地产生的数据包)
(2)nat 遇到创建新连接的数据包时,请查阅此表。包含三个内建的chains:
PREROUTING(改变包当它们一进入)
OUTPUT(改变本地产生的包在routing之前)
POSTROUTING(改变包当它们出去的时候)
(3)mangle此表专门用于数据包的更改
2.4.18以后的版本,
INPUT(对于进入盒子本身的包)
FORWARD(用于改变通过盒子路由的包)
POSTROUTING(用于改变数据包在它们出去的时候)
2.4.17以及之前版本,
PREROUTING(用于改变在routing之前进入的包)
OUTPUT(用于改变本地产生的包在routing之前)
(4)raw
This table is used mainly for configuring exemptions from connection tracking in combination with the NOTRACK target. It registers at the netfilter hooks with higher priority and is thus called before ip_conntrack, or any other IP tables. It provides the following built-in chains: PREROUTING (for packets arriving via any network interface) OUTPUT (for packets generated by local processes)


iptables [-t table] -[AD] chain rule-specification [options]             // append, delete
iptables [-t table] -I chain [rulenum] rule-specification [options]  // insert
iptables [-t table] -R chain rulenum rule-specification [options]    // replace
iptables [-t table] -D chain rulenum [options]                              // Delete one or more rules from the selected chain
iptables [-t table] -[LFZ] [chain] [options]                                   // list, flush, zero
        iptables -t nat -n -L          // -n Numeric output. IP addresses and port numbers will be printed in numeric format.
        iptables -L -v                  // -v Verbose output. This option makes the list command show the interface name, the rule options (if any), and the TOS masks.
iptables [-t table] -N chain                                                         // Create a new user-defined chain by the given name.
iptables [-t table] -X [chain]                                                      // Delete the optional user-defined chain specified.
iptables [-t table] -P chain target [options]                                // Set the policy for the chain to the given target.
iptables [-t table] -E old-chain-name new-chain-name               // Rename the user specified chain to the user supplied name.

参考文献
点击打开链接

原创粉丝点击