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 this blog post for showing me the way:
and
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
If you install the libvirt NSS plugin, then you won’t even need to do that!
https://wiki.libvirt.org/page/NSS_module
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.
`domifaddr` does not always work. I wrote a script that’s available at https://github.com/earlruby/create-vm/blob/master/get-vm-ip which uses this to get the IP:
“`
HOSTNAME=[your vm name]
MAC=$(virsh domiflist $HOSTNAME | awk ‘{ print $5 }’ | tail -2 | head -1)
arp -a | grep $MAC | awk ‘{ print $2 }’ | sed ‘s/[()]//g’
“`
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.