Home KVM How To Find The IP Address Of A KVM Virtual Machine In Linux

How To Find The IP Address Of A KVM Virtual Machine In Linux

Different Ways To Identify KVM Virtual Machine's IP Address In Linux

By sk
Published: Last Updated on 21.5K 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 determine the IP address of the KVM guest using ip command. However, it is also possible to identify a KVM Virtual machine 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
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
Find the IP address of KVM virtual machines using virsh

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
Find the IP address of KVM virtual machines using virsh domifaddr command

B. Determine 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
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
Find the IP address of KVM virtual machines using arp command

C. View 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
Find the IP address of KVM virtual machines using Virt-manager

These are a few way to identify a KVM virtual machine's IP address in Linux. the Hope this helps.

Featured image by Brett Sayles from Pexels.

You May Also Like

4 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
David May 6, 2022 - 12:16 am

Hello SK,

I use all of methods above but none of them works.
1. virsh net-dhcp-leases default
result: empty
2. virsh domifaddr myVM
result: empty
3. arp -n -a
none of the output ip address is of myVM
4. click the bulb toolbar icon
no response, or no ip address shown.

would you please help me out?
Thanks a lot!
David

Reply
sk May 6, 2022 - 11:46 am

It’s strange. If the VM is running, you should definitely see the IP address using any one of the above methods. Make sure you the VM is running and try again.

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. By using this site, we will assume that you're OK with it. Accept Read More