[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

可是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