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 15, 2024
Knowing the IP address of a system is essential for managing or troubleshooting network issues. In Linux, there are multiple ways to extract the IP address of a system, both through the command line and the graphical user interface.
In this tutorial, we’ll explore different methods to extract the IP address on a Linux system.
The ifconfig command is used to display the configurations of a network interface, as well as to enable and disable a network interface and assign an IP address to an interface. We can also use the ifconfig command to extract the IP address of an interface.
First, to list all network interfaces on our system, we can use the ip link show command:
$ ip link show
1: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:dd:ef:84 brd ff:ff:ff:ff:ff:ff
altname enp2s1
2: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:dd:ef:8e brd ff:ff:ff:ff:ff:ff5
altname enp2s5
This command shows that we have two network interfaces, ens33, and ens37, configured on our system.
To find the IP address of a particular network interface, we can use the ifconfig command followed by the interface name:
$ ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.44.128 netmask 255.255.255.0 broadcast 192.168.44.255
inet6 fe80::a56f:5571:bbaa:a75a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:dd:ef:84 txqueuelen 1000 (Ethernet)
RX packets 886446 bytes 1246553694 (1.2 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 687493 bytes 42931582 (42.9 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
The command output shows detailed information about the specified interface, including the IP address. To extract the IP address specifically, we can use the ifconfig command along with grep and awk:
$ ifconfig ens33 | awk '/inet / {print $2}'
192.168.44.128
Here, awk ‘/inet / ‘ filters the output of the ifconfig command to include only the line that contains the word inet followed by a space character. Then, ‘{print $2}’ prints the second field of this line, which is the IP address of our interface.
The hostname command in Linux is used to view and change the hostname of a system. Additionally, we can print the IP address of our system using the -i flag with the hostname command:
$ hostname -i
192.168.44.128
The output of the command displays the IP address of the system.
The ip addr command in Linux displays the IP addresses and other network-related information for all network interfaces configured on a system. To display the IP address of only a specific interface, we need to specify the interface name after the ip addr show the command:
$ ip addr show ens33
1: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 00:0c:29:dd:ef:84 brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 192.168.44.128/24 brd 192.168.44.255 scope global dynamic noprefixroute ens33
valid_lft 1573sec preferred_lft 1573sec
inet6 fe80::a56f:5571:bbaa:a75a/64 scope link noprefixroute
valid_lft forever preferred_lft forever
However, this includes detailed information about the specified interface. To filter out all the unnecessary details and display only the IP address, we can use the ip addr command along with the grep and awk commands:
$ ip addr show ens33 | grep 'inet ' | awk '{print $2}'
192.168.44.128/24
This command displays the IP address of ens33 along with the network prefix length. To display the IP address without the prefix length, we can pipe the output to the cut command:
$ ip addr show ens33 | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1
192.168.44.128
This command shows only the IP address, excluding the netmask length from the output.
The nmcli command in Linux is used to control NetworkManager and display the status of network devices. We can also use it to create, edit, display, activate, and deactivate network connections. Additionally, the nmcli command can show the IP address of a device.
Let’s find the IP address of our device:
$ nmcli -g ip4.address device show ens33
192.168.44.128/24
The -g ip4.address option filters the output to show only the IPv4 address, whereas device show ens33 limits the result to the ens33 interface.
Alternatively, we can also print the IP address without the netmask length by using the nmcli command along with the grep, awk, and cut commands:
$ nmcli device show ens33 | grep 'IP4.ADDRESS' | awk '{print $2}' | cut -d'/' -f1
192.168.44.128
Here, nmcli device show ens33 displays the results for the ens33 interface, while grep ‘IP4.ADDRESS’ filters the output to show only the line containing the IPv4 address. After that, the awk and cut commands extract the IP address excluding the netmask length.
The ip route command in Linux is used to view and modify routing table entries. It can also show the IP address configured on our system.
Let’s print only the IP address of our system using ip route:
$ ip route get 8.8.8.8 | cut -d' ' -f7
192.168.44.128
ip route get 8.8.8.8 shows the route our system would take to reach Google’s public DNS server at 8.8.8.8. On the other hand, cut -d’ ‘ -f7 extracts the 7th field from the output, which is the source IP address of our system that will be used to reach 8.8.8.8.
Using a Bash script makes it easy to extract the IP address efficiently. Let’s create a script named find_ip.sh that prompts for the interface name and returns the corresponding IP address:
$ cat find_ip.sh
#!/bin/bash
read -p "Enter the network interface (e.g., ens37): " INTERFACE
IP=$(ip addr show "$INTERFACE" | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)
if [ -z "$IP" ]; then
echo "No IP address found for interface $INTERFACE."
else
echo "IP address for $INTERFACE: $IP"
fi
Next, we make the script executable:
$ chmod +x find_ip.sh
Now that the script is executable, let’s run it:
$ ./find_ip.sh
Enter the network interface (e.g., ens37): ens33
IP address for ens33: 192.168.44.128
This script will display the IP address of the specified network interface.
In this article, we covered several ways to extract the IP address on a Linux system, using the ifconfig, hostname, ip addr, nmcli, and ip route commands. Additionally, we created a script that prompts for the network interface and returns the corresponding IP address.