Iptables工具的使用

                                                    Iptables工具的使用

                                                                                  —iptables指令参数

iptables命令在使用上有些复杂,但是如果一定规律发现还是没那么难懂的。

iptables指令可以划分为两个部分,一个是“iptables指令参数”,另一个是“规则语法”。这里先分析“iptables指令参数”。

(一)、iptables命令参数

                                                                

对于上图进行解释:

iptablesiptsbles命令

-tTABLE为选择Netfilter内部命令结构默认为FilterTables。目前TABLE有四个选项filternatmangleraw四种。

Filternetfilter内部最重要的机制,其任务为执行封包的过滤动作,也就是防火墙的功能。

NatNAT(NetworkAddress Translation)也是防火墙上衣个不可或缺的重要机制,其功能用通俗的方式来说,就是IP分享器,只不过其所能执行的功能比访问的IP分享器功能强大了许多。

Mangle我们可以借助mangle机制修改行经防火墙内的封包内容。

Raw负责加快封包穿越防火墙机制的速度,借此提高防火墙的性能。

Filter的操作方式:

-L列出Table内容

-F清除Table内容

-A加入新的规则

-P设定DefaultPilicy

-l插入新的规则

-R取代规则

-D删除规则

(二)、FilterTable的操作

举例1[root@guo~]#iptables -t filter -L

    filterTables的所有内容列出来 。

[root@guo~]#iptables -t filter -L

ChainINPUT (policy ACCEPT) ------------ INPUTChain的所有内容。目前INPUTChain的DefaltChain状态为ACCEPT
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT udp -- anywhere anywhere udp dpt:bootps

ChainFORWARD (policy ACCEPT) ----------- FORWARDChain的所有内容。目前FORWORDChain的DefaltChain状态为ACCEPT
target prot opt source destination ACCEPT all -- anywhere 192.168.122.0/24 stateRELATED,ESTABLISHED
ACCEPT all -- 192.168.122.0/24 anywhere
ACCEPT all -- anywhere anywhere REJECT all -- anywhere anywhere reject-withicmp-port-unreachable
REJECT all -- anywhere anywhere reject-withicmp-port-unreachable
REJECT all -- anywhere anywhere reject-withicmp-host-prohibited

ChainOUTPUT (policy ACCEPT) --------------- OUTPUT Chain的所有内容,此时为空,表示目前没有规则。目前OUTPUTChain 的DefaltChain状态为ACCEPT
target prot opt source destination

由上面可知:FilterTable包含三种ChainINPUTChainOUPUTChainFORWORDChain

举例2[root@guo~]# iptables -t filter -L INPUT

     filterTable中的INPUTChain内容列出来

[root@guo~]# iptables -t filter -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
ACCEPT udp -- anywhere anywhere udp dpt:bootps
ACCEPT tcp -- anywhere anywhere tcp dpt:bootps 
举例3[root@guo~]# iptables -t filter -F

filterTable中的所有内容清除

[root@guo~]# iptables -t filter -F
[root@guo~]# iptables -t filter -L

ChainINPUT (policy ACCEPT)
target prot opt source destination

ChainFORWARD (policy ACCEPT)
target prot opt source destination

ChainOUTPUT (policy ACCEPT)
target prot opt source destination 
举例4[root@guo~]# iptables -t filter -A INPUT -p icmp -j ACCEPT

把规则加入到FilterTabieINPUTChain

解释:-AINOUT将规则将如到INOUTChain

规则:-picmp -j ACCEPT为本次将如到INPUTChain的规则,匹配icmp协议

封包,-jACCEPT将符合条件的封包以特定的方式处理,这里为ACCEPT

[root@guo~]# iptables -t filter -A INPUT -p icmp -j ACCEPT
[root@guo~]# iptables -t filter -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere 
举例5[root@guo~]# iptables -t filter -P FORWARD DROP

-PFORWARD DROP:本次所要设定的FORWARDChainPolicy

[root@guo~]# iptables -t filter -P FORWARD DROP
[root@guo~]# iptables -t filter -L FORWARD

ChainFORWARD (policy DROP) ------------此时FPRWARDChain的DefaultPolicy已经设置为DROP。注意iptables中的-F不会影响到DefaultPolicy的值,若要改
DefaultPolicy的状态,一定要是用-P的参数来设定。
target prot opt source destination 
举例6[root@guo~]# iptables -tfilter -I INPUT 2 -p tcp -j ACCEPT

   INPUTChain内插入新规则INPUT2表示插在第二行

[root@guo~]# iptables -tfilter -I INPUT 2 -p tcp -j ACCEPT
[root@guo~]# iptables -tfilter -L

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere

ChainFORWARD (policy DROP)
target prot opt source destination

ChainOUTPUT (policy ACCEPT)
target prot opt source destination

[root@guo~]# iptables -t filter -L INPUT –line-number ---------在-L参数后方加–line-number的参数,这样会在规则的前方加上行号

ChainINPUT (policy ACCEPT)
num target prot opt source destination

1 ACCEPT udp -- anywhere anywhere udpdpt:domain
2 ACCEPT tcp -- anywhere anywhere tcpdpt:domain
3 ACCEPT udp -- anywhere anywhere udpdpt:bootps
4 ACCEPT tcp -- anywhere anywhere tcpdpt:bootps
5 ACCEPT all -- anywhere anywhere stateRELATED,ESTABLISHED 
举例7[root@guo~]# iptables -t filter -R INPUT 3 -p tcp -j ACCEPT

取代INPUTChain已经存在的规则,3表示取代第桑三条规则,-ptcp -j ACCEPT

以本规则取代原有的规则。

[root@guo~]# iptables -t filter -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT icmp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:domain

[root@guo~]# iptables -t filter -R INPUT 3 -p tcp -j ACCEPT
[root@guo~]# iptables -t filter -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT udp -- anywhere anywhere udp dpt:domain
ACCEPT tcp -- anywhere anywhere --------- 已被取代
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
举例8[root@guo~]# iptables -t filter -D INPUT 2

删除INPUTChain内已经存在的规则

-D INPUT删除INPUTChain已经存在的规则

2删除原有的第二条规则

[root@guo~]# iptables -t filter -L INPUT
ChainINPUT (policy ACCEPT)

target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT udp -- anywhere anywhere udp dpt:domain -----删除
ACCEPT tcp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:domain

[root@guo~]# iptables -t filter -D INPUT 2 ---- 删除
[root@guo~]# iptables -t filter -L INPUT ----- 删除成功

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere tcp dpt:domain
(三)、NATTable的操作

举例1[root@guo~]# iptables -t nat -L

查看NATTable的内容

[root@guo~]# iptables -t nat -L

ChainPREROUTING (policy ACCEPT)
target prot opt source destination

ChainPOSTROUTING (policy ACCEPT)
target prot opt source destination
MASQUERADE tcp -- 192.168.122.0/24 !192.168.122.0/24 masq ports:1024-65535
MASQUERADE udp -- 192.168.122.0/24 !192.168.122.0/24 masq ports:1024-65535
MASQUERADE all -- 192.168.122.0/24 !192.168.122.0/24

ChainOUTPUT (policy ACCEPT)
target prot opt source destination 
由上面可知NATTable也有三个Chain,分别为PREROUTINGChainPOSTROUTINGChainOUTPUT

举例2root@guo~]# iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.0/24 -jSNAT --to 222.24.21.168

将规则--oeth0 -s 192.168.0.0/24 -j SNAT --to 222.24.21.168加入到POSTROUTING Chain中。

[root@guo~]# iptables -t nat -A POSTROUTING -o eth0 -s 192.168.0.0/24 -j SNAT--to 222.24.21.168
[root@guo~]# iptables -t nat -L

ChainPREROUTING (policy ACCEPT)
target prot opt source destination

ChainPOSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 192.168.0.0/24 anywhere to:222.24.21.168 --证明已经成功

ChainOUTPUT (policy ACCEPT)
target prot opt source destination 
(四)、MangleTable的操作

[举例1root@guo~]# iptables -t mangle -L

MangleTable的内容列出

root@guo~]# iptables -t mangle -L

ChainPREROUTING (policy ACCEPT)
target prot opt source destination

ChainINPUT (policy ACCEPT)
target prot opt source destination

ChainFORWARD (policy ACCEPT)
target prot opt source destination

ChainOUTPUT (policy ACCEPT)
target prot opt source destination

ChainPOSTROUTING (policy ACCEPT)
target prot opt source destination
CHECKSUM udp -- anywhere anywhere udp dpt:bootpcCHECKSUM fill 
有上面可以看出:MangleTable包含五种ChainPREROUTINGChainINPUTChainFORWARDChainOUTPUTChainPOSTROUTINGChain

举例2[root@guo~]# iptables -t mangle -A INPUT -p icmp -j ACCEPT

将规则-p icmp -j ACCEPT将如到INPUTChain

[root@guo~]# iptables -t mangle -A INPUT -p icmp -j ACCEPT
[root@guo~]# iptables -t mangle -L INPUT

ChainINPUT (policy ACCEPT)
target prot opt source destination
ACCEPT icmp -- anywhere anywhere --- 查看已经添加成功
(五)、RAWTable的操作

举例1[root@guo~]# iptables -t raw -L

列出RAWTables内容

[root@guo~]# iptables -t raw -L

ChainPREROUTING (policy ACCEPT)
target prot opt source destination

ChainOUTPUT (policy ACCEPT)
target prot opt source destination
由上面可以看出RAWTables包含两种ChainPREROUTINGChainOUTPUTChain

举例2[root@guo~]# iptables -t raw -A OUTPUT -p tcp -j ACCEPT

将规则-p tcp -jACCEPT加入到OUTPUTChain中去

[root@guo~]# iptables -t raw -A OUTPUT -p tcp -j ACCEPT
[root@guo~]# iptables -t raw -L OUTPUT

ChainOUTPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- anywhere anywhere --查看已存在
(六)、由上面可以总结出Netfilter的结构图

                                         

原文地址:https://www.cnblogs.com/linuxer/p/2870538.html