原创 2017年06月13日 14:47:45windows
最近有一台Esxi服务器须要作托管,只有一个公网ip,可是全部虚拟机又都须要访问公网,我在Esxi里没有找到相似Vmware workstation 的NAT网络模式,因此我想使用一台虚拟机作NAT服务器。个人虚拟机系统有Win2008 和 CentOS7,考虑到稳定性我选择了在Linux上实现。 如下是我在笔记本上用Vmware WorkStation 作的模拟实验:centos
yum -y update
yum -y install net-tools.x86_64
二、配置IP地址服务器
[root@test ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.32.130 netmask 255.255.255.0 broadcast 192.168.32.255 inet6 fe80::20c:29ff:fe4c:4213 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:4c:42:13 txqueuelen 1000 (Ethernet) RX packets 6525 bytes 2334108 (2.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4816 bytes 2671369 (2.5 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.10.1 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::20c:29ff:fe4c:421d prefixlen 64 scopeid 0x20<link> ether 00:0c:29:4c:42:1d txqueuelen 1000 (Ethernet) RX packets 5656 bytes 1625792 (1.5 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3596 bytes 3461007 (3.3 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
个人两块网卡分别为 eno16777736 和 eno33554984,我打算将eno16777736 做为外网访问,故IP保持默认DHCP自动分配,这边分配到的IP为 192.168.32.130
。eno33554984 做为内网访问,ip设置静态 192.168.10.1
,ip具体设置方法再也不累述。网络
三、启用IP转发
修改 /etc/sysctl.conf 文件,添加以下行tcp
net.ipv4.ip_forward = 1
使用 sysctl -p
命令生效工具
四、修改接口区域
默认状况下两块网卡的zone为public,如今须要将eno16777736 设置为 external,将 eno33554984 设置为 internal。测试
firewall-cmd --zone=external --change-interface=eno16777736 --permanent firewall-cmd --zone=internal --change-interface=eno33554984 --permanent
其中 --permanent
表示永久生效。spa
五、设置IP地址假装.net
firewall-cmd --zone=external --add-masquerade --permanent
六、设置NAT规则code
firewall-cmd --permanent --direct --passthrough ipv4 -t nat -I POSTROUTING -o eno16777736 -j MASQUERADE -s 192.168.10.0/24
七、设置生效
firewall-cmd --reload
八、验证
在win7虚拟机中配置ip为 192.168.10.10
,网关192.168.10.1
,如图
ping 百度测试网络是否连通
至此,NAT服务器配置完成。
firewall-cmd --zone=external --add-forward-port=port=3389:proto=tcp:toport=3389:toaddr=192.168.10.10 --permanent firewall-cmd --reload
查看设置,我这里设置了两个端口
[root@test ~]# firewall-cmd --zone=external --list-forward-ports port=3389:proto=tcp:toport=3389:toaddr=192.168.10.10 port=5000:proto=udp:toport=5000:toaddr=192.168.10.10
验证端口映射:
在物理机上使用 mstsc 工具,链接CentOS 的外网IP ,即一开始提到的 192.168.32.130
好了,实验完毕。由于我全部的虚拟机都是静态ip,因此没有安装DHCP服务。