给VMWare Fusion设置固定IP

最近在Mac上用WMware FusionLinux,主要是用来做server端的开发,通过ssh连到虚拟机里做操作。

因为记性不好,我一般都会在/etc/hosts里设置一条记录,给虚拟机一个域名,像是这样

1
2
# ubuntu虚拟机
192.168.110.132 ubuntu.vm

然后我就可以通过域名的方式登录虚拟机了,像这样

1
ssh up4dev@ubuntu.vm

但是使用的过程中遇到了一个问题,有时候虚拟机重启后发现虚拟机的IP发生了变化,这就导致了hosts的设置失效,必须重新设置hosts。

那么怎么能让虚机IP固定下来呢,Google了一圈下来,终于有了比较靠谱的方式,说起来还有点小麻烦,不过按照下面的步骤一条一条的来,应该都会成功。

先说下我的环境,macOS版本是10.12VMware Fusion的版本是8.5.0

步骤1 - 查询虚拟机的MAC地址

话不多说,直接上图

设置-网络适配器

设置-网络是配置-MAC地址

步骤2 - 修改dhcpd.conf

dhcpd.conf位于目录/Library/Preferences/VMware Fusion/vmnet8

用你最喜欢的文本编辑器打开/Library/Preferences/VMware Fusion/vmnet8/dhcpd.conf,我这里用vim,需要用管理员权限sudo

1
sudo vim /Library/Preferences/VMware\ Fusion/vmnet8/dhcpd.conf

看到的内容大概是这样:

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
# Configuration file for ISC 2.0 vmnet-dhcpd operating on vmnet8.
#
# This file was automatically generated by the VMware configuration program.
# See Instructions below if you want to modify it.
#
# We set domain-name-servers to make some DHCP clients happy
# (dhclient as configured in SuSE, TurboLinux, etc.).
# We also supply a domain name to make pump (Red Hat 6.x) happy.
#
###### VMNET DHCP Configuration. Start of "DO NOT MODIFY SECTION" #####
# Modification Instructions: This section of the configuration file contains
# information generated by the configuration program. Do not modify this
# section.
# You are free to modify everything else. Also, this section must start
# on a new line
# This file will get backed up with a different name in the same directory
# if this section is edited and you try to configure DHCP again.
# Written at: 09/14/2016 14:21:31
allow unknown-clients;
default-lease-time 1800; # default is 30 minutes
max-lease-time 7200; # default is 2 hours
subnet 192.168.110.0 netmask 255.255.255.0 {
range 192.168.110.128 192.168.110.254;
option broadcast-address 192.168.110.255;
option domain-name-servers 192.168.110.2;
option domain-name localdomain;
default-lease-time 1800; # default is 30 minutes
max-lease-time 7200; # default is 2 hours
option netbios-name-servers 192.168.110.2;
option routers 192.168.110.2;
}
host vmnet8 {
hardware ethernet 00:50:56:C0:00:08;
fixed-address 192.168.110.1;
option domain-name-servers 0.0.0.0;
option domain-name "";
option routers 0.0.0.0;
}
####### VMNET DHCP Configuration. End of "DO NOT MODIFY SECTION" #######

我们在这个文件的最后添加以下内容:

1
2
3
4
host Ubuntu16.04_0 {
hardware ethernet 00:0C:29:79:EC:1A;
fixed-address 192.168.110.130;
}

有三行内容值得注意:

  • 第1行,Ubuntu16.04_0是虚拟机的名字,看下图,注意要拼写要完全一致。

    VM Name

  • 第2行,00:0C:29:79:EC:1A是上一步获取的MAC地址。

  • 第3行,192.168.110.130是要设置的固定IP地址,注意要在虚拟机的IP网段,一般情况下就用上次虚拟机运行时的动态IP就可以了。

步骤3 - 重启VMWare Fusion

必须重启VMWare Fusion才能使上边的设置生效。

步骤4 - 启动虚拟机

此时在启动虚拟机,你会发现虚拟机的地址不会再变来变去了,永远是你在步骤2设置的固定IP。

参考