前言

因为在 macOS 下实在仍受不了 VirtualBox 的各种 Bug 和低性能,转投熟悉的 VMware Fusion 后本来一直都挺稳定的,但在一次 Linked Clone 链接克隆中发现 NAT 网络下 VM 可以访问外网但宿主机和虚拟机之间的网络却无法 ping 通,查阅了非常多的资料仍然无解差点就要暴力选择添加一块新网卡做 host-only,最后发现重启下 VMware Fusion 网络就恢复了,这大概就是传说中的无脑重启解决一切问题。

更新历史

2020 年 06 月 16 日 - 初稿

阅读原文 - https://liaojiaxin158.github.io/post/vmware-fusion/


问题描述

  1. 创建 1 台 NAT 网络类型的 CentOS 虚拟机,IP 为默认 DHCP 段的 192.168.184.128,宿主机和虚拟机网络互通,虚拟机可以正常访问外网
  2. 创建 1 台 Linked Clone 链接克隆 CentOS 虚拟机,IP 为 192.168.184.129,宿主机和虚拟机网络无法互通,虚拟机可以正常访问外网

解决方案

网上解决方案:

  1. 检查 macOS 是否联网
  2. 检查 macOS 是否给予 VMware 权限
  3. 重启虚拟机
  4. 重启 VMware
  5. 重启 macOS

以上方法通通无效

1
2
3
# 重启 vmware network 服务解决问题
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --stop
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --start

修改 VMware Fusion networking

配置 VMware Fusion 虚拟网络配置

VMware Fusion 安装完成后,会在 macOS 中新建两个网卡:vmnet1 以及 vmnet8(在 /Library/Preferences/VMware Fusion 下可以看到)

  1. vmnet1 是 Host-only 模式
  2. vmnet8 是 NAT 模式

这里仅对网卡 vmnet8 进行修改(修改过程中需关闭 VMWare Fusion)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
sudo vi /Library/Preferences/VMware\ Fusion/networking

VERSION=1,0
answer VNET_1_DHCP yes
answer VNET_1_DHCP_CFG_HASH E805FD2F642680617F0D09147C8D5C5ED962ED29
answer VNET_1_HOSTONLY_NETMASK 255.255.255.0
answer VNET_1_HOSTONLY_SUBNET 172.16.131.0
answer VNET_1_VIRTUAL_ADAPTER yes
answer VNET_8_DHCP yes
answer VNET_8_DHCP_CFG_HASH ABBB4D6B9DEC5639F2756A1991B197986AB5597E
answer VNET_8_HOSTONLY_NETMASK 255.255.255.0
answer VNET_8_HOSTONLY_SUBNET 192.168.184.0
answer VNET_8_NAT yes
answer VNET_8_VIRTUAL_ADAPTER yes
add_bridge_mapping en0 2
  1. 将 DHCP 设置为 no, 即使用静态 IP。 将 SUBNET 修改为自己想用的网段,比如自定义 192.168.111.0 网段
  2. 保存退出

注意:只修改 vmnet8 的配置, 不要修改 vmnet1 的配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
sudo vi /Library/Preferences/VMware\ Fusion/vmnet8/nat.conf

# VMware NAT configuration file
# Manual editing of this file is not recommended. Using UI is preferred.

[host]

# NAT gateway address
ip = 192.168.184.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

设置网关为 192.168.111.2,网关的 IP 要和上一步中的 IP 保持网关一致,至此 VMware Fusion 的配置完毕。

配置虚拟机

  1. 将虚拟机网络切换到 NAT 模式
  2. 打开虚拟机,配置虚拟机网络配置信息

vi /etc/sysconfig/network-scripts/ifcfg-ens33

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=ens33
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.111.100      // 自定义的虚拟机 IP, 需与 VMware Fusion 配置的 IP 在同一个网段上
GATEWAY=192.168.111.2       // 配置的网关地址
NETMASK=255.255.255.0       // 配置的掩码
DNS1=114.114.114.114        // macOS 本机的 DNS 地址。 系统偏好设置 -> 网络 -> 在左侧选择当前使用的网络,点击右下角的“高级” 按钮 -> 切换 Tab 页,可找到 DNS 地址。
DNS2=8.8.8.8         // 同上

# 保存退出,重启网络服务
systemctl restart network

参考文章

Troubleshooting networking and internet connection issues in VMware Fusion (1016466)

VMware Fusion 中的网络和 Internet 连接问题故障排除 (1016466)

文章目录
  1. 1. 前言
  2. 2. 更新历史
  3. 3. 问题描述
  4. 4. 解决方案
  5. 5. 修改 VMware Fusion networking
    1. 5.1. 配置 VMware Fusion 虚拟网络配置
    2. 5.2. 配置虚拟机
  6. 6. 参考文章