8.2.2 基本的命令參數(shù)

2022-05-30 15:11 更新

iptables是一款基于命令行的防火墻策略管理工具,具有大量參數(shù),學習難度較大。好在對于日常的防火墻策略配置來講,大家無需深入了解諸如“四表五鏈”的理論概念,只需要掌握常用的參數(shù)并做到靈活搭配即可,這就足以應對日常工作了。

iptables命令可以根據(jù)流量的源地址、目的地址、傳輸協(xié)議、服務類型等信息進行匹配,一旦匹配成功,iptables就會根據(jù)策略規(guī)則所預設的動作來處理這些流量。另外,再次提醒一下,防火墻策略規(guī)則的匹配順序是從上至下的,因此要把較為嚴格、優(yōu)先級較高的策略規(guī)則放到前面,以免發(fā)生錯誤。表8-1總結(jié)歸納了常用的iptables命令參數(shù)。再次強調(diào),我們無需死記硬背這些參數(shù),只需借助下面的實驗來理解掌握即可。

表8-1 iptables中常用的參數(shù)以及作用

參數(shù) 作用
-P 設置默認策略
-F 清空規(guī)則鏈
-L 查看規(guī)則鏈
-A 在規(guī)則鏈的末尾加入新規(guī)則
-I num 在規(guī)則鏈的頭部加入新規(guī)則
-D num 刪除某一條規(guī)則
-s 匹配來源地址IP/MASK,加嘆號“!”表示除這個IP外
-d 匹配目標地址
-i 網(wǎng)卡名稱 匹配從這塊網(wǎng)卡流入的數(shù)據(jù)
-o 網(wǎng)卡名稱 匹配從這塊網(wǎng)卡流出的數(shù)據(jù)
-p 匹配協(xié)議,如TCP、UDP、ICMP
--dport num 匹配目標端口號
--sport num 匹配來源端口號

在iptables命令后添加-L參數(shù)查看已有的防火墻規(guī)則鏈:

    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED
    ACCEPT all -- anywhere anywhere 
    INPUT_direct all -- anywhere anywhere 
    INPUT_ZONES_SOURCE all -- anywhere anywhere 
    INPUT_ZONES all -- anywhere anywhere 
    ACCEPT icmp -- anywhere anywhere 
    REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
    ………………省略部分輸出信息………………

在iptables命令后添加-F參數(shù)清空已有的防火墻規(guī)則鏈:

    [root@linuxprobe ~]# iptables -F
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    ………………省略部分輸出信息………………

把INPUT規(guī)則鏈的默認策略設置為拒絕:

    [root@linuxprobe ~]# iptables -P INPUT DROP
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy DROP)
    target prot opt source destination 
    …………省略部分輸出信息………………

如前面所提到的防火墻策略設置無非有兩種方式,一種是“通”,一種是“堵”,當把INPUT鏈設置為默認拒絕后,就要往里面寫入允許策略了,否則所有流入的數(shù)據(jù)包都會被默認拒絕掉,同學們需要留意規(guī)則鏈的默認策略拒絕動作只能是DROP,而不能是REJECT。

向INPUT鏈中添加允許ICMP流量進入的策略規(guī)則:

在日常運維工作中,經(jīng)常會使用ping命令來檢查對方主機是否在線,而向防火墻的INPUT規(guī)則鏈中添加一條允許ICMP流量進入的策略規(guī)則就默認允許了這種ping命令檢測行為。

    [root@linuxprobe ~]# iptables -I INPUT -p icmp -j ACCEPT
    [root@linuxprobe ~]# ping -c 4 192.168.10.10
    PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
    64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.156 ms
    64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.117 ms
    64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.099 ms
    64 bytes from 192.168.10.10: icmp_seq=4 ttl=64 time=0.090 ms
    --- 192.168.10.10 ping statistics ---
    4 packets transmitted, 4 received, 0% packet loss, time 2999ms
    rtt min/avg/max/mdev = 0.090/0.115/0.156/0.027 ms

刪除INPUT規(guī)則鏈中剛剛加入的那條策略(允許ICMP流量),并把默認策略設置為允許:

    [root@linuxprobe ~]# iptables -D INPUT 1
    [root@linuxprobe ~]# iptables -P INPUT ACCEPT
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination
    ………………省略部分輸出信息………………

將INPUT規(guī)則鏈設置為只允許指定網(wǎng)段的主機訪問本機的22端口,拒絕來自其他所有主機的流量:

    [root@linuxprobe ~]# iptables -I INPUT -s 192.168.10.0/24 -p tcp --dport 22 -j ACCEPT
    [root@linuxprobe ~]# iptables -A INPUT -p tcp --dport 22 -j REJECT
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
    ………………省略部分輸出信息………………

再次重申,防火墻策略規(guī)則是按照從上到下的順序匹配的,因此一定要把允許動作放到拒絕動作前面,否則所有的流量就將被拒絕掉,從而導致任何主機都無法訪問我們的服務。另外,這里提到的22號端口是ssh服務使用的(有關ssh服務,請見下一章),劉遄老師先在這里挖坑,等大家學完第9章后可再驗證這個實驗的效果。

在設置完上述INPUT規(guī)則鏈之后,我們使用IP地址在192.168.10.0/24網(wǎng)段內(nèi)的主機訪問服務器(即前面提到的設置了INPUT規(guī)則鏈的主機)的22端口,效果如下:

    [root@Client A ~]# ssh 192.168.10.10
    The authenticity of host '192.168.10.10 (192.168.10.10)' can't be established.
    ECDSA key fingerprint is 70:3b:5d:37:96:7b:2e:a5:28:0d:7e:dc:47:6a:fe:5c.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '192.168.10.10' (ECDSA) to the list of known hosts.
    root@192.168.10.10's password: 
    Last login: Sun Feb 12 01:50:25 2017
    [root@Client A ~]#

然后,我們再使用IP地址在192.168.20.0/24網(wǎng)段內(nèi)的主機訪問服務器的22端口(雖網(wǎng)段不同,但已確認可以相互通信),效果如下,就會提示連接請求被拒絕了(Connection failed):

    [root@Client B ~]# ssh 192.168.10.10
    Connecting to 192.168.10.10:22...
    Could not connect to '192.168.10.10' (port 22): Connection failed.

向INPUT規(guī)則鏈中添加拒絕所有人訪問本機12345端口的策略規(guī)則:

    [root@linuxprobe ~]# iptables -I INPUT -p tcp --dport 12345 -j REJECT
    [root@linuxprobe ~]# iptables -I INPUT -p udp --dport 12345 -j REJECT
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
    ………………省略部分輸出信息………………

向INPUT規(guī)則鏈中添加拒絕192.168.10.5主機訪問本機80端口(Web服務)的策略規(guī)則:

    [root@linuxprobe ~]# iptables -I INPUT -p tcp -s 192.168.10.5 --dport 80 -j REJECT
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    REJECT tcp -- 192.168.10.5 anywhere tcp dpt:http reject-with icmp-port-unreachable
    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
    ………………省略部分輸出信息………………

向INPUT規(guī)則鏈中添加拒絕所有主機訪問本機1000~1024端口的策略規(guī)則:

    [root@linuxprobe ~]# iptables -A INPUT -p tcp --dport 1000:1024 -j REJECT
    [root@linuxprobe ~]# iptables -A INPUT -p udp --dport 1000:1024 -j REJECT
    [root@linuxprobe ~]# iptables -L
    Chain INPUT (policy ACCEPT)
    target prot opt source destination 
    REJECT tcp -- 192.168.10.5 anywhere tcp dpt:http reject-with icmp-port-unreachable
    REJECT udp -- anywhere anywhere udp dpt:italk reject-with icmp-port-unreachable
    REJECT tcp -- anywhere anywhere tcp dpt:italk reject-with icmp-port-unreachable
    ACCEPT tcp -- 192.168.10.0/24 anywhere tcp dpt:ssh
    REJECT tcp -- anywhere anywhere tcp dpt:ssh reject-with icmp-port-unreachable
    REJECT tcp -- anywhere anywhere tcp dpts:cadlock2:1024 reject-with icmp-port-unreachable
    REJECT udp -- anywhere anywhere udp dpts:cadlock2:1024 reject-with icmp-port-unreachable
    ………………省略部分輸出信息………………

有關iptables命令的知識講解到此就結(jié)束了,大家是不是意猶未盡?考慮到Linux防火墻的發(fā)展趨勢,大家只要能把上面的實例吸收消化,就可以完全搞定日常的iptables配置工作了。但是請?zhí)貏e注意,使用iptables命令配置的防火墻規(guī)則默認會在系統(tǒng)下一次重啟時失效,如果想讓配置的防火墻策略永久生效,還要執(zhí)行保存命令:

    [root@linuxprobe ~]# service iptables save
    iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號