Home » How-to » How to Get IP Address in Linux

How to Get IP Address in Linux

Each computer connected to the Internet must have an identifier. In the past, every computer had a unique address that could be used to connect to it. This address is called IP address. Nowadays, most home computers are hidden behind Network Address Translation (NAT), and only servers have public IP addresses.

IP addresses allow computers and servers to communicate with each other on a network. While this is more relevant for servers, it can also be useful for home computers and local networks. In this article, I will explain how to get IP Address in Linux in different ways. I will explain different ways how to find an external IP address which is visible on the Internet and a local address in your local network.


Table of Contents

Basics

First of all, let's take a look at what is an IP address and how it works. There are three types of IP addresses:

  • Internal IP address;
  • IP address in local network;
  • External IP address that is available on the Internet.

The internal IP address or loopback address allows programs to communicate with each other on the same computer using network protocols. For example, a browser can interact with a local web server. Loopback IP address is quite often used in system utilities and it is pretty easy to find it. It is always the same - 127.0.0.1.

In local network, things become a little more complicated. The Internet is not a peer-to-peer network where all computers are connected to each other at the same level, but many separate networks that are connected via routers. If your computer is connected to the Internet using a wired connection, you have a router to which you can connect multiple devices such as a computer, a laptop, and a smartphone. The network created by the router is called a local network, and on that network, the computer has an IP address that you can use to access it from other devices on that network. But for your ISP and the external Internet, all of these devices will have the same IP address - the router address.

Now, let's have a look at an external IP address. ISP providers unite their clients into a local network so that they all have the same external IP address for the Internet as well as you do this for your home devices. They do this because the Internet is very popular and IP addresses are limited. Today, only servers have white unique white IP addresses, and most other devices are placed by ISPs on one IP with NAT technology. This IP address is considered an external address.

Previously, ISPs gave users dynamic IPs, which were unique and changed when the router was rebooted. They allowed users to access their PC from the Internet with little router configuration. But then, they started using NAT technology to place hundreds of users on the same IP. However, this is not a big issue because it is safer for home PCs as nobody can access them from the Internet. If you want to access your PC over the Internet, you can use VPN or Ngrok.

The NAT technology allows PCs to interact with any server on the Internet, and receive a response from it. But it is impossible to initiate a connection to this computer from the outside because one external IP belongs to hundreds of computers, and the system just does not know which one is being addressed.

As you have realized, your home computer could have an external IP address available to everyone, but this is impractical for at least two reasons. Firstly, it is expensive, and secondly, it is very insecure. This is why connecting your computer to the Internet now looks something like this:

  • An external and publicly accessible IP that belongs to your ISP and is used to access the Internet by hundreds of users;
  • The IP address of your router in your ISP's local network. It is not accessible from the Internet, and you probably don't need it;
  • The IP address of your computer in the local (home) network created by the router which you use to connect your devices. This IP address can be used for communication between your local devices;
  • The internal IP address of your computer, which is not accessible from the outside and is used only for internal communications within the system.

Now, you know all necessary basics. Let's have a look at how to get IP address in Linux.

How to Find a Local IP Address in Linux

As mentioned before, the internal IP address is the same for every computer - 127.0.0.1. It works the same way on all operating systems. All IP addresses in the range from 127.0.0.1 to 127.255.255.255 are bound to loopback network interface and are used for local communication within the computer. Additionally, you can access your computer via the localhost domain. You can verify this by running the ping command:

ping -c 3 localhost

How to Find IP Address in Local Network Linux

It is not very difficult to find IP address Linux in a local network. You don't need any additional services to accomplish this, you can just look up the network connection information with the ip command. Here is the most convenient way to do it:

ip -br a

Here the -br option enables brief mode output and the a command means "addresses management". You should also add the s or show command by syntax, but you can skip it because it is used by default anyway. Here is the full version of the command:

ip --brief address show

When your run this command you will see the list of all network interfaces in the system, their state, and address. Let's have a look at their description:

  • lo - local network interface, which always points to the current computer;
  • enp* - wired network interface, stands for Ethernet PCI. The name eth* can also be used.
  • wlp* - WiFi wireless network interface, stands for Wireless PCI. The name wlan* can also be used.

In this case, the wired interface has the name enp3s0 and you can see its IP address in the third column. If you want to get an IP address for a specific interface, for example, enp3s0, use the following command:

ip --brief address show enp0s3

Additionally, you can filter only the IP address value with the following command:

ip --brief address show enp0s3 | awk '{print $3}' | cut -d'/' -f1

IP addresses on the local network must be in the following ranges:

  • From 192.168.0.0 to 192.168.255.255
  • From 10.0.0.0 to 10.255.255.255
  • From 172.16.0.0 to 172.31.255.255
  • From 100.64.0.0 to 100.127.255.255

These addresses are reserved for local networks and cannot be used for public access.

You can also use the deprecated ifconfig command. But you must install it first:

sudo apt install net-tools

It displays a lot of information such as: the MAC address, the netmask, and some statistics about the interface performance. The IP address itself is displayed after the word inet:

sudo ifconfig

To display only the IP addresses in the output, you can use the following command:

sudo ifconfig | grep "inet"

How to find External IP Address in Linux

Each website can see your external IP address. You can open a special website, which will look at your IP and tell it to you. There are quite a few of these sites. The easiest way to do this is with your browser, for example, open the website ifconfig.me:

However, this may not always be convenient. Sometimes, it is better to do it with a terminal. Just use the curl or wget command. Here is the list of a few such websites:

wget -qO- ifconfig.me curl ifconfig.me curl ipinfo.io/ip curl ipecho.net/plain curl icanhazip.com curl ipecho.net/plain curl ident.me curl api.ipify.org

To avoid typing a long command every time, you can create an alias using the alias command:

alias getip="curl ifconfig.me" getip

Don't forget to add this line to the ~/.bashrc file.

If you want to find IP address of your router in the ISP's local network, this approach will not help. You can find this information in the router settings or router's command line.

Wrapping Up

Now, you know how to get IP address in Linux. It's not as hard as you might think. You can still use the hostname utility with the -I option to get all the network addresses assigned to your computer or you can look up the IP address in NetworkManager GUI or with its console tool nmcli. But the ways described in the article are more versatile and simple. What tool do you use? Write in the comments!

Creative Commons License
The article is distributed under Creative Commons ShareAlike 4.0 license. Link to the source is required .

Leave a Comment