Home > vSphere API > Virtual Machine, IP Address, and MAC Address: Frequently Confused Concepts

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

April 13th, 2010 Leave a comment Go to comments

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.

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:

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.

  1. December 13th, 2010 at 10:43 | #1

    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. December 13th, 2010 at 23:27 | #2

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

  3. December 14th, 2010 at 10:14 | #3

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

  4. sasidhar
    September 22nd, 2011 at 23:52 | #4

    HI,

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

  5. jayadev
    May 19th, 2012 at 15:33 | #5

    Have any tried vSphere web svc with REST?

  6. kevin
    May 23rd, 2012 at 05:20 | #6

    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. santhosh
    November 5th, 2012 at 11:54 | #8

    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 )

  8. yasaswi
    March 1st, 2013 at 04:12 | #9

    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

  9. March 1st, 2013 at 11:36 | #10

    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

  10. Xiang Ren
    August 8th, 2013 at 00:34 | #11

    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?

  11. August 12th, 2013 at 02:02 | #12

    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

  12. maharani
    August 29th, 2013 at 05:44 | #13

    @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);

  13. apapap
    September 27th, 2013 at 01:20 | #14

    @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?

  14. July 23rd, 2015 at 05:54 | #15

    Hi Steve,

    Can you tell me what would be the dot notation to get the list of ipaddresses for a VM using the property collector considering that the VM might have multiple nics? I’m getting a blank after: guest.net.ipconfig since ipconfig itself can be an array

  15. July 23rd, 2015 at 21:55 | #16

    The dot notation can not traverse the array. If you need to have something with an item in the array, get the array first before locating the item. Hope it helps.

    Steve

  16. Manish Shenoy B
    January 14th, 2016 at 02:08 | #17

    Hi Steve,

    I know this thread is almost 5 years old, but can you please advice me on a problem?
    I am trying to change the IP address of an offline VM. But, once I have made changes in the registry – System\ControlSet001\Services\Tcpip\Parameters\Interfaces\GUID , I try to boot the system and I see that dual IP has been assigned to the VM in the Network settings advanced option, even when I have over-written the IPAddress. The newly assigned IP has has been showing as a secondary IP and the old IP is the primary IP. Is there a way to delete the traces of the old IP?

    Please advice.

  17. Melvin Moses
    August 19th, 2016 at 07:09 | #18

    Hi Steve,

    I’m a beginner in VMware. I’m using the SDK for cloning the vm. Can you please get me some java samples of vmware customization or links. It would be very grateful.

  18. August 23rd, 2016 at 13:01 | #19

    Hi Melvin,

    You may want to explore the samples we have at vijava.sf.net. Look at the right side for the link.

    -Steve

  19. Melvin Moses
    August 23rd, 2016 at 23:57 | #20

    Hi Steve,

    Thanks for your input. But I’m not looking for VI java API samples on customization. I’m needing some java samples on networking customization using VSphere SDK 6.0. Can you direct me to some samples or links. Thanks in advance.

    Melvin Moses

  20. August 24th, 2016 at 02:26 | #21

    You are very welcome Melvin,

    If there are samples, you should be able to find them in the vSphere SDK download. Another place for help is the VMware community. Good luck!

    -Steve

  1. No trackbacks yet.