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

2022-05-30 15:11 更新

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

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

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

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

在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ī)則鏈的默認(rèn)策略設(shè)置為拒絕:

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

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

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

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

    [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流量),并把默認(rèn)策略設(shè)置為允許:

    [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ī)則鏈設(shè)置為只允許指定網(wǎng)段的主機(jī)訪問(wèn)本機(jī)的22端口,拒絕來(lái)自其他所有主機(jī)的流量:

    [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ī)則是按照從上到下的順序匹配的,因此一定要把允許動(dòng)作放到拒絕動(dòng)作前面,否則所有的流量就將被拒絕掉,從而導(dǎo)致任何主機(jī)都無(wú)法訪問(wèn)我們的服務(wù)。另外,這里提到的22號(hào)端口是ssh服務(wù)使用的(有關(guān)ssh服務(wù),請(qǐng)見(jiàn)下一章),劉遄老師先在這里挖坑,等大家學(xué)完第9章后可再驗(yàn)證這個(gè)實(shí)驗(yàn)的效果。

在設(shè)置完上述INPUT規(guī)則鏈之后,我們使用IP地址在192.168.10.0/24網(wǎng)段內(nèi)的主機(jī)訪問(wèn)服務(wù)器(即前面提到的設(shè)置了INPUT規(guī)則鏈的主機(jī))的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 ~]#

然后,我們?cè)偈褂肐P地址在192.168.20.0/24網(wǎng)段內(nèi)的主機(jī)訪問(wèn)服務(wù)器的22端口(雖網(wǎng)段不同,但已確認(rèn)可以相互通信),效果如下,就會(huì)提示連接請(qǐ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ī)則鏈中添加拒絕所有人訪問(wèn)本機(jī)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主機(jī)訪問(wèn)本機(jī)80端口(Web服務(wù))的策略規(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ī)則鏈中添加拒絕所有主機(jī)訪問(wèn)本機(jī)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
    ………………省略部分輸出信息………………

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

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)