WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud Foundation
  • VKS
  • Homelab
    • Resources
    • Nested Virtualization
  • VMware Nostalgia
  • Apple
You are here: Home / Automation / How to extract host information from within a VM?

How to extract host information from within a VM?

01.15.2011 by William Lam // 34 Comments

From time to time, I see this question come up asking how one might be able to extract a certain piece of information from either ESX(i) or the management APIs (vSphere API) from within a virtual machine. The simple answer is you can not, by default the guest operating system has no idea of the underlying hypervisor nor does it have the access to the management APIs. This of course, assumes you are following VMware's best practices in isolated and segregating off your management network from your virtual machine network.

Having said that, there are certain bits of information that you can extract about your ESX(i) host from within the guestOS using some of the utilities that is installed with VMware Tools. The first utility is called VMware Toolbox command which can be found on both UNIX/Linux and Windows systems that have tools installed.

UNIX/Linux - /usr/bin/vmware-toolbox-cmd

Windows - C:\Program Files\VMware\VMware Tools\VMwareToolboxCmd.exe

This utility provide some information about ESX(i) and guestOS configuration including basic resource statistics.

One interesting sub-command is the resource statistics about both ESX(i) and guestOS such as speed of the hypervisor's CPUs or the current balloon, swap, memory limit, memory reservations, cpu limit or cpu reservations.

Here is an example of retrieving ESX(i) CPU speed.

As I mentioned before, you do not have access to the management network that your ESX(i) are on and that also means you do not have access to the vSphere APIs. What if you wanted to to associate a specific piece of information from ESX(i) and be able to access that piece of information from the guestOS? You can do so with the vmtoolsd (VMware Tools Daemon) utility.

UNIX/Linux - /usr/bin/vmtoolsd

Windows - C:\Program Files\VMware\VMware Tools\vmtoolsd.exe

This utility has an option called --cmd that allows you to run various commands including one that allows you to extract guest information using the "info-get" parameter. This guestinfo can be set using two methods:

1) Hard-coded within the virtual machine's .vmx configuration file
2) In VMX memory while a virtual machine is running within ESX(i)

Option 1 is pretty straight forward, you can add a custom attribute that has the following format:

guestinfo.[variable] = [value]

e.g.
guestinfo.provision.date = "01/11/2011"

You can use the vSphere Client to add this custom attribute while the virtual machine is powered off or you can manually edit the .vmx configuration file. If you use the latter, you will need to reload the VM's configuration, you can use vim-cmd vmsvc/reload.[vmid] to do so. One the VM is powered on, you can extract this piece of information, here is an example:

Option 2 is actually pretty interesting as the configuration of the custom guestinfo variable is not permanently stored. What I mean by this is the configuration is only persisted while the VM is running. This is interesting because if you have runtime information that potentially could change, this allows you to dynamically present this information to the guestOS. One use case could be set for management VMs such vCenter running in a VM and you wanted to know at any given time which ESX(i) host is currently managing it. This can be trivial with vCenter access, but what if the service is offline but the virtual machine is still running? Using the classic ESX Service Console command vmware-cmd you can loop through all powered on VMs and set some custom variables for example the current hostname of the ESX host managing the VM and version it is currently on.

The syntax for the command would be the following:

vmware-cmd [VM_VMX_PATH] setguestinfo [VARIABLE_NAME] [VARIABLE_VALUE]

e.g
vmware-cmd /vmfs/volumes/4a48004d-f9af7fa0-5bbf-003048d9586b/scofield/scofield.vmx setguestinfo hypervisor.hostname $(hostname)

Note: Notice, you do not need to append the keyword guestinfo, you just need to specify the variable name. Once you are in the guestOS, to access the custom variable, you will need to specify the full name which should be "guestinfo.[VARIABLE]"

As you can see this can be tedious to set for each and every VM, so let's automate this using a script that will go through all powered on VMs and set two custom variables called "hypervisor.hostname" which will set the current hostname of the ESX host and "hypervisor.build" which will retrieve the build of your ESX. To further automate this since you might have multiple ESX hosts running in a DRS cluster, you would store this script in a shared storage (VMFS and/or NFS) and setting up a cronjob that would run at some interval to always provide the latest information to the guestOS.

Here is an example of a shell scrip that does exactly that:

#!/bin/bash

IFS=$'\n'
for VM in $(vmware-cmd -l);
do
    VM_STATE=$(vmware-cmd "${VM}" getstate | awk -F "= " '{print $2}')
    if [ "${VM_STATE}" == "on" ]; then
        echo "Setting info for ${VM}"
        vmware-cmd "${VM}" setguestinfo hypervisor.hostname "$(hostname)"
        vmware-cmd "${VM}" setguestinfo hypervisor.build "$(vmware -v)"
    fi
done
unset IFS

You will want to save this script to your ESX host and make sure the script has executable permissions for it to execute. You will also create a cronjob based on how often you want the script to run and this needs to be configured for every ESX host that you would like to take part in this update.

Here is an example of running the script every hour:

0 * * * * /vmfs/volume/shared-storage/setGuestInfo.sh > /tmp/out

Note: I redirect the output to /tmp/out to ensure that script is in fact working and once you have, you can remove that all together.

Here is an example of extracting these two custom variables on both a Linux and Windows VM:

As I mentioned earlier, option 2 does not persist these custom variables with respect to the VM's configuration file. The variable configuration only takes affect when the VM is powered on and once it is powered off, the information is discarded. To support ESXi, you can use the various vSphere SDKs (vSphere SDK for Perl, PowerCLI, etc) but what you will find is that the behavior of setting this attribute is similar to that of the local vmware-cmd on classic ESX, it does not append the entry into the .vmx configuration file.

Here is a vSphere SDK for Perl script called addVMAdvParamOption.pl that can be used to guestinfo parameter for a given VM. Here is an example of this script:

If you are using vMA, you can also use the vCLI's vmware-cmd utility to set this variable. The format is slightly different than that of the classic ESX's vmware-cmd. Here is an example of this:

Notice, the variable name must include "guestinfo." keyword before specifying the variable name.

If you wanted to persist these configurations, you will need to adjust your provision workflow to append the variables into the VM's .vmx configuration file.

More from my site

  • OVF Runtime Environment
  • Quick Tip - Upgrading VMware Tools for Nested ESXi 6.0
  • Quick Tip - Suppress in-guest VMware Tools update notifications
  • Quick Tip - Using PowerCLI to query VMware Tools Configuration at scale 
  • New detailed GuestOS data in vSphere 8.0 Update 2

Categories // Automation, OVFTool, vSphere Tags // guestinfo, vmtoolsd, vmware tools, vmware-cmd

Comments

  1. *protectedMario Grunert says

    01/17/2011 at 4:19 pm

    I was wondering how the customization process (by quickprep or sysprep) is working, does it use this method to pass the settings into the VM ?

    Or does it use a floppy or a cdrom media ? I would like to have some insight into this.

    Reply
  2. *protectedWilliam says

    01/17/2011 at 5:40 pm

    @Mario,

    Please take a look at this thread - http://communities.vmware.com/thread/299305?tstart=0

    Reply
  3. *protectedDave says

    02/23/2011 at 6:01 pm

    My linux guests do not seem to have either vmware-toolbox-cmd or vmtoolsd. I have the following rpm installed (rpm -qa | grep VMware):

    VMwareTools-8194-236512

    and I know that I am running vmware tools (/etc/init.d/vmware-tools status):

    vmware-guestd is running

    So do you know why I don't have the binaries that you mention in this article?

    Reply
  4. *protectedWilliam says

    02/23/2011 at 6:04 pm

    @Dave,

    You're probably running the older version of tools, try type "vmware" and then hit "tab" button, you should see it auto-complete and there should be a utility with "toolbox" in the name, I forget off hand what it's called.

    Reply
  5. *protectedYacine says

    03/06/2012 at 6:21 pm

    Hi,

    Concerning the command

    vmware-cmd [VM_VMX_PATH] setguestinfo [VARIABLE_NAME] [VARIABLE_VALUE]

    as I understood, you run it on the ESX box, right?
    However I have vmware-cmd on my linux guest after installing VMware Tools, so is there a way to use it from the guest so that the guest actually gets the host to add his name (or any other unique info) ?

    thanks

    Reply
  6. *protectedsanky says

    04/09/2012 at 6:10 am

    Hi ,

    I want to know whether my guestos is running on a ESX/ESXi machine or on a workstation ? Can you please suggest a way out to identify this through my application's C Code.

    Thanks

    Reply
  7. *protectedAnonymous says

    09/27/2012 at 5:14 pm

    Hi, i need to do the opposite - to extract information about all guests from the host OS. Is that possible?
    Thanks

    Reply
  8. *protectedAnonymous says

    10/24/2012 at 6:07 pm

    Hi,

    Just wanted to try you manual out but aint working, f.e. I have the in the vmx file
    tools.syncTime = "FALSE"

    if I type on the linux guest
    vmware-rpctool "info-get guestinfo.tools.syncTime"
    No value found
    vmware-rpctool "info-get tools.syncTime"
    Invalid key name supplied

    Am I doing something wrong?

    txs in advance

    Martin

    Reply
    • *protectedWilliam says

      11/06/2012 at 5:01 pm

      tools.syncTime is a default .VMX parameter so you will not be able to view the variable. This method only works if you SET your own guestinfo.[variable]

      Reply
  9. *protectedAnonymous says

    11/16/2012 at 2:53 pm

    vmware-cmd doesn't exist with version 5 of ESX..... could you posibly translate this script into vim-cmd please?

    Reply
    • *protectedWilliam says

      11/18/2012 at 5:35 pm

      There is a "remote" version of vmware-cmd which is part of the vCLI that supports ESX(i)

      Reply
  10. *protectedBuy CNC Machine says

    02/16/2013 at 10:05 am

    I recall studying the introduction of cast and wrought iron into construction eons ago when I was a lowly undergraduate in architecture. Iron Bridge was one of the artifacts that I had to learn to recognise. That work really was the bleeding edge of the time. Fascinating stuff.:-)

    Reply
  11. *protectedAnonymous says

    05/03/2013 at 7:51 am

    Hello,

    regarding setting guestinfo the hard-coded way in the virtual machine's .vmx configuration file.

    You are stating:

    "You can use the vSphere Client to add this custom attribute while the virtual machine is powered off or you can manually edit the .vmx configuration file."

    Manually editing via ESXi host shell works for me, however I cannot edit the guestinfo.* parameters via vSphere Client 5.0 (ESXi 5.0).

    After adding "guestinfo.whatever" with a dummy value via vSphere, the parameter is simply ignored, i.e. the .vmx file is not updated with it.

    Do I have to configure something to make it work, or it is per design that one cannot set the guestinfo.* params via vSphere Client.

    BR, Ondrej.

    Reply
  12. *protecteddoofy says

    05/16/2014 at 7:50 pm

    where do you find the vmtoolsd options and parameters ? this info is useless for esx 5.5 .. please update it!

    Reply
  13. *protectedSharath Shanth says

    08/06/2014 at 9:05 am

    Hi,
    How to get the guest OS IP address from the esxi host?

    Thanks
    -sharath

    Reply
  14. *protectedMichael SIlveus says

    12/10/2014 at 6:02 pm

    Hi William,

    Is there a way to retrieve the ESXI host physical MAC addresses from inside the guest OS? I have searched high and low but am coming up empty. We are trying to come up with a scheme of node locking our software.

    Thanks,
    Mike

    Reply
    • William Lam says

      12/11/2014 at 1:01 am

      Please refer to the blog post, it shows how you how can do it for IP Address and you can do it for anything else as long as you can set it into the guestOS. If you're not doing the above, then no, you can not which is the point of isolating the VMs.

      Reply
  15. *protectedFT says

    02/25/2015 at 9:45 pm

    on VMA 5.5 Use :

    esxcli --server $vc --vihost $vhost1 vm process list|awk -F: '/Config File/{print $2}'
    To get a list of VM with a path usable

    Use:
    vmware-cmd -h $vhost4 "${VM}" setguestinfo hypervisor.hostname "$vhostname"

    vhost1 being the IP of the ESX and vhost4 being the FQDN (actual name of the ESX as showing)

    vhost1/vhost4 must resolve properly on the /etc/host

    Reply
    • *protectedFrancois TURI says

      02/25/2015 at 9:57 pm

      (VMA)
      Also:https://communities.vmware.com/message/2184934

      vhostSerial=$(esxcli --server $vc --vihost $vhost1 hardware platform get|awk -F: '/Serial/{print $2}')

      vmware-cmd -h $vhost4 "${VM}" setguestinfo guestinfo.hypervisor.Serial "$vhostSerial"

      (Linux VM)
      # vmtoolsd --cmd "info-get guestinfo.hypervisor.Serial"

      Reply
  16. *protectedKris says

    07/07/2015 at 2:24 pm

    I was seriously trying this option to get the host name and realized lately that this post is way back in 2011 and tested in ESX 4. This command is not working in ESXi 5.x

    Reply
    • William Lam says

      07/07/2015 at 5:34 pm

      I assume you're asking about 'vmware-cmd' which only exists in classic ESX, with ESXi this command no longer exists but you can use the vSphere API (an example in the blog post uses Perl SDK) to set the VM Advanced Options. For any flavors of ESXi 3.x, 4.x, 5.x 7 6.x you'll need to use the API which is available through any of the SDKs or CLI's like PowerCLI for example.

      Reply
      • *protectedAboubacar Diare says

        05/11/2016 at 11:37 am

        Hey William.

        Thanks for this great post. I was able to get this working using VI API with PowerCLI commands. However, for my solution to be viable I really need an equivalent to vmware-cmd that runs inside the ESXi host. Is there any such commands? I looked at vim-cmd but there wasn't much there. Having to use vSphere API forces some outside orchestrator to be in the mix when this information exchange could be limited to the host and guest only if a facility like vmware-cmd could be added back in.

        Alternatively, is there a way to run powercli tools from inside an ESXi server? Not ideal but thought I'd ask.

        Reply
  17. *protectedConrad says

    10/11/2015 at 1:03 am

    I was wondering where can locate my Windows files on my Mac. VMWare will no longer allow me to start up Windows. Every time I start up Windows it asks me to activate Windows and when I click yes it says it's already activated.

    Reply
  18. *protectedRenan Vasconcelos says

    05/05/2016 at 5:58 pm

    hello I run the command to get hypervisor information and an error was returned.
    the version of my ESX 5.5

    Reply
    • *protectedRenan Vasconcelos says

      05/05/2016 at 6:08 pm

      performed exactly the image 4 command

      Reply
  19. *protectedBill Scott (NTTData, Formally Dell Services) says

    11/22/2016 at 3:23 pm

    William,

    Been a while...
    Took a swag at updating this using the ESXi console.
    I have it using vim-cmd and looping through the running VM;s on the system, but for the life of me I cannot get a vmtoolsd --cmd "info-get guestinfo. to work! I have tried for any standard value and nothing works! Any ideas why?

    Oh! below is the code I wrote (Im sure it could be critiqued/enhanced...):
    clear
    #Get a list of all running VM's on the host
    vim-cmd vmsvc/getallvms > /tmp/runningVMs
    #Delete the first line in the output (Header information)
    sed -i '1d' /tmp/runningVMs
    lcount=$(wc -l < /tmp/runningVMs)
    echo "Number of entries: $lcount"
    until [ $lcount -eq 0 ]
    do
    #Get the first VM's Identifyer
    VM_ID=$(awk 'NR==1{print $1; exit}' /tmp/runningVMs)
    echo "VM ID: $VM_ID"
    #Get Guest Details
    VM_FileDetails=$(vim-cmd vmsvc/get.filelayout $VM_ID | awk -F 'vmPathName = "' '{print $2}')
    #Set pattern variable to look for
    cBracket="]"
    sBracket="["
    #Volume where VM's vmx (Configuration File) is located.
    VM_Volume=$(echo $VM_FileDetails | awk -F "$cBracket" '{print $1}')
    VM_Volume=$(echo $VM_Volume | awk -F "$sBracket" '{print $2}')
    #Folder/File where vmx file is located.
    VM_FF=$(echo "$VM_FileDetails" | awk -F "$cBracket " '{print $2}')
    VM_FF=$(echo "$VM_FF" | awk -F "vmx" '{print $1}')
    VM_FF="$VM_FF""vmx"
    VM_FF=${VM_FF//$'\n'/}
    #Check if hypervisor.host entry exists
    hventry=$(grep 'hypervisor.hostname' "/vmfs/volumes/$VM_Volume/$VM_FF")
    hventry=${#hventry}
    if [ $hventry -gt 0 ]
    then
    echo "Found"
    sed -i "/hypervisor.hostname/c \hypervisor.hostname = '$(hostname)'" "/vmfs/volumes/$VM_Volume/$VM_FF"
    else
    echo "Not Found"
    sed -i "$ a hypervisor.hostname = '$(hostname)'" "/vmfs/volumes/$VM_Volume/$VM_FF"
    fi
    sed -i '1d' /tmp/runningVMs
    #Cleanup
    unset VM_ID
    unset VM_FileDetails
    unset VM_Volume
    unset VM_FF
    unset hventry
    lcount=$(wc -l < /tmp/runningVMs)
    echo "Entry: $lcount"
    done

    If anyone can help me with why I am not able to get any results from vmtoolsd I would greatly appreciate it!
    I am running ESXi 6 and the latest tools.

    Reply
  20. *protectedHisham says

    06/06/2018 at 2:26 am

    How i get MoRef id from within the VM?

    Is the value i set using ' vmtoolsd --cmd "info-Set guestinfo.test testkey" 'command will loose after restart of the vm ?

    Reply
  21. *protectedManas says

    07/08/2021 at 9:44 am

    This procedure not showing about the ESXi IP Address or Host name information from my Windows OS.

    Reply
  22. *protectedmikeschinkel says

    10/07/2021 at 6:16 pm

    I am wondering if there is an easy way to get JSON output when running this?

    vmtoolsd --cmd "info-get guestinfo.ovfenv"

    Specifically I'm wondering if `vmtoolsd` can do it rather than having to convert it using another utility?

    Reply
    • William Lam says

      10/07/2021 at 7:32 pm

      No, the format is XML which is format OVF properties are coming in. You can use any language to do conversion 🙂

      Reply
  23. *protectedShilpi Mitra says

    07/28/2022 at 4:34 am

    This is a very informative post. Thank you Willian for writing this. When I am trying this command for getting hostname it is giving no values. My understanding is this needs custom value to be set before for retriving in this way.
    Is there any way in which we can query hostname or hostid on which the VM resides from within the VM?

    Reply

Trackbacks

  1. Unix:How to get CPU info on a vmware guest – Unix Questions says:
    10/25/2015 at 12:29 am

    […] http://www.virtuallyghetto.com/2011/01/how-to-extract-host-information-from.html […]

    Reply
  2. vSphere Datasets - New Virtual Machine Metadata Service in vSphere 8 says:
    09/21/2022 at 9:28 am

    […] metadata between the vSphere Management layer and the guest operating system was to use either guest variables (guestinfo) or the OVF runtime […]

    Reply
  3. Разнообразные выноски по теме VMware – VSAN – mechanicusjr says:
    01/30/2023 at 3:54 am

    […] https://williamlam.com/2011/01/how-to-extract-host-information-from.html […]

    Reply

Thanks for the comment!Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search

Thank Author

Author

William is Distinguished Platform Engineering Architect in the VMware Cloud Foundation (VCF) Division at Broadcom. His primary focus is helping customers and partners build, run and operate a modern Private Cloud using the VMware Cloud Foundation (VCF) platform.

Connect

  • Bluesky
  • Email
  • GitHub
  • LinkedIn
  • Mastodon
  • Reddit
  • RSS
  • Twitter
  • Vimeo

Recent

  • VMware Flings is now available in Free Downloads of Broadcom Support Portal (BSP) 05/19/2025
  • VMUG Connect 2025 - Minimal VMware Cloud Foundation (VCF) 5.x in a Box  05/15/2025
  • Programmatically accessing the Broadcom Compatibility Guide (BCG) 05/06/2025
  • Quick Tip - Validating Broadcom Download Token  05/01/2025
  • Supported chipsets for the USB Network Native Driver for ESXi Fling 04/23/2025

Advertisment

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright WilliamLam.com © 2025