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.
Table of Contents
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:
- net-dhcp-leases,
- 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
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
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
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
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
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.
These are a few way to identify a KVM virtual machine's IP address in Linux. the Hope this helps.
Related read:
Featured image by Brett Sayles from Pexels.
4 comments
Thank you SK for all this virtualization series. I’ve been following all the articles and are very useful and informative. Thanks again
You are welcome, mate.
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
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.