Home VirtualizationKVM How To Find The IP Address Of A KVM Virtual Machine
find the IP address of a KVM virtual machine

How To Find The IP Address Of A KVM Virtual Machine

By sk
872 Views

Finding the IP address of a KVM Linux guest virtual machine is not a big deal. If you have console access to the virtual machine, you can easily find the IP address of the KVM guest using “ip” command. However, it is also possible to identify a KVM VM’s IP address without having to access its console. In this tutorial, we are going to see a few different ways to find the IP address of a KVM virtual machine (guest) in Linux.

Find The IP Address Of A KVM Virtual Machine

We can find the IP address of a KVM guest virtual machine either from virsh command line interface or via any KVM graphical management tools like Virt-manager.

First, we will see the command line way.

A. Find the IP address of KVM virtual machines using "virsh" command

Virsh is a command line interface for creating and managing KVM guest virtual machines. It provides the following two commands to find the virtual machine’s IP address:

  1. net-dhcp-leases,
  2. domifaddr.

1. Net-dhcp-leases

The net-dhcp-leases command retrieves the DHCP leases info for a given virtual network and/or given network interface.

Make sure your VM is running!

First, list the available virtual networks in the KVM host. To do so, run:

$ virsh net-list

Sample output:

 Name      State    Autostart   Persistent
--------------------------------------------
 default   active   yes         yes

List available virtual networks in the KVM host

As you can see, my KVM host has a virtual network named “default”. This is a private virtual network bridge created by the KVM by default. It provides its own subnet and DHCP to configure the guest’s network and uses NAT to access the host network.

Let us view the basic information for the “default” virtual network:

$ virsh net-info default

Sample output:

Name:           default
UUID:           ce25d978-e455-47a6-b545-51d01bcb9e6f
Active:         yes
Persistent:     yes
Autostart:      yes
Bridge:         virbr0

Since KVM has its own DHCP, we can find out the IP address that has been assigned to each KVM virtual machine using this command:

$ virsh net-dhcp-leases default

Sample output:

Expiry Time           MAC address         Protocol   IP address           Hostname      Client ID or DUID
---------------------------------------------------------------------------------------------------------------
 2020-07-25 18:51:53   52:54:00:42:b6:5a   ipv4       192.168.122.119/24   centos8       01:52:54:00:42:b6:5a
 2020-07-25 18:54:04   52:54:00:61:2a:88   ipv4       192.168.122.20/24    centos8uefi   01:52:54:00:61:2a:88

Find the IP address of KVM virtual machines using virsh command

You don’t have to log in to the virtual machine. From the KVM host’s terminal, we can easily find the IP addresses of all running KVM virtual machines.

2. domifaddr

The other way to find KVM virtual machine’s IP address from the command line is by querying the guest operating system's IP address using the “domifaddr” command.

First, let us get the list of running KVM guest machines:

$ virsh list

Sample output:

Id   Name           State
------------------------------
 1    centos8        running
 2    centos8-uefi   running

As you see in the output, there are two KVM guests are running! To find the IP address of a KVM virtual machine named "centos8", run:

$ virsh domifaddr centos8

Sample output:

Name       MAC address          Protocol     Address
-------------------------------------------------------------------------------
 vnet0      52:54:00:42:b6:5a    ipv4         192.168.122.119/24

Find the IP address of KVM virtual machines using virsh domifaddr command

B. Find the IP address of KVM virtual machines using "arp" command

ARP, stands for Address Resolution Protocol, is used to find the media access control address of a network neighbour for a given IPv4 Address. It manipulates or displays the kernel's IPv4 network neighbour cache. It can add entries to the table, delete one or display the current content.

If arp command is not available, install the “net-tools” package.

On Debian, Ubuntu:

$ sudo apt install net-tools

On CentOS, RHEL:

$ sudo dnf install net-tools

To find the IP address of a KVM virtual machine in Linux using “arp” command, simply run:

$ arp -n

Sample output:

Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.122.20           ether   52:54:00:61:2a:88   C                     virbr0
192.168.225.34           ether   7c:d3:0a:0c:6b:9a   C                     wlp9s0
192.168.225.1            ether   12:b0:25:2b:ec:19   C                     wlp9s0
192.168.122.119          ether   52:54:00:42:b6:5a   C                     virbr0

Find KVM virtual machine ip address using arp command

Did you notice the “virbr0” entries in the above command? It is our KVM virtual network interface, hence the IP address of the KVM VMs are 192.168.122.119 and 192.168.122.20. It is OK if there is only one or a few VMs running. But if you have many, how do you know which MAC address corresponds to the one you want? Here is where the “domiflist” and “dumpxml” commands come in handy.

Find the list of running virtual machines:

$ virsh list

Get the MAC address of any running VM, for example centos8, using command:

$ virsh domiflist centos8

Sample output:

 Interface   Type      Source    Model    MAC
-------------------------------------------------------------
 vnet0       network   default   virtio   52:54:00:42:b6:5a

Finally find the IP address associated to the above mac address:

$ arp -n | grep 52:54:00:42:b6:5a

Or,

$ arp -an | grep 52:54:00:42:b6:5a

Sample output:

? (192.168.122.119) at 52:54:00:42:b6:5a [ether] on virbr0

Find the IP address of KVM virtual machines using arp command

C. Find the IP address of KVM virtual machines using Virt-manager GUI

Virt-manager is a graphical alternative to the Virsh command line utility. If you have installed it already in your KVM host, finding the IP address of any running KVM virtual machines is incredibly easy!

Open Virt-manager application and double click on any running virtual machine. Next click on the “Show virtual hard details” icon in the menu bar. Finally click on the “NIC :xx:xx:xx” device from the list and you will see KVM VM’s IP address on the right side.

Find the IP address of KVM virtual machines using Virt-manager

Hope this helps.

Featured image by Brett Sayles from Pexels.

You May Also Like

2 comments

CG July 26, 2020 - 3:03 am

Thank you SK for all this virtualization series. I’ve been following all the articles and are very useful and informative. Thanks again

Reply
sk July 26, 2020 - 3:00 pm

You are welcome, mate.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More