How to find ip address of Linux KVM guest virtual machine

How do I find out an IP address of Linux KVM guest from Linux host itself? I wanted to ssh into KVM guest VM. How can I find the IP address of a KVM Virtual Machine Guest on a CentOS Linux 7.x/8.x server?

There are various ways to get the IP address of KVM guest from the host. If you are using virbr0 (default bridge) use the virsh command. It is also possible to use the combination of arp command or check DHCP server log if you are using a dedicated DHCPD over a bridged network such as br0 that give access to physical LAN.

ADVERTISEMENTS

Steps to find the ip address of Linux KVM guest virtual machine

  1. Open the terminal app or login using ssh to host server
  2. Get the network list: virsh net-list
  3. Type the command: virsh net-dhcp-leases networkNameHere

Let us see steps in details.

Method 1 – Find the IP addresses of VMs in KVM with virsh

Type the following command to list network:
# virsh net-list
# virsh net-info default
# virsh net-dhcp-leases default

Linux find the ip address of Linux KVM guest virtual machine
Here is another session for my centos-8-cloud VM:
$ virsh net-dhcp-leases default

 Expiry Time           MAC address         Protocol   IP address          Hostname         Client ID or DUID
-----------------------------------------------------------------------------------------------------------------
 2020-08-23 18:53:39   52:54:00:33:0c:ee   ipv4       192.168.122.32/24   centos-8-cloud   01:52:54:00:33:0c:ee

You can type the ssh command to log in to OpenBSD VM named nixcraft-openbsd with IP address 192.168.122.124:
$ ssh vivek@192.168.122.124
For CentOS 8 vm:
$ ssh 192.168.122.32
$ ssh vivek@192.168.122.32

Method 2 – Get the IP address of Linux KVM guest using domifaddr

Find network interfaces’ addresses for a running domain called freebsd11.1:
$ virsh list
$ virsh domifaddr freebsd11.1

Combine both virsh and bash shell while loop as follows:

virsh list --name | while read n 
do 
  [[ ! -z $n ]] && virsh domifaddr $n
done
Getting a Virtual Machines IP Address from virsh

Method 3 – Use arp command to get a KVM guest’s IP address

Use the virsh command to find out the mac address of domain:
$ virsh list
$ virsh dumpxml VM_NAME | grep "mac address" | awk -F\' '{ print $2}'
$ arp -an | grep 52:54:00:ce:8a:c4

How to find ip address in linux virtual machine

In short use any one of the following virsh command to get the ip address

$ virsh list
$ virsh domifaddr rhel7

Say hello to Libvirt NSS module

When it comes to managing guests and executing commands inside them, logging into guest operating system and doing the job is convenient. Users are used to ssh in this case. Ideally,
ssh user@ip-Address-Here
But depending on virtual network configuration it might not be always possible. For instance, when using libvirt NATed network it’s dnsmasq (spawned by libvirt) who assigns IP addresses to domains. But by default, the dnsmasq process is then not consulted when it comes to host name translation. Users work around this problem by configuring their libvirt network to assign static IP addresses and maintaining /etc/hosts file in sync. But this puts needless burden onto users. This is where NSS module comes. Install it using the yum command/dnf command/apt command/apt-get command:
$ sudo yum install libvirt-nss ## RHEL/CentOS/Fedora ##
$ sudo apt install libnss-libvirt ## Debian/Ubuntu ##

Make sure /etc/nsswitch.conf updated as follows. Verify it using the grep command:
$ grep -w 'hosts:' /etc/nsswitch.conf

files libvirt libvirt_guest dns mymachines

Now list vm and try to use the ping command or ssh command to resolve those VMs via ibvirt NSS module:
$ virsh list
$ ping vm-name-here
$ ssh user@vm-name-here

How to find the IP address of a KVM Virtual Machine

Finding the IP address of a KVM Virtual Machine/Guest using CLI

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
1 comment… add one
  • JUAN ENCISO CONDENA Sep 28, 2020 @ 15:18

    Great post. Very simple and didactic. Thanks for share your tips

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.