I prefer to use local virtual machines for development. And running a fully-configured Ubuntu machine on VMWare Fusion has been the most flexible way of doing that.

I also like to have separate entries for each hostname in my Mac’s /etc/hosts so that I can easily demo the work via screensharing applications like Google Hangout, Join.me and WebEx, which work best on the host.

Here is a snippet of my /etc/hosts:

etc/hosts entry

One thing that used to bother me was how the IP address for the VM would change every time I started it. I knew I could setup a host-only network, but I also wanted the ability to connect to the internet. Finally, I found a solution: setup two networks, one that is shared with my Mac and one that is host-only.

Here are my two network devices on the VM settings page:

VMWare Settings

Here are the details of the host-only network (if you expand “Advanced options” at the bottom, you will see the Mac address):

VMWare Network Settings

Static IP

Next, we need to setup the static IP address for the host-only network. In order to do that, we need to edit the DHCP configuration file for that network:

sudo vi /Library/Preferences/VMware\ Fusion/vmnet1/dhcpd.conf

As shown in the title bar of the images above, the name of my VM is “Ubuntu1204”. So, here is my entry at the bottom of the above file:

host Ubuntu1204 {
  hardware ethernet <enter mac address here>;
  fixed-address 192.168.125.125;
}

After modifying the DHCP settings for a network, we have to refresh the VM network (or restart it):

sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --configure

Stopped DHCP service on vmnet1
Disabled hostonly virtual adapter on vmnet1
Stopped DHCP service on vmnet8
Stopped NAT service on vmnet8
Disabled hostonly virtual adapter on vmnet8
Stopped all configured services on all networks
Backed up existing network settings to backup file "/tmp/vmware.YIsh5G"
Restored network settings
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --stop
Stopped all configured services on all networks
sudo /Applications/VMware\ Fusion.app/Contents/Library/vmnet-cli --start

Enabled hostonly virtual adapter on vmnet1
Started DHCP service on vmnet1
Started NAT service on vmnet8
Enabled hostonly virtual adapter on vmnet8
Started DHCP service on vmnet8
Started all configured services on all networks

VMWare will now assign a static IP address for the VM. But I will need to force the DHCP client to renew my VM’s IP address.

In the VM, release the current IP for the network-only interface (eth1):

sudo dhclient -v -r eth1

This is what the response will look like:

Release IP

Now, obtain a new IP:

sudo dhclient -v eth1

Renew IP

That’s it!

Resources