How to find ip address of Linux KVM guest virtual machine

ADVERTISEMENTS



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

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

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.

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



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 virsh list --name | while read n do [[ ! -z $n ]] && virsh domifaddr $n done

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



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

