標籤:linux 防火牆 資料包 service
首先要注意的是iptables不是防火牆,而是實現防火牆功能的工具。
1.iptables的兩張架構圖:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6B/92/wKioL1Uxs8XDPuL7AAIyHGE7Rec248.jpg" style="float:none;" title="gjorovrpkopk.png" alt="wKioL1Uxs8XDPuL7AAIyHGE7Rec248.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6B/96/wKiom1Uxsm-gquG1AAIbDWY8DEU043.jpg" style="float:none;" title="zfmfkcnooknb.png" alt="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 顯示當前已經設定好的防火牆規則顯示結果:[[email protected] ~]# iptables -nvLChain 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 ACCEPTiptables -P OUTPUT ACCEPTiptables -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:指定目標連接埠
--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-numberChain 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
注意:
1、若規則列表中有多條相同的規則時,按內容匹配只刪除序號最小的一條;
2、按號碼匹配刪除時,確保規則號碼小於等於已有規則數,否則報錯;
3、按內容匹配刪除時,確保規則存在,否則報錯。
技術連結: http://www.aminglinux.com/bbs/forum.php
本文出自 “我不是我” 部落格,請務必保留此出處http://wangwq.blog.51cto.com/8711737/1634806
Linux中iptables的一些解讀