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.comCode language: JavaScript (javascript)

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.comCode language: CSS (css)
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}'Code language: JavaScript (javascript)
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.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 68%