3

From the libvirt / KVM host, I want to get the IP address of running virtual machines that are connected to the network via a bridged adapter.

Important: I know how to get the IP address when the virtual machine is connected via the libvirt / kvm default NAT network - and that is what most solutions on the Internet to this question describe.

I can see no way however to find the IP address of virtual machines connected via a bridge.

From within virtual machine:

root@zvm:~# ifconfig -a
ens3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.133  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::5054:ff:fed0:1e27  prefixlen 64  scopeid 0x20<link>
        ether 52:54:00:d0:1e:27  txqueuelen 1000  (Ethernet)
        RX packets 36  bytes 18540 (18.5 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 37  bytes 4128 (4.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 334  bytes 24670 (24.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 334  bytes 24670 (24.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

The network is working:

root@zvm:~# curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.au/?gfe_rd=cr&amp;dcr=0&amp;ei=u-uJWqTyJPLc8weU9JHABg">here</A>.
</BODY></HTML>
root@zvm:~# curl www.google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="http://www.google.com.au/?gfe_rd=cr&amp;dcr=0&amp;ei=ge6JWv_0Co7r8wf6gKGwBQ">here</A>.
</BODY></HTML>
root@zvm:~# 

From the virtual machine host:

(venv2.7) ubuntu@kvmhost:/opt/foo$ arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.122.35           ether   52:54:00:61:1b:f2   C                     virbr0
192.168.122.210                  (incomplete)                              virbr0
192.168.1.1              ether   30:b5:c2:08:91:e3   C                     br0
192.168.122.10           ether   52:54:00:ee:c8:2a   C                     virbr0
192.168.122.80           ether   52:54:00:18:93:6e   C                     virbr0
192.168.1.126            ether   ac:87:a3:00:b1:63   C                     br0
192.168.122.21           ether   52:54:00:5f:3a:70   C                     virbr0
192.168.122.182                  (incomplete)                              virbr0
192.168.1.1                      (incomplete)                              enp3s0
192.168.122.32                   (incomplete)                              virbr0
192.168.122.112          ether   52:54:00:82:7d:75   C                     virbr0
192.168.122.183          ether   52:54:00:a4:eb:ba   C                     virbr0
192.168.122.36           ether   52:54:00:b1:e4:83   C                     virbr0
192.168.122.139                  (incomplete)                              virbr0
192.168.122.209                  (incomplete)                              virbr0
192.168.1.140                    (incomplete)                              br0
(venv2.7) ubuntu@kvmhost:/opt/foo$

ifconfig:

(venv2.7) ubuntu@kvmhost:/opt/foo$ ifconfig -a
br0       Link encap:Ethernet  HWaddr d2:d6:16:58:19:e0
          inet addr:192.168.1.139  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::d0d6:16ff:fe58:19e0/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:576693 errors:0 dropped:0 overruns:0 frame:0
          TX packets:381883 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:399735319 (399.7 MB)  TX bytes:39484740 (39.4 MB)

enp3s0    Link encap:Ethernet  HWaddr fc:aa:14:a9:95:bf
          inet addr:192.168.1.140  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:577654 errors:0 dropped:153 overruns:0 frame:0
          TX packets:384437 errors:1 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:407978078 (407.9 MB)  TX bytes:39670335 (39.6 MB)

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:814 errors:0 dropped:0 overruns:0 frame:0
          TX packets:814 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1
          RX bytes:168110 (168.1 KB)  TX bytes:168110 (168.1 KB)

virbr0    Link encap:Ethernet  HWaddr 52:54:00:22:5c:7b
          inet addr:192.168.122.1  Bcast:192.168.122.255  Mask:255.255.255.0
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:901 errors:0 dropped:0 overruns:0 frame:0
          TX packets:536 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:76426 (76.4 KB)  TX bytes:175557 (175.5 KB)

virbr0-nic Link encap:Ethernet  HWaddr 52:54:00:22:5c:7b
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

vnet0     Link encap:Ethernet  HWaddr fe:54:00:d0:1e:27
          inet6 addr: fe80::fc54:ff:fed0:1e27/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:82 errors:0 dropped:0 overruns:0 frame:0
          TX packets:325 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:7776 (7.7 KB)  TX bytes:69359 (69.3 KB)

wlp2s0    Link encap:Ethernet  HWaddr d0:7e:35:69:cf:b2
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

(venv2.7) ubuntu@kvmhost:/opt/foo$

XML dump of the virtual machine from the host:

(venv2.7) ubuntu@kvmhost:/opt/foo$ virsh dumpxml zvm
<domain type='kvm' id='41'>
  <name>zvm</name>
  <uuid>4e544a34-5a31-43ac-a2af-dbac612110d4</uuid>
  <metadata>
    <kvirt:info xmlns:kvirt="kvirt">
      <kvirt:creationdate>18-02-2018 21:05</kvirt:creationdate>
      <kvirt:profile>zesty</kvirt:profile>
      <kvirt:template>zesty-server-cloudimg-amd64.img</kvirt:template>
      <kvirt:plan>kvirt</kvirt:plan>
    </kvirt:info>
  </metadata>
  <memory unit='KiB'>1048576</memory>
  <currentMemory unit='KiB'>1048576</currentMemory>
  <vcpu placement='static'>1</vcpu>
  <resource>
    <partition>/machine</partition>
  </resource>
  <os>
    <type arch='x86_64' machine='pc-i440fx-2.5'>hvm</type>
    <boot dev='hd'/>
    <boot dev='cdrom'/>
    <bootmenu enable='yes'/>
  </os>
  <features>
    <acpi/>
    <apic/>
    <pae/>
  </features>
  <cpu mode='custom' match='exact'>
    <model fallback='allow'>Westmere</model>
    <feature policy='require' name='vmx'/>
  </cpu>
  <clock offset='utc'/>
  <on_poweroff>destroy</on_poweroff>
  <on_reboot>restart</on_reboot>
  <on_crash>restart</on_crash>
  <devices>
    <emulator>/usr/bin/kvm-spice</emulator>
    <disk type='file' device='disk'>
      <driver name='qemu' type='qcow2'/>
      <source file='/var/lib/libvirt/images/zvm_1.img'/>
      <backingStore type='file' index='1'>
        <format type='qcow2'/>
        <source file='/var/lib/libvirt/images/zesty-server-cloudimg-amd64.img'/>
        <backingStore/>
      </backingStore>
      <target dev='vda' bus='virtio'/>
      <alias name='virtio-disk0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
    </disk>
    <disk type='file' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <source file='/var/lib/libvirt/images/zvm.ISO'/>
      <backingStore/>
      <target dev='hdd' bus='ide'/>
      <readonly/>
      <alias name='ide0-1-1'/>
      <address type='drive' controller='0' bus='1' target='0' unit='1'/>
    </disk>
    <controller type='usb' index='0'>
      <alias name='usb'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x2'/>
    </controller>
    <controller type='pci' index='0' model='pci-root'>
      <alias name='pci.0'/>
    </controller>
    <controller type='ide' index='0'>
      <alias name='ide'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/>
    </controller>
    <interface type='bridge'>
      <mac address='52:54:00:d0:1e:27'/>
      <source bridge='br0'/>
      <target dev='vnet0'/>
      <model type='virtio'/>
      <alias name='net0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
    </interface>
    <serial type='pty'>
      <source path='/dev/pts/2'/>
      <target port='0'/>
      <alias name='serial0'/>
    </serial>
    <console type='pty' tty='/dev/pts/2'>
      <source path='/dev/pts/2'/>
      <target type='serial' port='0'/>
      <alias name='serial0'/>
    </console>
    <input type='tablet' bus='usb'>
      <alias name='input0'/>
    </input>
    <input type='mouse' bus='ps2'/>
    <input type='keyboard' bus='ps2'/>
    <graphics type='spice' port='5900' autoport='yes' listen='0.0.0.0'>
      <listen type='address' address='0.0.0.0'/>
    </graphics>
    <video>
      <model type='cirrus' vram='16384' heads='1'/>
      <alias name='video0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
    </video>
    <memballoon model='virtio'>
      <alias name='balloon0'/>
      <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
    </memballoon>
  </devices>
  <seclabel type='dynamic' model='apparmor' relabel='yes'>
    <label>libvirt-4e544a34-5a31-43ac-a2af-dbac612110d4</label>
    <imagelabel>libvirt-4e544a34-5a31-43ac-a2af-dbac612110d4</imagelabel>
  </seclabel>
</domain>

(venv2.7) ubuntu@kvmhost:/opt/foo$
6
  • If the VM's IP address is allocated by dnsmasq or dhcpd, my answer here can extract it from /var/log/daemon.log (or wherever dhcp is logged on your system): unix.stackexchange.com/a/239170/7696. BTW this works for VMs with any kind of NIC (e.g. bridged or NATted) as long as they get their IP from DHCP. NOTE: if the VM host is not the same machine as the DHCP server then you'll have to run the virsh dumpxml on the VM host and the awk command to extract the IP from the log on the dhcp server.
    – cas
    Commented Feb 19, 2018 at 2:09
  • If the virtual machines are responding to mDNS or/and LLMNR queries (provided by Avahi or systemd-resolved), just ping the hostname from the same subnet. Or forget about the IP address and use the name directly. Commented Feb 19, 2018 at 10:32
  • 1
    I posted an answer to this question with scripts on a linked question. BTW I don't think this is off topic at all. This should have been maybe closed as a duplicate of this unix.stackexchange.com/questions/33191/… Commented Feb 26, 2018 at 6:22
  • 1
    No obvious multipost elsewhere in the SE sites Commented Apr 30, 2019 at 22:17
  • 1
    @roaima - I believe I found the one in question, on Stack Overflow: same subject, same day. My opinion is that it's less relevant on SO, and more relevant here, so I'll reopen this and flag the other one for closure. Duke, I'd encourage you to delete the SO version of this question.
    – Jeff Schaller
    Commented May 1, 2019 at 12:57

3 Answers 3

5

This may work for a VM guest called "pollyanna" if the host has communicated with the VM recently, so that its IP address is in the ARP cache:

arp -na | awk -v mac=$(virsh domiflist pollyanna | awk '$2=="bridge"{print $NF}') '$0 ~ " at " mac {gsub("[()]", "", $2); print $2}'

Let's split this out:

# Get the MAC for a VM guest called pollyanna
vmac=$(virsh domiflist pollyanna | awk '$2=="bridge"{print $NF}')

# List the ARP cache
arp -na |

# Match the MAC on a line like "? (192.168.1.12) at 12:34:56:78:9a:bc [ether] on ethX"
# and return the IP address. The gsub() function strips the "()" characters
awk -v mac="$vmac" '$0 ~ " at " mac {gsub("[()]", "", $2); print $2}'

Otherwise you may need to scan the entire plausible IP address range to force the IP address into the ARP cache, and then retry the lookup:

nmap -sn '192.168.1.*'    # Ugh. Every address on the 192.168.1.0/24 network
1
2

Generally speaking, when you are using a bridge, the VM is effectively not under libvirt's built-in dnsmasq control but gets it's IP from the DHCP in the bridged-to network or from internal settings. This means libvirt has no means of setting that IP or getting it.

You can either do what @roaima described above as a workaround, monitor your DHCP for IP assignments to the VM's MAC, or install an agent inside the VM which will report the details of the VM's internal configuration back to you (like ovirt-guest-agent does). The rule of thumb here is, with a bridged network, the VM behaves and is generally managed just like any other physical machine on that network

1
arp -na | grep $(virsh domiflist VM01 | tail -n +3 | awk '{print$5}' | sed '/^$/d') | cut -c 4-17

This works well for me.

To make cache in arp,

IP=`nmap -sn '192.168.200.*' | grep 'Nmap scan' | awk '{print$5}'` ; for i in $IP ; do ping $i -c 1 ; done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .