Iptables(2) - iptables命令的基本用法

一、iptables命令

iptables是一個規則管理工具. 具有添加、修改、刪除和顯示等功能.

規則和鏈都有計數器:

  • pkts: 由規則或鏈所匹配到的報文的個數
  • bytes: 由規則或鏈匹配到的所有報文大小之和

1.1 用法

用法:

$ iptables [-t table] SUBCOMMAND CHAIN CERTERIA -j TARGET
  • -t table:
    • filter
    • nat
    • mangle
    • raw
  • SUBCOMMAND:
    • 鏈管理:
      • -F: flush, 清空規則鏈, 省略鏈, 表示清空指定表上的所有鏈.
      • -N: new, 創建新的自定義規則鏈.
      • -X: drop, 刪除用戶自定義的規則鏈(空鏈).
      • -Z: zero, 清零規則計數器.
      • -P: Policy, 爲指定鏈設置默認策略, 對filter表中的鏈而言, 默認策略通常有ACCEPT, DROP, REJECT.
      • -E: rEname, 重命名自定義鏈, 引用計數不爲0的自定義鏈, 無法改名, 也無法刪除.
    • 規則管理:
      • -A: append, 將新規則追加於指定鏈的尾部.
      • -I: insert, 將新規則插入指定鏈的指定位置.
      • -D: delete, 刪除指定鏈上的指定規則; 有兩種指定方式, 指定匹配條件和指定規則編號.
      • -R: replace, 替換指定鏈上的指定規則.
      • -C: check, 檢查指定鏈上的指定規則.
    • 查看:
      • -L: list, 列出指定鏈上的所有規則.
      • -n: numberic, 以數字格式顯示地址和端口號.
      • -v: verbose, 顯示詳細信息; -vv, -vvv.
      • --line-numbers: 顯示規則編號.
      • -x: exactly, 顯示計數器計數結果的精確值.
  • 目標:
    • -j TARGET: jump至指定的TARGET.
      • ACCEPT: 接受
      • DROP: 丟棄
      • REJECT: 拒絕
      • RETURN: 返回調用鏈
      • REDIRECT: 端口重定向
      • LOG: 記錄日誌
      • MARK: 做防火牆標記
      • DNAT: 目標地址轉換
      • SNAT: 源地址轉換
      • MASQUERADE: 地址僞裝
      • ...: 更多請查看man文檔.
  • 匹配條件:

    • 基本匹配:
      • [!] -s, --src, --source IP|NetAddr: 檢查報文中源IP地址是否符合此處指定的範圍, 前面加“!”爲非.
      • [!] -d, --dst, --destination IP|NetAddr: 檢查報文中源IP地址是否符合此處指定的地址範圍.
      • [!] -p, --protocol {tcp|udp|icmp}: 檢查報文中的協議, 即ip首部中的protocol所標識的協議.
      • [!] -i, --in-interface IFACE: 數據報文的流入接口, 僅能用於PREROUTING, INPUT以及FORWARD鏈上.
      • [!] -o, --out-interface IFACE: 數據報文的流出接口, 僅能用於FORWARD, OUTPUT以及FORWARD鏈上.
    • 擴展匹配: -m macth_name --spec_options; 例如-m tcp --dport 22

      • 隱式擴展: 對-p protocol指明的協議進行擴展, 可省略-m選項
      • -p tcp:
        • --dport PORT[-PORT]: 目標端口, 可以是單個端口或多個連續的端口.
        • --sport PORT[-PORT]: 源端口.
        • --tcp-flags LIST1 LIST2: 檢查LIST1所指明的所有標誌位, 且這其中, LIST2所表示出的所有標記爲必須爲1, 而餘下的必須爲0, 沒有在LIST1中指明的不做檢查. 標誌位, SYN, ACK, FIN, RST, PSH, URG; 示例, --tcp-flags SYN,ACK,FIN,RST SYN, 匹配tcp三次握手的第一次.
        • --syn: 檢查是否爲新建tcp連接請求的第一次請求.
      • -p udp:
        • --dport
        • --sport
      • -p icmp:

        • --icmp-type: 可用數字表示其類型, 0(echo-reply)和8(echo-request)
      • 顯示擴展: 必須使用-m選項指定使用的擴展, 請查看iptables顯示擴展

二、示例

2.1 在filter表中創建一個新的鏈, 名字叫做IN_putlic

$ iptables -t filter -N IN_public
$ iptables -t filter -L -n
...
Chain IN_public (0 references)
target     prot opt source               destination        

2.2 修改IN_putlic鏈爲OUT_putlic

$ iptables -t filter -E IN_putlic OUT_pulic 
$ iptables -t filter -L -n
...
Chain OUT_pulic (0 references)
target     prot opt source               destination

2.3 修改filter表上的FORWARD鏈的規則爲DROP

$ iptables -t filter -P FORWARD DROP
$ iptables -t filter -L -n
...
Chain FORWARD (policy DROP)
target     prot opt source               destination         
...

2.4 打開firewalld服務, 刪除FORWARD鏈上的第九條規則

$ iptables -t filter -L -n --line-numbers
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
3    FORWARD_direct  all  --  0.0.0.0/0            0.0.0.0/0           
4    FORWARD_IN_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
5    FORWARD_IN_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
6    FORWARD_OUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
7    FORWARD_OUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
8    DROP       all  --  0.0.0.0/0            0.0.0.0/0            ctstate INVALID
9    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

$ iptables -t filter -D FORWARD 9 
$ iptables -t filter -L -n --line-numbers
...
Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
3    FORWARD_direct  all  --  0.0.0.0/0            0.0.0.0/0           
4    FORWARD_IN_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
5    FORWARD_IN_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
6    FORWARD_OUT_ZONES_SOURCE  all  --  0.0.0.0/0            0.0.0.0/0           
7    FORWARD_OUT_ZONES  all  --  0.0.0.0/0            0.0.0.0/0           
8    DROP       all  --  0.0.0.0/0            0.0.0.0/0            ctstate INVALID

2.5 對訪問本機TCP協議的報文通通放行

$ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -p tcp -j ACCEPT
$ iptables -t filter -L -n
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            192.168.123.101     

2.6 允許本機的所有TCP報文通通發出去

iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -p tcp -j ACCEPT
$ iptables -t filter -L -n
...
Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.123.101      0.0.0.0/0           

2.7 將filter表上的所有鏈改爲DROP

$ iptables -t filter -P INPUT DROP
$ iptables -t filter -P FORWARD DROP
$ iptables -t filter -P OUTPUT DROP 

2.8 允許其他主機ping 192.168.123.101這臺主機

$ iptables -t filter -A INPUT -d 192.168.123.101 -p icmp -j ACCEPT
$ iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -p icmp -j ACCEPT
$ iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  779 83096 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.123.101     
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.123.101     

...       

Chain OUTPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  458 64864 ACCEPT     tcp  --  *      *       192.168.123.101      0.0.0.0/0           
    0     0 ACCEPT     icmp --  *      *       192.168.123.101      0.0.0.0/0           

2.9 刪除filter表上的INPUT和OUTPUT鏈上的第二條規則

$ iptables -t filter -L -n --line-numbers
Chain INPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            192.168.123.101     
2    ACCEPT     icmp --  0.0.0.0/0            192.168.123.101     

...       

Chain OUTPUT (policy DROP)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  192.168.123.101      0.0.0.0/0           
2    ACCEPT     icmp --  192.168.123.101      0.0.0.0/0  

$ iptables -t filter -D INPUT 2
$ iptables -t filter -D OUTPUT 2
$ iptables -t filter -L -n
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            192.168.123.101     

...      

Chain OUTPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  192.168.123.101      0.0.0.0/0       

2.10 允許報文從ens33接口中流入和流出

$ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -i ens33 -j ACCEPT
$ iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -o ens33 -j ACCEPT
$ iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 1256  133K ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.123.101     
    0     0 ACCEPT     all  --  ens33  *       0.0.0.0/0            192.168.123.101     

...         

Chain OUTPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  791  113K ACCEPT     tcp  --  *      *       192.168.123.101      0.0.0.0/0           
    0     0 ACCEPT     all  --  *      ens33   192.168.123.101      0.0.0.0/0           

2.11 允許其他主機通過22號端口訪問192.168.123.101這臺主機

$ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -p tcp --dport 22 -j ACCEPT
$ iptables -t filter -A OUTPUT -s 192.168.123.100 -d 0.0.0.0/0 -p tcp --sport 22 -j ACCEPT
$ iptables -t filter -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
...    
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.123.101      tcp dpt:22

...       

Chain OUTPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
...          
    0     0 ACCEPT     tcp  --  *      *       192.168.123.100      0.0.0.0/0            tcp spt:22

2.12 刪除filter表上INPUT和鏈上的第二條規則

$ iptables -t filter -D INPUT 2
$ iptables -t filter -D OUTPUT 2

2.13 允許其他主機通過80端口訪問192.168.123.101這臺主機的httpd服務

$ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -p tcp --dport 80 -j ACCEPT
$ iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -p tcp --sport 80 -j ACCEPT
$ iptables -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
...
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            192.168.123.101      tcp dpt:80

...       

Chain OUTPUT (policy DROP 2 packets, 152 bytes)
 pkts bytes target     prot opt in     out     source               destination         
...
    0     0 ACCEPT     tcp  --  *      *       192.168.123.101      0.0.0.0/0            tcp spt:80

2.13 允許192.168.123.101這臺主機ping其他主機, 不允許其他主機ping 192.168.123.101.

$ iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -p icmp --icmp-type 9 -j ACCEPT
$ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -p icmp --icmp-type 0 -j ACCEPT
$ iptables -t filter -L -n -v
Chain INPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
...
    0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            192.168.123.101      icmptype 0

...        

Chain OUTPUT (policy DROP 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
...
    0     0 ACCEPT     icmp --  *      *       192.168.123.101      0.0.0.0/0            icmptype 9
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章