Having answered many questions about IP addresses of virtual machines at different occasions, I still see more are coming. I think it’s time to write a blog about it. Hopefully people would search the Internet before raising the question.
First of all, there is a big confusion on the relationship of IP addresses and virtual machines. Many people tend to associate IP addresses with virtual machines, and want to retrieve/change the IP address of a virtual machine.
Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.
In fact, a virtual machine is very much like its physical counterpart. It does not have an IP address by itself. In other words, an IP address is NOT an intrinsic attribute of a machine, either virtual or physical. It might have one or more only after an OS is installed. In most cases, it does have one or more IP addresses, which gives the impression that every machine has an IP address.
A virtual machine does have intrinsic attributes such as MAC addresses if NIC cards are configured. Unlike its physical counterpart, a virtual machine’s MAC address can be re-configured. Some software vendors rely on MAC addresses to lock down their licensed software on particular machines. This mechanism can be, therefore, compromised in virtual environments.
Why do people associate IPs with virtual machines? I think it’s mainly because we take a virtual machine as software rather than hardware and most of the time we have OSes preinstalled. Still, we should not confuse its logical responsibility as a machine rather than OS.
Having said that, vSphere API does help to retrieve IP address from the virtual machine if VMware Tools is installed inside the guest OS. It’s the VMware Tools that reports the IP address back.
With this clarified, we can easily understand many different things. Here are several frequently asked questions and answers.
Q: Can I retrieve the IP address of a virtual machine with vSphere API?
A: Yes. But you ***MUST*** install VMware Tools in the OS running on that virtual machine. To get the IP address, you can retrieve the property guest (Type: GuestInfo) of a virtual machine, and get ip address from the GuestInfo object.
Using vSphere Java API, it’s one line call:
Q: Can I change the MAC address?
A: Yes. You want to call ReconfigVM_Task() defined with VirtualMachine. The VirtualMachineConfigSpec argument could be tricky. I suggest you to use Onyx to record your manual changes and translate the script to Java code.
Q: Can I change the IP address of a virtual machine with vSphere API?
A: Yes. You can change the IP address via customization as part of cloning, or customize an existing VM’s guest OS. Alternatively, you can connect to the guest OS if you have the IP of an existing VM’s guest OS.
The key to the customization via API is to understand the CustomizationSpec. Again the VMware Tools is required at the guest OS. Depending on the type of OS to customize, you may need Microsoft Sysprep tools installed on the vCenter server.
In general, you want to look at other approaches, for example configuring DHCP server to assign IPs either dynamically or as you planned based on their MAC addresses. In vApp, you can also think about the IP pool for assigning IP addresses.