Getting a Virtual Machine’s IP Address from virsh

Ten Years later, and I finally know how to get virsh to tell me the IP address for a VM.

So, I have a virtual machine image called cfme. To launch it, I run

sudo virsh start cfme

That gets me a running virtual machine. To find the domain ID that it has been assigned:

$ sudo virsh list
 Id Name State
----------------------------------------------------
 1 cfme running

And to Find Its IP Address I use the domifaddr subcommand:

$ sudo virsh domifaddr 1
 Name MAC address Protocol Address
-------------------------------------------------------------------------------
 vnet0 52:54:00:e5:5d:e3 ipv4 192.168.122.98/24

From which I can see that the address is 192.168.122.98

Wish I knew this years ago.  Ah well.  Better late then never.

Thanks to and this blog post for showing me the way:

 

 

5 thoughts on “Getting a Virtual Machine’s IP Address from virsh

  1. For even more convenience you can add the libvirt or libvirt-guest NSS module to nsswitch.conf The former does lookups based on the guest’s configured hostname, while the latter does lookups based on the guest name. Both ultimately fetch the IP address from the same source as ‘domifaddr’

    https://wiki.libvirt.org/page/NSS_module

  2. I observed that for bridged guests, you need to use the –source agent flag, and have the requisite qemu guest agent running. I’m guessing the default source is lease, which I assume only works on the NAT network where the guests have local dhcp leases.
    For fedora and ubuntu, that agent package is named qemu-guest-agent.

  3. The magic is that virtual machines interfaces are listed than mac address is grepped. After grepping mac address we are looking our ARP table to find corresponding ip address and put it to the screen. This script will list all interfaces of VM if it has more than one.

Leave a Reply

Your email address will not be published. Required fields are marked *

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