iptables介绍

2020-03-22  本文已影响0人  酱油王0901

iptables由三部分组成,tableschainsrules

Tables

其中tables包含五种类型:FilterNAT(network address translation)Mangle, RawSecurity

Chains

Chains主要包括五种: PREROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING

Chains
需要注意的是:并不是所有的chains对所有tables都适用。如图所示,例如Filter表有三种ChainsINPUTFORWARDOUTPUT

Chains的遍历次序

Rules

Rules就是定义一组规则用来操纵网络traffic。

Commands to manipulate network traffic.

例如:为了阻塞某个IP地址

iptables -A INPUT -s x.x.x.x -J DROP

每条rule一般包含两个基本的组成部分,matching componenttarget component。例如上面的例子中-s x.x.x.x即为matching component-J DROP即为target component

iptables -t [table] -OPTIONS [CHAIN] [matching component] [action component]
Generic Implicit Explicit
p - Protocol TCP Match Extensions
s - Source IP -sport -m
d - Dest IP -dport conntrack, dscp, ecn, iprange etc.
i - IN Interface --tcp-flags
o - OUT Interface

例子

  1. 我们可以通过iptables -nvL查看默认table的iptables规则,也可以通过-t参数指定table。如下所示,prot代表Protocol,opt代表IP options,inout分别代表input和output interface。sourcedestination分别代表source IP地址和destination IP地址。
(ENV) [root@ceph-2 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 943M packets, 1125G bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DOCKER-USER  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT 938M packets, 1540G bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain DOCKER-USER (1 references)
 pkts bytes target     prot opt in     out     source               destination
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0
  1. 配置阻塞访问百度这种垃圾网站,如下所示:
(ENV) [root@ceph-2 ~]# iptables -A INPUT -s baidu.com -j DROP
(ENV) [root@ceph-2 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 8209 packets, 13M bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DROP       all  --  *      *       220.181.38.148       0.0.0.0/0
    0     0 DROP       all  --  *      *       39.156.69.79         0.0.0.0/0

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination
    0     0 DOCKER-USER  all  --  *      *       0.0.0.0/0            0.0.0.0/0

Chain OUTPUT (policy ACCEPT 8297 packets, 25M bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain DOCKER-USER (1 references)
 pkts bytes target     prot opt in     out     source               destination
    0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0

References

上一篇 下一篇

猜你喜欢

热点阅读