[root@hanfeng-001 ~]# 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.202.130/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端口數據包放行 而後保存退出:wq [root@hanfeng ~]# sh /usr/local/sbin/iptables.sh //執行腳本 [root@hanfeng ~]# iptables -nvL Chain INPUT (policy DROP 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 30 2148 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 0 0 ACCEPT tcp -- * * 192.168.202.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 18 packets, 1816 bytes) pkts bytes target prot opt in out source destination [root@hanfeng ~]#
[root@hanfeng-001 ~]# iptables -I INPUT -p icmp --icmp-type 8 -j DROP //會發現可ping通外面的網絡,但本身的虛擬機和物理機則沒法鏈接 [root@hanfeng-001 ~]# ping www.qq.com PING www.qq.com (180.96.86.192) 56(84) bytes of data. 64 bytes from 180.96.86.192: icmp_seq=1 ttl=128 time=7.38 ms 64 bytes from 180.96.86.192: icmp_seq=2 ttl=128 time=6.16 ms 64 bytes from 180.96.86.192: icmp_seq=3 ttl=128 time=7.73 ms ^C --- www.qq.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2004ms rtt min/avg/max/mdev = 6.166/7.092/7.731/0.677 ms [root@hanfeng-001 ~]#
[root@hanfeng ~]# iptables -D INPUT -p icmp --icmp-type 8 -j DROP iptables: Bad rule (does a matching rule exist in that chain?). [root@hanfeng ~]#
[root@hanfeng ~]# service iptables restart //重啓iptables服務 Redirecting to /bin/systemctl restart iptables.service [root@hanfeng ~]# iptables -nvL //這裏會看到還沒禁掉以前的規則 Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 81 6996 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 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 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 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 Chain OUTPUT (policy ACCEPT 61 packets, 6060 bytes) pkts bytes target prot opt in out source destination [root@hanfeng ~]#
首先準備兩臺機器(如果怕實驗失敗,可先快照下)html
在虛擬機hf上在添加一塊網卡(已添加過一塊虛擬網卡)linux
而後默認的是NAT模式,而後點擊完成vim
在選擇添加的網卡,並在右側,選擇LAN區段windows
選擇LAN區段,就至關於咱們給網卡鏈接到了內網的一個交換機上,這個交換機,用windows機器是沒法鏈接的,這樣這臺機器和那臺機器連上同一個內網的交換機,它們二者之間通訊便可。bash
而後選擇LAN區段中,剛設置的名稱爲「內網」網絡
最後點擊肯定tcp
選擇克隆的虛擬機hf-02,並添加網卡——>在這臺虛擬機上自己有一塊網卡,由於已經編輯過IP,因此咱們能夠直接禁掉這個網卡.net
而後hf-02添加一塊網卡,和hf添加網卡步驟同樣相同,並選擇LAN區段,中的「內網」,並確認命令行
重啓兩臺虛擬機rest
打開hf虛擬機,輸入ifconfig命令,會看到添加了一個網卡eno33554984
這裏會看到eno33554984網卡,並無IP,並設置IP(新建那個當私網IP,你能夠跟跟視頻中的同樣,也能夠本身設置)
手動命令行ifconfig ens36 192.168.100.1/24,設置IP
![輸入圖片說明](https://static.oschina.net/uploads/img/201712/01120906_md73.png "新增網卡eno3355498")
可是eno33554984網卡的IP,在虛擬機一重啓,那麼IP就會沒了——>若想永久生效,就去編輯配置文件,但默認配置文件是不存在的,須要拷貝eno16777736網卡的配置文件,而後更改其中的內容
由於hf-02虛擬機沒法遠程,因此只能在虛擬機上操做
用ifconfig命令查看hf-02虛擬機的網卡,會發現也沒有IP地址
這裏若想徹底禁掉eno16777736網卡,可執行ifdown eno16777736命令(這一步可省略,若一直失敗的話,只要在設置好IP,兩個私網IP互通便可)
給eno33554984網卡設置IP地址,命令行ifconfig ens36 192.168.100.100/24
而後用ifconfig命令,查看新建的網卡IP地址
這裏會看到hf-02虛擬機中的eno16777736網卡,可是是沒法ping通的——>若爲了保險這裏能夠執行命令ifdown eno16777736
而後給ens36網卡設置IP地址,命令行ifconfig ens36 192.168.100.100/24
這時候,兩臺虛擬機中新建網卡的IP互相嘗試互相ping通
這時hf機器上須要打開路由轉發 ——>想使用nat表,使用網絡的轉發,必須修改內核參數
[root@hanfeng ~]# cat /proc/sys/net/ipv4/ip_forward 0 [root@hanfeng ~]#
[root@hanfeng ~]# echo "1" > !$ echo "1" > /proc/sys/net/ipv4/ip_forward [root@hanfeng ~]# !cat cat /proc/sys/net/ipv4/ip_forward 1 [root@hanfeng ~]#
[root@hanfeng ~]# iptables -t nat -A POSTROUTING -s 192.168.100.0/24 -o ens33 -j MASQUERADE [root@hanfeng ~]#
實現100.0網段能夠上網,作一個欺騙
給hf-02設置網關(數據包過去,從hf到hf-02,從hf-02到hf,設置好默認網關)
而後hf-02嘗試是否能ping通192.168.202.130,這就意味着能夠和外網通訊了
設置DNS,在/etc/resolv.conf中設置,添加mameserver 119.29.29.29