- 需求:虚拟机使用NAT模式,自动获取10.0.0.0/24的IP地址,网关是10.0.0.254,虚拟机可以ping通 www.baidu.com
- 科普:NAT模式使用vmnet8网卡
- NAT(Network Address Translation,网络地址转换),NAT模式是比较简单的实现虚拟机上网的方式。简单地理解,NAT模式的虚拟机就是通过宿主机(物理计算机)上网和交换数据的。
- 在NAT模式下,虚拟机的网卡连接到宿主机的VMnet8上。此时系统的VMware NAT Service服务就充当了路由器,负责将虚拟机发送到VMnet8的包进行地址转换之后发送到实际的网络上,再将实际网络上返回的包进行地址转换后通过VMnet8发送给虚拟机。VMware DHCP Service负责为虚拟机分配IP地址。
- NAT网络特别适合于家庭里计算机直接连接网线的情况,当然办公室的局域网环境也是适合的,其优势是不会与其他物理主机IP冲突,且在没有路由的环境下也可以通过SSH NAT连接虚拟机学习,换了网络环境虚拟机IP等不受影响,这是老男孩推荐的选择。
- 由于macOS平台下的VMware Fusion 8软件不具备“虚拟网络编辑器”功能,必须通过修改两个配置文件来实现对NAT模式的配置
- “虚拟网卡编辑器”配置文件
/Library/Preferences/VMware\ Fusion/networking - “vmnet8默认网关”配置文件
/Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
- “虚拟网卡编辑器”配置文件
- 解决问题:在root身份下修改两个配置文件
- 第零步:切换到root身份下
- $ sudo su
- 第一步:修改“虚拟网卡编辑器”配置文件
- $ cp /Library/Preferences/VMware\ Fusion/networking{,.bak}
# 先备份源文件,避免改错配置之后造成配置文件损坏 - $ vim /Library/Preferences/VMware\ Fusion/networking
- 默认配置如下(修改前的状态)
VERSION=1,0
answer VNET_1_DHCP yes
answer VNET_1_DHCP_CFG_HASH BBDD0CB0C52A2FFF44945811CE493B7216FA3BAE
answer VNET_1_HOSTONLY_NETMASK 255.255.255.0
answer VNET_1_HOSTONLY_SUBNET 192.168.38.0
answer VNET_1_VIRTUAL_ADAPTER yes
answer VNET_2_VIRTUAL_ADAPTER no
answer VNET_8_DHCP yes
answer VNET_8_DHCP_CFG_HASH 02CB138B4747CCA0E3A6F8274E3C26A790519B77
answer VNET_8_HOSTONLY_NETMASK 255.255.255.0
answer VNET_8_HOSTONLY_SUBNET 192.168.78.0
answer VNET_8_NAT yes
answer VNET_8_VIRTUAL_ADAPTER yes
add_bridge_mapping en0 2 - 将 answer VNET_8_HOSTONLY_SUBNET 这一项后面的地址改为 10.0.0.0
- :wq 保存退出
- 默认配置如下(修改前的状态)
- $ cp /Library/Preferences/VMware\ Fusion/networking{,.bak}
- 第二步:修改“vmnet8默认网关”配置文件
- cp /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf{,.bak}
# 先备份源文件,避免改错配置之后造成配置文件损坏 - $ vim /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf
- 默认配置如下(修改前的状态)
# VMware NAT configuration file
[host]
# NAT gateway address
ip = 192.168.78.2
netmask = 255.255.255.0
# VMnet device if not specified on command line
device = vmnet8
# Allow PORT/EPRT FTP commands (they need incoming TCP stream ...)
activeFTP = 1
# Allows the source to have any OUI. Turn this on if you change the OUI
# in the MAC address of your virtual machines.
allowAnyOUI = 1
# Controls if (TCP) connections should be reset when the adapter they are
# bound to goes down
resetConnectionOnLinkDown = 1
# Controls if (TCP) connection should be reset when guest packet‘s destination
# is NAT‘s IP address
resetConnectionOnDestLocalHost = 1
# Controls if enable nat ipv6
natIp6Enable = 0
# Controls if enable nat ipv6
natIp6Prefix = fd15:4ba5:5a2b:1008::/64
[tcp]
# Value of timeout in TCP TIME_WAIT state, in seconds
timeWaitTimeout = 30
[udp]
# Timeout in seconds. Dynamically-created UDP mappings will purged if
# idle for this duration of time 0 = no timeout, default = 60; real
# value might be up to 100% longer
timeout = 60
[netbios]
# Timeout for NBNS queries.
nbnsTimeout = 2
# Number of retries for each NBNS query.
nbnsRetries = 3
# Timeout for NBDS queries.
nbdsTimeout = 3
[incomingtcp]
# Use these with care - anyone can enter into your VM through these...
# The format and example are as follows:
#<external port number> = <VM‘s IP address>:<VM‘s port number>
#8080 = 172.16.3.128:80
[incomingudp]
# UDP port forwarding example
#6000 = 172.16.3.0:6001 - (找到 # NAT gateway address 的下一行)将 ip = 192.168.78.2 这一项的地址改为 10.0.0.254
- :wq 保存退出
- 默认配置如下(修改前的状态)
- cp /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf{,.bak}
- 第三步:先关闭正在运行的虚拟机,然后退出VMware Fusion软件,再重新打开VMware Fusion软件,正常启动虚拟机即可
- 第四步:记得退出root身份的登录状态
- $ exit
# 也可以按 Command+R 两个键,效果等价
- $ exit
- 第零步:切换到root身份下
原文地址:https://www.cnblogs.com/linyongfeng/p/9310516.html
时间: 2024-10-28 04:59:33