Linux中iptables詳解

    首先要注意的是iptables不是防火牆,而是實現防火牆功能的工具。

    

1.iptables的兩張框架圖:

wKioL1Uxs8XDPuL7AAIyHGE7Rec248.jpg

wKiom1Uxsm-gquG1AAIbDWY8DEU043.jpg


表:

raw表:     對報文設置一個標誌,決定數據包是否被狀態跟蹤機制處

mangle表:  主要用於修改數據包

nat表:     主要用處是網絡地址轉換、端口映射

fileter表: 主要用於過濾包

一般情況我們對filter表做配置的更多。

鏈:

INPUT:      作用於進入本機的包

OUTPUT:     作用於本機送出的包      

FORWARD:    匹配穿過本機的數據包(轉發)       

PREROUTING: 用於修改目的地址(DNAT)       

POSTROUTING:用於修改源地址 (SNAT)


2.iptables的基本操作

啓動指令:service iptables start    
重啓指令:service iptables restart    
關閉指令:service iptables stop
保存指令:service iptables save
清除規則:iptables -F   
將鏈的記數的流量清零: iptables -Z
清除鏈: iptables -X
清空iptables時一般-F -Z -X一起使用
iptables -nvL 顯示當前已經設置好的防火牆規則

顯示結果:
[root@wangwq ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   37  4317 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    1    52 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:22 
   39  4106 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT 37 packets, 5314 bytes)
 pkts bytes target     prot opt in     out     source               destination

iptables的三種狀態:

ACCEPT  允許

DROP    丟棄

REJECT  拒絕

DROP和REJECT的區別:DROP是直接不讓進入,而REJECT是先讓進入然後再拒絕,DROP更安全,所以一般拒絕都用DROP。

iptables默認的規則是允許,即

iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT

3.iptables的配置:

iptables的兩種配置思路: 

 1)默認允許,拒絕特別;

 2)默認拒絕,允許特別;

二者都有自己的特點,看情況而定。但是注意如果要選擇第二種配置思路,配置前切記先把ssh設置爲ACCEPT,因爲一般機器不在我們身邊,一旦配置默認拒絕,那我們的遠程登錄就會斷開連接,那問題就大了。

配置默認拒絕前設置:

iptables -A INPUT -p tcp --dport 22 -j ACCEPT    

iptables -A OUTPUT -p tcp --sport 22 -j ACCEPT

還有一種方法:做一個計劃任務,讓iptables定期停止,即執行service iptables stop,這樣的話即使配置默認拒絕前沒有允許ssh也沒關係,等到計劃任務生效的時間iptables就會自動清除所有的配置,包括默認規則。

iptables的基本語法:

iptables [-t filter/nat] [-A/I] [INPUT/OUTPUT/FORWARD] [-i/o interface] [-p tcp/udp/icmp/all] [-s ip/network] [--sport ports] [-d ip/network] [--dport ports] [-j ACCEPT/DROP]

不加-t時默認是filter

語法參數:

-I:第一行插入

-A:最後追加 

-i/o:指的是數據要進入或出去所要經過的端口,如eth1,eth0,pppoe等

-p:你所要指定的協議 

-s:指定來源ip,可以是單個ip如192.168.109.131,也可以是一個網絡 192.168.109.0/24,還可以是一個域名如163.com,如果你填寫的是域名系統會自動解析出他的ip並在iptables裏顯示

--sport:來源端口 

-d:指定目標ip

--dport:目標端口 

-j:執行參數ACCEPT或DROP,REJECT一般不用

如果配置的是INPUT(進入),則來源ip是運程ip,目標端口就是本機;OUTPUT相反,相信可以理解。


4.iptables的執行優先級: 

iptables的執行順序是自上而下,當有配置產生衝突時,前面執行的生效。

5.刪除iptables

有時我們需要刪除其中一條或幾條iptables,那就不能iptables -F 了,刪除某一條iptables常用的有兩種方法:

1.修改配置文件

# vim /etc/sysconfig/iptables

刪除相應的行,然後service iptables restart,再service iptables save.

注意:

    修改完配置文件不能先save,一定要先restart才能save,要不然就白做了。因爲save會在iptables服務啓動時重新加載,要是在重啓之前直接先調用了service iptables save 那麼你    的/etc/sysconfig/iptables 配置就回滾到上次啓動服務的配置了。

  

2.命令刪除

 1)如果你記得配置時的寫法,那麼可以直接iptables -D 後跟上配置時的寫法。如:

 iptables -D INPUT -s 10.72.11.12 -p tcp --sport 1234 -d 10.72.137.159 --dport 80 -j DROP

  2)一般的方法:

a. 查看每條iptables的序號:# iptables -nvL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      263 21025 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
2        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0
b. 根據查看的序號刪除:# iptables -D INPUT 2



技術鏈接: http://www.aminglinux.com/bbs/forum.php

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章