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.
Mario Grunert says
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.
William says
@Mario,
Please take a look at this thread - http://communities.vmware.com/thread/299305?tstart=0
Dave says
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?
William says
@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.
Yacine says
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
sanky says
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
Anonymous says
Hi, i need to do the opposite - to extract information about all guests from the host OS. Is that possible?
Thanks
Anonymous says
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
William says
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]
Anonymous says
vmware-cmd doesn't exist with version 5 of ESX..... could you posibly translate this script into vim-cmd please?
William says
There is a "remote" version of vmware-cmd which is part of the vCLI that supports ESX(i)
Buy CNC Machine says
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.:-)
Anonymous says
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.
doofy says
where do you find the vmtoolsd options and parameters ? this info is useless for esx 5.5 .. please update it!
Sharath Shanth says
Hi,
How to get the guest OS IP address from the esxi host?
Thanks
-sharath
Michael SIlveus says
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
William Lam says
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.
FT says
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
Francois TURI says
(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"
Kris says
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
William Lam says
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.
Aboubacar Diare says
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.
Conrad says
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.
Renan Vasconcelos says
hello I run the command to get hypervisor information and an error was returned.
the version of my ESX 5.5
Renan Vasconcelos says
performed exactly the image 4 command
Bill Scott (NTTData, Formally Dell Services) says
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.
Hisham says
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 ?
Manas says
This procedure not showing about the ESXi IP Address or Host name information from my Windows OS.
mikeschinkel says
I am wondering if there is an easy way to get JSON output when running this?
Specifically I'm wondering if `vmtoolsd` can do it rather than having to convert it using another utility?
William Lam says
No, the format is XML which is format OVF properties are coming in. You can use any language to do conversion 🙂
Shilpi Mitra says
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?