Linux iptables基本管理

2021-09-23 08:15:17 字數 4601 閱讀 3961

1.iptables基本管理

問題本案例要求熟悉iptables工具的基本管理,分別練習以下幾方面的操作:

檢視當前生效的防火牆規則列表

追加、插入新的防火牆規則,修改現有的防火牆規則

刪除、清空指定的防火牆規則

方案採用兩台rhel6虛擬機器,在其中svr5上配置iptables防火牆規則,pc205作為測試用的客戶機,如圖-1所示。

圖-1步驟

實現此案例需要按照如下步驟進行。

步驟一:檢視當前生效的防火牆規則列表

1)列出filter表的規則

filter表是iptables預設操作的表,因此 -t filter 通常被省略。

filter表預設包括input、output、forward這三個規則鏈。

[root@svr5 ~]# iptables -l //檢視所有規則鏈

chain input (policy accept)

target prot opt source destination

chain forward (policy accept)

target prot opt source destination

chain output (policy accept)

target prot opt source destination

[root@svr5 ~]# iptables -l input //只看input鏈

chain input (policy accept)

target prot opt source destination

2)列出nat表的規則

nat表預設包括prerouting、postrouting、output這三個規則鏈。

[root@svr5 ~]# iptables -t nat -l

chain prerouting (policy accept)

target prot opt source destination

chain postrouting (policy accept)

target prot opt source destination

chain output (policy accept)

target prot opt source destination

3)列出raw表的規則

raw表預設包括prerouting、output這兩個規則鏈。

[root@svr5 ~]# iptables -t raw -l

chain prerouting (policy accept)

target prot opt source destination

chain output (policy accept)

target prot opt source destination

4)列出mangle表的規則

mangle表預設包括所有的五種規則鏈。

[root@svr5 ~]# iptables -t mangle -l

chain prerouting (policy accept)

target prot opt source destination

chain input (policy accept)

target prot opt source destination

chain forward (policy accept)

target prot opt source destination

chain output (policy accept)

target prot opt source destination

chain postrouting (policy accept)

target prot opt source destination

步驟二:追加、插入新的防火牆規則

1)-a,在末尾追加一條新的防火牆規則

允許訪問本機的所有tcp資料報:

[root@svr5 ~]# iptables -a input -p tcp -j accept //新增一條規則

[root@svr5 ~]# iptables -l input //檢視結果

chain input (policy accept)

target prot opt source destination

accept tcp – anywhere anywhere

在檢視規則時,可以結合-n選項以數字形式顯示位址、埠等資訊:

[root@svr5 ~]# iptables -nl input

chain input (policy accept)

target prot opt source destination

accept tcp – 0.0.0.0/0 0.0.0.0/0

還可以結合–line-numberrs來顯示規則的行號(順序號):

[root@svr5 ~]# iptables -nl input --line-numbers

chain input (policy accept)

num target prot opt source destination

1 accept tcp – 0.0.0.0/0 0.0.0.0/0 //當前鏈的第1條規則

2)-i,在開頭或指定位置插入一條新的防火牆規則

在input鏈的開頭插入規則,允許所有的udp協議資料報:

[root@svr5 ~]# iptables -i input -p udp -j accept

[root@svr5 ~]# iptables -nl input --line-numbers

chain input (policy accept)

num target prot opt source destination

1 accept udp – 0.0.0.0/0 0.0.0.0/0 //新增規則作為第一條

2 accept tcp – 0.0.0.0/0 0.0.0.0/0 //原第一條變為第二條

插入input鏈的第2條規則,允許所有的icmp協議資料報:

[root@svr5 ~]# iptables -i input 2 -p icmp -j accept

[root@svr5 ~]# iptables -nl input --line-numbers

chain input (policy accept)

num target prot opt source destination

1 accept udp – 0.0.0.0/0 0.0.0.0/0

2 accept icmp – 0.0.0.0/0 0.0.0.0/0 //新增的第2條規則

3 accept tcp – 0.0.0.0/0 0.0.0.0/0

步驟三:刪除、清空指定的防火牆規則

1)-d,刪除指定的規則

刪除input鏈的第3條規則:

[root@svr5 ~]# iptables -d input 3 //刪除第3條規則

[root@svr5 ~]# iptables -nl input --line-numbers //確認刪除結果

chain input (policy accept)

num target prot opt source destination

1 accept udp – 0.0.0.0/0 0.0.0.0/0

2 accept icmp – 0.0.0.0/0 0.0.0.0/0

刪除指定具體內容的規則:

[root@svr5 ~]# iptables -d input -p udp -j accept //刪除udp放行規則

[root@svr5 ~]# iptables -nl input --line-numbers //確認刪除結果

chain input (policy accept)

num target prot opt source destination

1 accept icmp – 0.0.0.0/0 0.0.0.0/0

2)-f,清空規則

清空filter表的所有規則:

[root@svr5 ~]# iptables -f

[root@svr5 ~]# iptables -nl --line-numbers

chain input (policy accept)

num target prot opt source destination

chain forward (policy accept)

num target prot opt source destination

chain output (policy accept)

num target prot opt source destination

清空nat表的所有規則:

[root@svr5 ~]# iptables -t nat -f

[root@svr5 ~]# iptables -t mangle -f

[root@svr5 ~]# iptables -t raw -f

Linux Iptables命令詳解

ipaddr ipaddr port port 可以指定乙個單一的新的ip位址,乙個ip位址範圍,也可以附加乙個埠範圍 只能在指定 p tcp 或者 p udp的規則裡 如果未指定埠範圍,源埠中512以下的 埠 會被安置為其他的512以下的埠 512到1024之間的埠會被安置為1024以下的,其他埠...

Linux iptables規則詳解

filter input drop 345 43237 forward accept 0 0 output accept 306 41346 ainput p tcp m tcp dport 10022 j accept ainput p tcp m tcp dport 80 j accept ai...

Linux iptables使用例項

1.使區域網使用者可共享外網 撥號上網 echo 1 proc sys net ipv4 ip forward iptables t nat a postrouting o ppp0 j masquerade iptables t nat a postrouting s 192.168.1.0 24...