Iptables 记录

1 iptables 预置了四张表: filter, mangel ,nat ,raw 。每张表有不同的功能。

  fileter表

  filter表使我们最常用的表,负责过滤功能,像 允许那些ip访问,拒绝哪些ip访问,允许/拒绝哪些端口访问。

  filter表会根据我们制定的规则去进行过滤,也是命令中默认的操作表。

  nat表

  nat(network address translation) 网络地址转换表,可以实现 一对一,一对多,多对多,的网络地址转换(NAT)工作,iptables 就是使用nat表实现共享上网的。

  mangle 表

  主要工作功能修改数据报文的属性,拆解报文,做出修改,并重新封装的功能。

  raw 表

  优先级最高的表,可以对收到的数据在连接跟踪前进行处理,一旦某个链上的RAW表处理完后,将跳过nat表和ip_conntracck处理,即不在做地址转换和数据包 的链接跟踪处理了。

  各表之间的优先级:

  raw >mangle >nat > filter

 

 

  

2.一些基本操作

  查看filter 表中的规则:下面的命令列出了filter表中的所有规则,总共列出了三条链。INPPUT FORWARD  OUTPUT .  这三条链都有过滤的功能,多以我们要定义某条“过滤“的了规则时,我们会在filter表中定义,具体在那张表中定义取决于工作需求。

  例如:如果要禁止某个主机地址访问我们的主机,需要在input链上制定规则。因为当报文发到主机时,会经过prerouting和input链,但是pretouting链没有fileter表,也就是prerouting没有过滤功能。

  同样可以查看别的表格里的规则

iptables -t raw -L
iptables -t mangle -L
iptables -t nat -L

 

  橙色部分显示的是规则,

也可以查看制定链里的规则,例如查看filter表中的INPUT链的规则 ,(大小写)

-v, --verbose(详细的)

可用此选项的命令:--list, --append, --insert, --delete, --replace

说明:该选项使输出更详细,常与--list 连用。与--list连用时,输出中包括网络接口的地址、规则的选项、TOS掩码、 字节和包计数器,其中计数器是以K、M、G(这里用的是10的幂而不是2的幂)为单位的。若-v 和--append、--insert、--delete 或--replace连用,iptables会输出详细的信息告诉你规则是如何被解释的、是否正确地插入等等。

[root@localhost ~]# iptables -vL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     tcp  --  any    any     anywhere             anywhere             tcp dpt:ssh #conn src/32 > 5 reject-with icmp-port-unreachable
41226   13M ACCEPT     all  --  any    any     anywhere             anywhere             state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere            
  430 27758 ACCEPT     all  --  lo     any     anywhere             anywhere            
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             state NEW tcp dpt:ssh
 9472 1218K REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited
[root@localhost ~]# iptables -L INPUT
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
REJECT     tcp  --  anywhere             anywhere             tcp dpt:ssh #conn src/32 > 5 reject-with icmp-port-unreachable
ACCEPT     all  --  anywhere             anywhere             state RELATED,ESTABLISHED
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere             state NEW tcp dpt:ssh
REJECT     all  --  anywhere             anywhere             reject-with icmp-host-prohibited

 

范例:只要是联机成功的数据包或者与已发送出去的请求相关的数据包就给予通过,不合法就丢弃

iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#对已经建立连接的的数据包给与通过 iptables
-A INPUT -m state --state INVALID -j DROP
#不合法的要丢弃(这句必须要,否则就相当于没有配置,不合法的必须丢弃)


iptables -P OUTPUT DROP
#修改默认规则为丢弃
iptables -A OUTPUT -s 172.16.100.1 -m state --state ESTABLISHED -j ACCEPT


#iptables -P INPUT DROP
#iptables -A INPUT -d 172.16.100.1 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT 

 

 

 

也可以查看其他链的规则

 
posted @ 2019-01-09 12:32  Carry00  阅读(261)  评论(0编辑  收藏  举报