I have several VMs running on Ubuntu 9.10 via KVM+libvirt. I want to be able to find out the IP address that has been assigned to each host without physically opening a physical "console" to each machine and invoking ifconfig
.
Consider:
rascher@localhost:~$ virsh -c qemu:///system list --all Connecting to uri: qemu:///system Id Name State ---------------------------------- 1 machine1 running 2 machine2 running - machine3 shut off
My network configuration looks like:
<network>
<name>default</name>
<uuid>1be...</uuid>
<forward mode='route' dev="eth0"/>
<bridge name='virbr0' stp='on' forwardDelay='0' />
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254' />
</dhcp>
</ip>
</network>
So how can I get a listing which says:
machine1 IP address = 192.168.122.16 machine2 IP address = 192.168.122.238 ...
I played with arp
:
rascher@localhost:~$ arp Address HWtype HWaddress Flags Mask Iface 192.168.122.238 ether 00:16:36:00:61:b0 C virbr0 192.168.122.16 ether 00:16:36:52:e8:9c C virbr0 ...
But this doesn't map to a virtual machine's ID.
Is there some tool (via the command line, virsh
or virt-*
) I can ascertain this information? Or do I need to have some fancy script which runs on each individual VM, checks its own IP, and reports it back to the host OS?
This feature was requested a long time ago. Now libvirt supports it by providing two new commands: domifaddr and net-dhcp-leases
In a different scenario:
libvirt uses dnsmasq to provide DHCP to the guests, so you could trawl /var/log/daemon.log or dig through the leases file in /var/lib/libvirt to get an IP to hostname mapping.
So, when investigating this, I found that libvirt uses dnsmasq in order to do DHCP and DNS for guest OSes.
And dnsmasq will set the hostname in the hosts's DNS table based on whatever hostname it receives from the guest.
So in accordance with these instructions and a lot of googling, I simply needed to create and add this to /etc/dhclient.conf:
Now, from my host OS, I can
ping machine1.
Does anyone know why I need to add the trailing "." in order for the DNS entry to resolve? How can I change this?
I had the same problem so I created the following script:
Lars Kellogg-Stedman has created a set of scripts to automate some of this process. He calls it 'virt-utils'.
He describes it in his blog post here: http://blog.oddbit.com/2013/10/04/automatic-dns-entrie/
He also has a github with some of the scripts he wrote, here:
https://github.com/larsks/virt-utils
You can basically just run this:
and you will get a listing of each virtual machine by it's "domain name" inside libvirt's virtual-machine-manager. For example, on my machine I have 3 vms running.
Note, this is not the 'hostname' the VM itself is using, but for a large number of use-cases, it will be 'good enough' and solves the problem of having to 'ifconfig' from within each VM in dhcp land.
Lars' blog posting also shows a way for this to 'auto update' your own /etc/hosts file as libvirt starts and/or stops new VMs. This enables you to do things like ssh myname@fedora20vm or ssh myname@debian6vm without having to find the 192.168.122.x addresses by hand.
I have added a few very minor enhancements, like a script to spit out some ~/.ssh/config options (very very handy for using github on VMs, via Agent Forwarding), here:https://github.com/donbright/virt-utils (appears to be deleted?)I'd also like to note that the method of editing dhclient.conf to 'send host-name xxxxx' only works on systems that actually use dhclient.conf in a standard manner. Mageia, for example, has an unusual setup of how it's dhclient works, so the simple instructions wont necessarily work. However, with Lars' method, it works regarldess of the guest OS'es dhcp setup, because he is not relying on the VM to send it's hostname - he is using the 'domain names' within libvirt's machine manager.
at least on fedora you can get that information this way:
cat /var/lib/libvirt/dnsmasq/default.leases
has an output similar to:
1412006226 52:54:00:fe:b3:c0 192.168.122.117 coreos0 01:52:54:00:fe:b3:c0
although that is a bit more than you asked for
On Ubuntu dnsmasq is used to provide DNS and DHCP services to the VMs. The dnsmasq processes on the host store their leases in this file:
This is a normal text file and the lines in it might look similar to this here:
The fields of interest to you are the third and the fourth column: the third field contains the IPv4 address of the VM and the fourth field either contains an asterisk or the hostname of the VM. This depends on the DHCP reply send by the guest to the dnsmasq service process.
You can change the
default
network definition, map MAC to IP within the xml:Once you start a guest, you can list all guest's MAC address via
According to the last byte of a MAC address, you can infer the IP address of a guest.