Monday, December 9, 2013

Creating non-DHCP networks in VMWare Fusion on Mac OS X

Creating custom networks in VMWare Fusion on Mac OS X

VMWare Fusion is a hosted virtualization solution that allows us to create VMs on top of other Operating Systems like Windows or Mac OS X. A similar product is Sun Microsystem's Virtual Box (Oracle vBox).

Fusion tends to be easy to use when creating VMs that need to be able to talk to the outside world, because by default, it creates a vNIC bound to a network that is automatically NATed (Network Address Translation) by Fusion's hosted hypervisor. However, by default, Fusion runs a DHCP server on this network and hence, a VM's vNIC will receive a DHCP assigned IP address that can change when the VM is rebooted.

Also, we would like to create networks with customized CIDRs, with DHCP disabled, and assign a custom gateway IP address from the subnet, to each such network that we create. This helps us configure VMs with networks that match our needs exactly.

This blog entry describes how to create these custom networks and plumb static IPs to VMs' vNICs, on Mac OS X.


Step 1. Create a network using the Fusion GUI

It's easiest to begin creating a network using the Fusion GUI, and then edit the required files to suit our requirements. So, start by opening the Fusion application in Mac, then go to VMware Fusion -> Preferences, then click on the Network icon, then "Click on the lock ..." at the bottom of the dialog box, enter your Mac OS X login password, then click on the + sign box - a new network, autonamed by Fusion as vmnet<x> will be created. Let's say our network is vmnet4.

Now, depending on whether you need a purely internal network that doesn't need to talk to the outside world, or otherwise, you do not select or select respectively, the box that says "Allow virtual machines on this network to connect to external networks (using NAT)".

Tick the box that says "Connect the host Mac to this network" if you would like to have an additional IP plumbed on the VM that lets you connect from the Mac OS X to the VM, but ideally you won't need this since you will be assigning a static IP from a range later.

Now, the third box that says "Provide addresses on this network via DHCP" - we don't want DHCP, so leave this box unchecked.


Step 2. Modify the network to define a subnet for it.

cd to the /Library/Preferences/VMware\ Fusion/ directory and open the file named "networking". You will find the following entries for vmnet4 (the network name in our case):

answer VNET_4_DHCP no
answer VNET_4_HOSTONLY_NETMASK 255.255.255.0
answer VNET_4_HOSTONLY_SUBNET 192.168.212.0
answer VNET_4_NAT yes

answer VNET_4_VIRTUAL_ADAPTER yes


Modify the field below:

VNET_4_HOSTONLY_SUBNET 192.168.212.0

to any subnet that you wish to use. Let's say we want to use 10.2.2.0 :

VNET_4_HOSTONLY_SUBNET 10.2.2.0


Step 3. Define the gateway IP for the network

Since we enabled NAT for this network when creating it, Fusion creates a directory named after the network under the same directory above ( /Library/Preferences/VMware\ Fusion/). In our case, this directory would be:

 /Library/Preferences/VMware\ Fusion/vmnet4/

cd to it.

You will find a file, nat.conf under this directory. Open it, and edit the following field to set the gateway IP for this network:

# NAT gateway address
ip = 192.168.212.2

In our case, we can set it to :

ip = 10.2.2.1


NOTE: If you did not select the NAT option, you will not have the nat.conf file to set the gateway IP. Fusion assigns the default IP for the gateway as x.x.x.2, which would be 10.2.2.2 in our case.



Step 4. Restart Fusion

This will cause Fusion to reload the network configuration and use the new settings.


Step 5. Plumb a static iP in your guest VM.

I'll take the example of an Ubuntu 12.04 guest VM. You will need to add a network adapter belonging to the above network when creating the VM. While booting, it will attempt DHCP on this interface, so bootup may be delayed a bit. After the VM comes up, login as root, edit the /etc/network/interfaces file so that your entry looks something like this:


# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
  network 10.2.2.0
  netmask 255.255.255.0
  address 10.2.2.32
  dns-nameservers 8.8.8.8
  gateway 10.2.2.2


Then do an ifdown eth0; ifup eth0 to bring up the interface. If for any reason you do not want to define the gateway in the eth0 entry above, you can use:

# route add default gw 10.2.2.2 eth0

in the VM, but you will need to do this as root each time the VM reboots.

You should be able to ping the outside world from the VM now.


Additional cases:

Case 1.
Sometimes, you may want a DHCP enabled network, but you may want to explicitly set the subnet. You can then still edit the subnet range in the networking file as described above and restart Fusion.

Case 2.
You may want a DHCP enabled network, but you may want to statically assign DHCP IPs to some VMs. You can follow this blog link to configure this case: http://andrewelkins.com/linux/vmware-fusion-5-set-static-ip-address/