Get IP Address of a Machine from Bash



IP address stands for Internet Protocol address, which is a unique string of numbers assigned to each device connected to a computer network that uses the Internet Protocol for communication. A system needs an IP address for various networking tasks in Linux, like setting up network connections, configuring services like file sharing, SSH or FTP or troubleshooting network-related issues, etc.

An IP address can be classified into two main categories, public and private. A public IP address usually can be accessed from the Internet, whereas private addresses are reserved for internal use within your private network without being directly exposed. Furthermore, there are two types of IP addresses, IPv4 (IP version 4) and IPv6 (IP version 6).

A Linux machine usually have multiple IP addresses which can be of following types ?

  • Loopback ? These kinds of IP addresses are used for internal communication within the host. Typically, 127.0.0.1 is assigned for localhost.
  • Local Network IP ? Local network IP addresses are usually assigned by your local router, and it has private range IP like 191.168.x.x or 10.x.x.x.
  • Public IP ? These are assigned by ISP (Internet Service Provider) and are usually visible to the outside world for external communication.

There are several methods to obtain the IP address of a machine using simple Bash commands in Linux/Unix systems. We'll cover a few common ones in the next steps.

Using the "ip" Command

This is the modern and most preferred way to get IP address information in Linux as shown below ?

$ ip addr show
# or shorter version
$ ip a

Output of this command will display detailed information about all network interfaces including their IP addresses. You need to look for the inet entry below the desired network interface to find the IP address (e.g. eth0, wlan0).


For more precise output, you can also try using below expression using grep ?

$ ip -4 addr show | grep -oP "(?<=inet\s)\d+(\.\d+){3}"


Using "hostname" Command

The hostname command with -I option can be used to display the IP address of your machine.

$ hostname -I #Show all IP addresess
$ hostname -i #Show the primary IP address


Using the "ifconfig" Command

ifconfig command comes as part of net-tools package, which might need to be installed first if it is not already installed. You should be able to use it as ?

$ ifconfig


Like the ip command, look for inet entry below the desired interface. For a more direct way you can use the command below using grep & awk as below ?

$ ifconfig | grep "inet " | grep -v "127.0.0.1" | awk "{print $2}"


Using "nmcli" Command

For Linux systems that use NetworkManager for managing the network, the nmcli command can be used to get the desired IP address information ?

$ nmcli -p device show 

In the output you need to look for IP4.ADDRESS entry under the desired network interface.


Using "awk" Command

This is a script-oriented approach, where you can parse the /proc/net/fib_trie file using awk tool and it will extract the IP address from the kernel"s routing table.

$ awk "/32 host/ { print f } {f=$2}" /proc/net/fib_trie


Using "dig" Command

To get your external or public IP address using a DNS lookup is by using dig command with opendns,com as shown below ?

$ dig +short myip.opendns.com @resolver1.opendns.com

Using "curl" Command

Another method to fetch your external or public IP address is by using curl command with external 3rd party sites that can provide the required information.

$ curl ifconfig.me
# or
$ curl icanhazip.com
# or
$ curl -4 icanhazip.com
# or
$ curl -6 icanhazip.com
# or
$ curl api.ipify.org
# or
$ curl ipinfo.io/ip
# or
$ curl ipecho.net/plain

Conclusion

In the world of networking and system administration, knowing how to retrieve the IP address of a machine is a fundamental skill. Whether you're troubleshooting network issues, configuring servers, or simply curious about your network setup, being able to quickly find your machine's IP address using Bash can be incredibly useful.

In this tutorial, we provided a comprehensive overview of the various methods to obtain IP address related information using simple Bash commands, suitable for both beginners and experienced users. We covered multiple ways to get IP address information in Bash, some using default commands while some with 3rd party networking tools.

Updated on: 2025-01-28T17:18:45+05:30

99 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements