Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: October 30, 2024
While working on a virtual machine (VM), we often require its IP address for various reasons. These include remote access and management, network configuration, file transfers, and testing services hosted on the VM.
In this tutorial, we’ll explore how to find the IP of a Linux virtual machine. We’ll cover methods for locating it through the GUI, using commands over SSH, and when SSH is active but inaccessible through the hypervisor.
First, we discuss various commands and the GUI method that we can utilize to find the IP address of any Linux virtual machine if we already have access to it.
The hostname command in Linux is used to set or view the current hostname or domain name of the system. We can also utilize the -I flag of this command to get the IP address.
Let’s find the IP address of the Linux virtual machine by executing the hostname command in a local terminal or SSH remote session:
$ hostname -I
192.168.2.106
This command lists all the IP addresses assigned to the machine’s network interfaces, separated by spaces. We can also run the hostname -i command to find the IP associated with the machine’s hostname. However, it only works if the hostname is resolvable.
The ifconfig command in Linux is used to view and configure the parameters of network interfaces. Moreover, we can use this command to get the MAC and IP addresses of a virtual machine.
Before using the ifconfig command, we might need to install net-tools on the Linux system, as it’s a part of that package:
$ sudo apt install net-tools
After installing the net-tools, let’s find the IP address of the virtual machine by executing the ifconfig command:
$ ifconfig
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.106 netmask 255.255.255.0 broadcast 192.168.2.255
inet6 fe80::a2b2:be53:aea1:ae45 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:21:0b:f5 txqueuelen 1000 (Ethernet)
...
Consequently, we can observe that the IP address of the VM is 192.168.2.106.
As a newer and recommended alternative to ifconfig, the ip command enables users to configure and manage network settings, including assigning IP addresses, changing network interfaces, and managing the ARP cache.
Let’s execute the ip addr command in the command prompt to locate all the network interfaces and their associated IP addresses:
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
...
inet 192.168.2.106/24 brd 192.168.2.255 scope global dynamic noprefixroute enp0s3
....
Consequently, we can see the IP address of the Linux VM. We can also use the ip addr show command instead of the ip addr, as it’s the explicit version.
If we have GUI access to the VM, we can locate the IP address by navigating to Settings > Network. Then, we click the Settings icon under the Wired section:
It opens a dialog where we can view the system’s IP address under the Details tab.
We can acquire the IP addresses of virtual machines running on the host machine, even if we can’t access them through the hypervisor or log in via SSH. For that, we use network scanning tools on the host machine to check the IP addresses of all running VMs.
For demonstration, we use a Ubuntu 22.04 host machine and a Ubuntu VM. While the VM IP address is 192.168.197.129, we assume it’s unknown.
First, let’s discuss the arp (Address Resolution Protocol) command. It’s used to display or modify the ARP cache on a system. The ARP cache is a table that maps IP addresses to their corresponding MAC addresses.
We can find the IP addresses of the host machine and all the VMs on the local network by running the arp command as long as the VM is in bridged mode:
$ arp
Address HWtype HWaddress Flags Mask Iface
192.168.197.1 ether 00:50:56:c0:00:08 C ens33
_gateway ether 00:50:56:e5:5d:50 C ens33
192.168.197.129 ether 08:00:27:eb:ab:1a C ens33
192.168.197.254 ether 00:50:56:ee:ce:8d C ens33
Thus, we list all the entries in the ARP cache, including actively connected VMs and those that have recently communicated with the host machine.
Additionally, we can utilize various network scanning tools to locate the IP addresses of all the VMs running on the local network.
Let’s use Nmap, a popular open-source network scanner. Before using Nmap, we can install it from Ubuntu’s official repository:
$ sudo apt install nmap
Next, let’s run this nmap command to locate the IP addresses of VMs with SSH (port 22) open within a specified range of IPs:
$ nmap -p 22 --open 192.168.197.0/24
Starting Nmap 7.80 ( https://nmap.org ) at 2024-10-27 16:38 GMT
Nmap scan report for baeldung (192.168.197.128)
Host is up (0.000056s latency).
PORT STATE SERVICE
22/tcp open ssh
Nmap scan report for 192.168.197.129
Host is up (0.00029s latency).
...
Thus, we can observe that we’ve successfully found the IP address of the Ubuntu virtual machine.
In this article, we discussed the ifconfig, ip addr, and hostname -I command to find the IP address of a Linux virtual machine. Additionally, we covered the GUI method for finding the IP address. We also explored ARP and Nmap methods to locate the IP of the virtual machine running an SSH server, even if we can’t access it through the hypervisor or log in.