If you are starting virtual machines via libvirt, and you have attached them to the default network, there is a very simple method you can use to determine the address assigned to your running instance:

  • Libvirt runs dnsmasq for the default network, and saves leases in a local file (/var/lib/libvirt/dnsmasq/default.leases under RHEL).
  • You can get the MAC address assigned to a virtual machine by querying the domain XML description.

Putting this together gets us something along the lines of:


# Get the MAC address of the first interface.
mac=$(virsh dumpxml $1 |
  xml2  |
  awk -F= '$1 == "/domain/devices/interface/mac/@address" {print $2; exit}')

# Get the ip address assigned to this MAC from dnsmasq
ip=$(awk -vmac=$mac '$2 == mac {print $3}' /var/lib/libvirt/dnsmasq/default.leases )
echo $ip


This uses xml2 to transform the XML description into something more amendable to processing in a shell script. You could accomplish much the same thing using some sort of XPath based tool. For example:

mac=$(virsh dumpxml $1 |
  xmllint --xpath //interface'[1]/mac/@address' - |
  sed 's/.*="\([^"]*\)"/\1/'

xmllint is part of libxml2.