iptables

yum search iptables
yum install iptables-services.x86_64 -y
systemctl stop firewalld.service    ##停止火牆

systemctl mask firewalld.service    ##凍結火牆

一、相關概念

Iptables 利 用的是數據包過濾的機制,所以他會分析數據包的報頭數據,根據報頭數據與定義的規則來決定該數據包是否可以進入主機或者被丟棄,也就是說,根據數據包的分 析資料比對預先定義的規則內容,若數據包數據與規則內容相同則進行動作,否則就繼續下一條規則的比對,重點在比對與分析順序。

iptables -nL    ##查看所有策略


iptables -F    ##清空所有策略(臨時清空,重啓服務後策略恢復)

service iptables save    ##保存



iptables -t filter -nL    ##查看filter
iptables -t nat -nL    ##查看nat

iptables -t mangle -nL    ##查看mangle


iptables的3張表5條鏈:

Filter :主要跟進入linux本機的數據包有關,是默認的table。

INPUT:主要與想要進入linux主機的數據包有關。

OUTPUT :主要與linux主機所要送出的數據包有關。

FORWORD :與linux主機沒有關係,他可以傳遞數據包到後臺的計算機中,與NAT的table相關性較高。

NAT :是network address translation 的縮寫,這個表格主要是用來進行來源與目的地的IP或port的轉換,與linux本機無關,主要與linux主機後的局域網內計算機相關。

PREROUTING :在進行路由判斷之前所要進行的規則(DNAT/REDIRECT)

POSTROYTING:在進行路由判斷之後所要進行的規則(SNAT/MASQUERADE)

OUTPUT :與發送出去的數據包有關。

Mangle :這個表格主要是與特殊的數據包的路由標誌有關。


iptables
              [-t filter,nat,mangle]
              [-AI INPUT,OUTPUT,FORWARD]    #A順序寫入,I插入
              [-io interface]                                       #i輸入設備,o輸出設備
              [-p tcp,udp.icmp,all]                           #網絡協議
              [-s ip/nerwork]                                     #源地址
              [--sport ports]                                      
              [-d ip/network]                                     #目的地址
              [--dport ports]
              [-j ACCEPT DROP REJECT REDIRECT MASQUERADE LOG DNAT SNAT MIRROR QUEUE RETURN MARK]


iptables 是從上向下依次讀取並匹配
iptables -A INPUT  -s 172.25.254.60 -p tcp --dport 80 -j ACCEPT  ##允許來自172.25.254.60主機對80端口訪問
iptables -A INPUT  -p tcp --dport 22 -j ACCEPT    ##允許所有對22端口的訪問
iptables -A INPUT -i lo  -j ACCEPT    ##添加lo迴環接口 
iptables  -A INPUT -j REJECT     ##拒絕其他請求


iptables -nL



iptables -I INPUT 3 -p tcp --dport 80 -j REJECT    ##在第三行插入禁止對80端口的訪問
iptables -nL

但是iptables是由上到下依次匹配數據的,所以60主機依舊能夠訪問80端口


iptables -R INPUT 3 -p tcp --dport 80 -j ACCEPT    ##修改第3條爲允許對80端口的訪問
iptables -nL


iptables -D INPUT  3    ##刪除
iptables -nL



iptables -P INPUT  DROP    ##INPUT策略修改爲丟掉  
iptables -nL



iptables -N westos    ##添加鏈 westos
iptables -nL


iptables -E westos www    ##修改鏈westos 爲www
iptables -nL


iptables -X www    ##刪除鏈
iptables -nL


火牆策略優化
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT  #正在進行的和已經進行過的全部允許
iptables -A INPUT -m state --state NEW -i lo -j ACCEPT                                #新的迴環全部允許
iptables -A INPUT -m state --state NEW -p tcp --dport 80 -j ACCEPT          #新的80端口允許
iptables -A INPUT -m state --state NEW -p tcp --dport 22 -j ACCEPT           #新的22端口允許
iptables -A INPUT -m state --state NEW -j REJECT                                         #新的數據包全部拒絕



iptables -nL


所有策略再次使用時會直接使用第一條提升速度


源地址轉換與地址僞裝

做實驗前要清除filter表
iptables -t nat -A POSTROUTING -j SNAT --to-source 172.25.254.60
iptables -nL -t nat


ssh連接172.25.60.60,看到的ip是172.25.254.60



iptables -t nat  -A PREROUTING -j DNAT --to-dest 172.25.60.60
iptables -nL -t nat 


連接本機172.25.254.160,本機將地址轉到172.25.60.60



iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT  --to-dest 192.168.0.108
iptables -t nat -nL


PREROUING   路由後
POSTROUING   路由前

iptables -t nat -A POSTROUTING -o eth1 -j SNAT --to-source 172.25.254.60  #(eth1-172.25.254.160)源地址轉換,從eth1進來的數據轉換成172.25.254.60

sysctl -a |grep ip_forward    ####開啓本機兩個網卡之間通信
vim /etc/sysctl.cof
net.ipv4.ip_forward = 1
sysctl -p


iptables -t nat -A PREROUTING -i eth0 -d 172.25.254.60 -j DNAT  --to-dest 172.25.60.60  #(eth0-172.25.60.160)目的地址轉換,從eth0進來的數據直接轉到172.25.60.60

發佈了50 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章