Linux學習系列之Iptables

2022-07-23 06:15:14 字數 4317 閱讀 9609

iptables命令是linux上常用的防火牆軟體,是netfilter專案的一部分。可以直接配置,也可以通過許多前端和圖形介面配置。

iptables(選項)(引數)
-t《表》:指定要操縱的表;

-a:向規則鏈中新增條目;

-d:從規則鏈中刪除條目;

-i:向規則鏈中插入條目;

-r:替換規則鏈中的條目;

-l:顯示規則鏈中已有的條目;

-f:清楚規則鏈中已有的條目;

-z:清空規則鏈中的資料報計算器和位元組計數器;

-n:建立新的使用者自定義規則鏈;

-p:定義規則鏈中的預設目標;

-h:顯示幫助資訊;

-p:指定要匹配的資料報協議型別;

-s:指定要匹配的資料報源ip位址;

-j《目標》:指定要跳轉的目標;

-i《網路介面》:指定資料報進入本機的網路介面;

-o《網路介面》:指定資料報要離開本機所使用的網路介面。

iptables命令選項輸入順序:

iptables -t 表名 <-a/i/d/r> 規則鏈名 [規則號] <-i/o 網絡卡名》 -p 協議名 <-s 源ip/源子網》 --sport 源埠 <-d 目標ip/目標子網》 --dport 目標埠 -j 動作
表名包括:

mangle:資料報修改(qos),用於實現服務質量。

net:位址轉換,用於閘道器路由器。

filter:包過濾,用於防火牆規則。

規則鏈名包括:

動作包括:

清除已有iptables規則

iptables -f

iptables -x

iptables -z

開放指定的埠

iptables -a input -s 127.0.0.1 -d 127.0.0.1 -j accept               #允許本地回環介面(即執行本機訪問本機)

iptables -a input -m state --state established,related -j accept #允許已建立的或相關連的通行

iptables -a output -j accept #允許所有本機向外的訪問

iptables -a input -p tcp --dport 22 -j accept #允許訪問22埠

iptables -a input -p tcp --dport 80 -j accept #允許訪問80埠

iptables -a input -p tcp --dport 21 -j accept #允許ftp服務的21埠

iptables -a input -p tcp --dport 20 -j accept #允許ftp服務的20埠

iptables -a input -j reject #禁止其他未允許的規則訪問

iptables -a forward -j reject #禁止其他未允許的規則訪問

遮蔽ip

iptables -i input -s 123.45.6.7 -j drop       #遮蔽單個ip的命令

iptables -i input -s 123.0.0.0/8 -j drop #封整個段即從123.0.0.1到123.255.255.254的命令

iptables -i input -s 124.45.0.0/16 -j drop #封ip段即從123.45.0.1到123.45.255.254的命令

iptables -i input -s 123.45.6.0/24 -j drop #封ip段即從123.45.6.1到123.45.6.254的命令是

檢視已新增的iptables規則

iptables -l -n -v

chain input (policy drop 48106 packets, 2690k bytes)

pkts bytes target prot opt in out source destination

5075 589k accept all -- lo * 0.0.0.0/0 0.0.0.0/0

191k 90m accept tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22

1499k 133m accept tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80

4364k 6351m accept all -- * * 0.0.0.0/0 0.0.0.0/0 state related,established

6256 327k accept icmp -- * * 0.0.0.0/0 0.0.0.0/0

chain forward (policy accept 0 packets, 0 bytes)

pkts bytes target prot opt in out source destination

chain output (policy accept 3382k packets, 1819m bytes)

pkts bytes target prot opt in out source destination

5075 589k accept all -- * lo 0.0.0.0/0 0.0.0.0/0

刪除已新增的iptables規則

將所有iptables以序號標記顯示,執行:

iptables -l -n --line-numbers
比如要刪除input裡序號為8的規則,執行:

iptables -d input 8

例項:

iptables -a input -s 127.0.0.1 -d 127.0.0.1 -j accept

iptables -a input -m state --state established,related -j accept

iptables -a output -j accept

iptables -a input -p tcp --dport 22 -j accept

iptables -a input -p tcp --dport 80 -j accept

iptables -a input -p tcp --dport 443 -j accept

iptables -a input -p tcp --dport 111 -j accept

iptables -a input -p tcp --dport 21 -j accept

iptables -a input -p tcp --dport 20 -j accept

iptables -a input -p tcp --dport 7789 -j accept

iptables -a input -p tcp --dport 10050 -j accept

iptables -a input -p tcp --dport 30000:65535 -j accept

iptables -a input -p udp --dport 123 -j accept

iptables -a input -p udp --dport 957 -j accept

iptables -a input -p udp --dport 924 -j accept

iptables -a input -p udp --dport 883 -j accept

iptables -a input -p udp --dport 908 -j accept

iptables -a input -j reject

Linux學習系列 訊號

訊號是軟體中斷,提供了典型的非同步機制。每個訊號有乙個編號,訊號分為兩類 非實時訊號和實時訊號。0 31編號屬於非實時訊號 31 63編號屬於實時訊號。為什麼會分為這兩類訊號呢?這個主要是因為歷史原因,首先實現的是非實時訊號,非實時訊號也成為不可靠訊號,是因為其實現機制導致這類訊號可能會丟失 而實時...

linux 學習系列 Linux 遠端登入

linux一般作為伺服器使用,而伺服器一般放在機房,你不可能在機房操作你的linux伺服器。這時我們就需要遠端登入到linux伺服器來管理維護系統。linux系統中是通過ssh服務實現的遠端登入功能,預設ssh服務埠號為 22。window系統上 linux 遠端登入客戶端有securecrt,pu...

Linux命令系列之htpasswd

htpasswd命令是apache的web伺服器內建工具,用於建立和更新儲存使用者名稱 域和使用者基本認證的密碼檔案。c 建立乙個加密檔案 n 不更新加密檔案,只將加密後的使用者名稱密碼顯示在螢幕上 m 預設採用md5演算法對密碼進行加密 d 採用crypt演算法對密碼進行加密 p 不對密碼進行進行...