Find IP Address in Linux Using the Command Line

How to Find/Get IP Address in Linux Using Command Line

This article explains the most used ways for finding a Linux system’s public and private IP addresses using the command line.

One of the first questions many new Linux system administrators face is getting the IP address. Furthermore, this question is regularly asked when applying for positions that require Linux skills.

However, IP addresses can be public (external) and private (internal). So, before we get into the practical side of things, let’s clarify what public and private IP addresses are and how they differ.

What Is a Public IP Address?

A public IP address is used to access the Internet. Unlike private IP addresses, however, public IP addresses can be routed over the Internet.

Public IP addresses are used by devices and services that require a direct Internet connection. In the most common situation, these are typically network devices that handle traffic in both directions, from the Internet to them and from them to the Internet.

As the simplest example, we can point to your home router, which works with a public IP address obtained from your ISP.

What Is a Private IP Address?

A private IP address is reserved for internal usage behind a router or other network device and is used to communicate only within the same network. In other words, it operates as a unique identification for all devices connected to a router or other device that serves IP addresses.

However, unlike public IP addresses, the private IP addresses cannot be routed or used on the Internet; they are only supposed to work within the local network. Therefore, direct Internet access from a private IP address is not possible.

On top of that, private IP addresses have a limited range within which they can be extended. Therefore, this is the most reliable way to determine if an address is public (external) or private (internal).

To be more specific, each private IP address must belong to one of the three ranges shown below:

  • 10.0.0.0 to 10.255.255.255, 10.0.0.0/8, (65,536 IP addresses)
  • 172.16.0.0 to 172.31.255.255, 172.16.0.0/12, (1,048,576 IP addresses)
  • 192.168.0.0 to 192.168.255.255, 192.168.0.0/16, (16,777,216 IP addresses)

To make things even more transparent, we’ve included a diagram below. It shows your home router, which typically contains at least two network interfaces.

One is specially designed to use your public IP address given to you by your ISP. The other has an automatically assigned private IP address and can provide IP addresses from the private network ranges to downstream devices such as your laptop, TV, smart home devices, and so on.

What is private and public IP address?

Now that we’ve clarified those points and you’re more familiar with public and private IP addresses let’s move on to the main topic of this guide.

How to Find IP Address in Linux Using Command Line

Depending on the method used, you may obtain information on your public or private IP address. We’ll look at both approaches in the examples below.

Find Your Public IP Address

The commands below use cURL to connect to a remote API and return your public IP address in response. So, whichever of the following examples you choose, the result will be your public IP address.

curl ifconfig.me
curl ipinfo.io/ip
curl -s https://checkip.amazonaws.com
curl api.ipify.org
curl icanhazip.com

Of course, using other tools like dig or wget can achieve the same result.

dig +short myip.opendns.com @resolver1.opendns.com
wget -qO - icanhazip.com
Find your public IP address

Find Your Private IP Address

As previously stated, private IP addresses cannot transmit data over the Internet and are only intended to function within the local network. For example, your router assigns a private IP address to each device on your home local network.

So you can find your private IP addresses using the commands below. Of course, if you run them on a device with direct Internet connectivity, you will also get its public IP address.

The ifconfig command is one of the simplest ways to obtain information on available network interfaces in Linux and their associated IP addresses. However, it should be noted that the ifconfig command has been deprecated in recent years.

ifconfig
Find an IP address by using the ifconfig command

We can see from the above that the network interface eth0 is assigned the private IP address 10.0.0.4. The second IP address, 127.0.0.1, is the address of the loopback interface, which is outside the scope of this guide.

Check out the man page of the infonfig command for more information.

The other most popular way to find the IP address is by using the ip addr command. And, like in the preceding example, no additional arguments are required.

ip addr
Find an IP address by using the ip addr command

However, the output can be challenging to read, especially if you have numerous network interfaces. Fortunately, combining the command output applying a Linux pipe and a regular expression, the result is much more pleasant, showing only the IP addresses.

ip addr | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
Find an IP address by using the ip addr command

Check out the man page of the ip command for more information.

The hostname command is the final technique we’ll show you how to find an IP address using the command line in Linux. After that, add the -I parameter.

hostname -I
Find an IP address by using the hostname command

When you press Enter, the IP address of your device will be displayed on the terminal just below your typed command.

Check out the man page of the hostname command for more information.

Conclusion

As you know, the IP address is used in a system for communication. Therefore, you will often need to know your device’s IP addresses. Now, you may quickly find them by using any of the methods given above.

Furthermore, if you want to learn how to change your IP address in Linux effortlessly, check out our excellent guide, “How to Set Static IP Address and Modifying Routing Table on Linux.”

We hope that this article will be of great use to you in the future.

Leave a Reply

Your email address will not be published.