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.
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
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
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
Finding the IP address of a KVM Virtual Machine/Guest using CLI
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
- RSS feed or Weekly email newsletter
- Share on Twitter • Facebook • 1 comment... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Great post. Very simple and didactic. Thanks for share your tips