首页 文章

libvirt / kvm - 如何在桥接网络上获取虚拟机的IP地址?

提问于
浏览
-1

从libvirt / KVM主机,我想获得通过桥接适配器连接到网络的正在运行的虚拟机的IP地址 .

重要提示:我知道如何通过libvirt / kvm默认NAT网络连接虚拟机时获取IP地址 - 这就是互联网上针对此问题描述的大多数解决方案 .

但我无法找到通过网桥连接的虚拟机的IP地址 .

从虚拟机内部:

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

网络正在运行:

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:~#

从虚拟机主机:

(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转储:

(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$

1 回答

  • -1

    vm如何获取IP地址?如果它是DHCP - 您应该转到DHCP数据,并查找接口的地址(即:52:54:00:d0:1e:27) .

    您可以尝试以下方法:

    grep '52:54:00:d0:1e:27' /var/lib/libvirt/dnsmasq/default.hostsfile
    

    您可以在/var/lib/libvirt/dnsmasq/default.conf中查看dhcp-hosts文件,以查找此文件的位置 .

    编辑:

    如果是桥接网络,则主机无法直接知道访客的IP . 您可以尝试一些方法,例如:

    • 在主机的arp输出中查找MAC地址 .

    • ssh到guest虚拟机并使用ifconfig打印IP .

相关问题