需求:
把80,22,21端口放行,但22端口指定一個IP段,只容許這個IP段的IP訪問的時候,纔可訪問到,其餘段的一律拒絕; 使用腳原本實現
RELATED狀態,這是一個邊緣的一個狀態
好比:客戶端和服務端相互了通訊,創建完鏈接以後,還會有一些額外的連接出來,這時候狀態就變成了RELATED(若牢牢只有ESTABLISHED,而沒有RELATED,頗有可能致使其餘的通訊被禁掉,由於默認策略是INPUT DROP)
ESTABLISHED狀態, 保持鏈接
有時,在沒有增長-m --state這條規則,致使增長了80或21端口,可是不能正常通訊,加這條規則的目的是爲了讓通訊更加順暢vim
編輯腳本bash
[root@yong-02 ~]# vim /usr/local/sbin/iptables.sh
添加內容:網絡
#!/bin/bash ipt="/usr/sbin/iptables" #這裏ipt是定義個一個變量(寫腳本的時候,寫全局的路徑,就是絕對路徑,就是後面再加載它 ,用變量去代替,看着更加簡單) $ipt -F #清空以前的規則——>在沒有 -t 指定表的時候,默認的就是filter表 $ipt -P INPUT DROP #把IPPUT的策略給扔掉 $ipt -P OUTPUT ACCEPT #把OUTPUT放行 $ipt -P FORWARD ACCEPT #把FORWARD放行 $ipt -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT #增長規則,-m --state 指定了狀態,並針對這些狀態放行(-m --state這種用法並很少見, 可是這條規則必須寫進來,目的是讓相關>的數據包放行) $ipt -A INPUT -s 192.168.180.0/24 -p tcp --dport 22 -j ACCEPT #把該網段的22端口數據包放行——>這裏的IP段根據本身的IP段來作實驗 $ipt -A INPUT -p tcp --dport 80 -j ACCEPT #把80端口數據包放行 $ipt -A INPUT -p tcp --dport 21 -j ACCEPT #把21端口數據包放行
執行腳本:tcp
[root@yong-02 ~]# sh /usr/local/sbin/iptables.sh
[root@yong-02 ~]# iptables -nvL Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 29 1924 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT tcp -- * * 192.168.180.0/24 0.0.0.0/0 tcp dpt:22 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:21 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 17 packets, 1492 bytes) pkts bytes target prot opt in out source destination
注意:爲何這裏要用腳本:由於這裏有INPUT DROP,確定要把你的遠程鏈接給斷開,因此須要執行腳本把你的命令批量執行。spa
iptables -I INPUT -p icmp --icmp-type 8 -j DROP 這個規則會產生一個效果,讓你ping外面的機器還能夠ping通,可是ping本機的時候,就不會通了rest
[root@yong-02 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP
注意:這裏會發現可ping通外面的網絡,但本身的虛擬機和物理機則沒法鏈接code
[root@yong-02 ~]# ping www.qq.com PING www.qq.com (192.168.180.135) 56(84) bytes of data.
這時用本身的物理機去ping虛擬機,會發現沒法鏈接ip
C:\Users\YueYong>ping 192.168.180.135 正在 Ping 192.168.180.135 具備 32 字節的數據: 請求超時。 請求超時。 請求超時。 請求超時。 192.168.180.135 的 Ping 統計信息: 數據包: 已發送 = 4,已接收 = 0,丟失 = 4 (100% 丟失),
這時,再來刪除這條命令get
[root@yong-02 ~]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP
service iptables restart 重啓iptables服務虛擬機
[root@yong-02 ~]# service iptables restart //重啓iptables服務 Redirecting to /bin/systemctl restart iptables.service [root@yong-02 ~]# iptables -nvL //這裏會看到還沒禁掉以前的規則 Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 34 2304 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 1 76 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 24 packets, 1980 bytes) pkts bytes target prot opt in out source destination