Virtual Machine, IP Address, and MAC Address: Frequently Confused Concepts

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.

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:

vm.getGuest().getIpAddress();

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.

This entry was posted in vSphere API and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

14 Comments

  1. Posted December 13, 2010 at 10:43 am | Permalink

    Hey , I have one more question. I have four nics configured with DHCP settings, but I have eth1 as my management interface, how can I get the IP address for eth1 which is the dhcp address, if I do vm.getGuest().getIpAddress(); it is giving me IP address of eth0.

  2. Posted December 13, 2010 at 11:27 pm | Permalink

    Have you checked the “guest.net” property (type: GuestNicInfo[])? It should have the IP addresses associated with other NICs.

  3. Posted December 14, 2010 at 10:14 am | Permalink

    Hi Steve,
    Thanks for the help, actually GuestNicInfo was returning the ip address as array of Strings which caused confusion.

  4. sasidhar
    Posted September 22, 2011 at 11:52 pm | Permalink

    HI,

    can anyone has idea how to set/change the ip adress of VM. If So can anyone share the code..

  5. jayadev
    Posted May 19, 2012 at 3:33 pm | Permalink

    Have any tried vSphere web svc with REST?

  6. kevin
    Posted May 23, 2012 at 5:20 am | Permalink

    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.

    can you give a simple example how to do it by using the way of customize an existing VM’s guest OS? i need to change the ip address of vm but from the sdk i cannot find the interface to do it,thanks

  7. Posted May 23, 2012 at 11:01 pm | Permalink
  8. santhosh
    Posted November 5, 2012 at 11:54 am | Permalink

    Hi,
    I am running 2 VM machines in my physical machine. Can You tell me how i connect the folders of those 2 machines each other using their IP addresses?(I am using windows 7 operating system in my physical and as well as virtual machines )

  9. yasaswi
    Posted March 1, 2013 at 4:12 am | Permalink

    i want to know code for knowing network address and status of guest os whether they are power on or off..i tried the above code vm.getGuest().getIpAddress(); but im getng null

  10. Posted March 1, 2013 at 11:36 am | Permalink

    It happens if you don’t have VMware Tools installed. Even if you have the Tools installed, it takes a little time for the IP to be sent back.
    Steve

  11. Xiang Ren
    Posted August 8, 2013 at 12:34 am | Permalink

    Hi, I have difficultities in set the CustomizationSpec to change IP address through the CloneVm_Task() function, could you please give some more details about the code to change IP address? Thank you very much! BTW, I used .NET as programming tool, should it be much different from Java?

  12. Posted August 12, 2013 at 2:02 am | Permalink

    Hi Xiang,

    Using .NET as programming tool is not allowed as we are all Java here. :) In truth it doesn’t matter as it’s based Web Services. I suggest you to use DoubleCloud proxy to record how vSphere Client does it.

    Best luck!

    Steve

  13. maharani
    Posted August 29, 2013 at 5:44 am | Permalink

    @Xiang Ren
    Hi Xiang,

    I used ASP.net with C# as based language to clone VM and change the IP.
    The code is work and the IP changed.
    FYI I used vim25api. Here is the code.

    VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
    string guestCustomizationScript = “your guest customization script name”;

    //get your customization spec item by name.
    CustomizationSpecItem cusSpecItem = _service.GetCustomizationSpec(_sic.customizationSpecManager, guestCustomizationScript);
    cloneSpec.customization = cusSpecItem.spec;

    //SET IP ADDRESS
    CustomizationFixedIp fixedIP = new CustomizationFixedIp();
    fixedIP.ipAddress = ipAddress;
    cloneSpec.customization.nicSettingMap[0].adapter.ip = fixedIP;

    //USE cloneSpec AS PARAMETER TO CLONEVM_TASK
    ManagedObjectReference cloneTask = _service.CloneVM_Task(vmRef, vmFolderRef, cloneName, cloneSpec);

  14. apapap
    Posted September 27, 2013 at 1:20 am | Permalink

    @maharani
    I am getting the following error :
    “Exception in thread “main” com.vmware.vim25.NotFound” on the following line:
    “CustomizationSpecItem cusSpecItem = _service.GetCustomizationSpec(_sic.customizationSpecManager, guestCustomizationScript);”
    I guess it cannot find the “guestCustomizationScript”.
    Can you please help me with this?

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • NEED HELP?


    My consulting helps clients with virtualization and cloud computing, including VMware infrastructure automation and orchestration, vSphere management APIs, and deep product integration with hypervisors. Current training offerings include vSphere APIs training, vCenter Orchestrator training, and etc. Should you, or someone you know, need these consulting services or training, please feel free to contact me: steve __AT__ doublecloud.org.

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.