How to Find IP Address in Linux in Three Ways

Last Updated May 11, 2022
Shyam Purkayastha

Shyam is the Founder of Radiostud.io, a content-lead innovation studio, focusing on showcasing use cases of emerging technologies. He's an entrepreneur, a technology evangelist, author, and mentor with a deep passion for nurturing ideas and building things around emerging and futuristic trends in Computing, Information Technology, and Telecommunications.

An IP Address identifies a device on the Internet or the Local Area Network (LAN). It is configured at the network layer within the operating system of a computer. All applications running on the computer and accessing other services over the Internet use the IP address to route data to and from the computer. This is the typical client-server form of communication over the Internet, where the client application running on one computer addresses the server application running on another computer, and vice versa. 

A non-technical computer user may never realize the presence of an IP address. However, if you are a techie and want to tinker with the networking configuration and connectivity options of your computer, then this post is for you. Let’s take a dig at Linux's way of finding out the IP address.

Don't reinvent the wheel.
Abstract's APIs are production-ready now.

Abstract's suite of API's are built to save you time. You don't need to be an expert in email validation, IP geolocation, etc. Just focus on writing code that's actually valuable for your app or business, and we'll handle the rest.

Get started for free

Prerequisites for Getting a Linux IP address

This post applies to the most popular Linux-based distributions such as Ubuntu, Red Hat, Debian as well as Unix flavors. You will need a computer running any of these flavors of Linux. Alternatively, you can also install Linux as a VM.

IP addresses in Linux are managed via a set of administration commands, via the command line terminal. Make sure you have a terminal window open.

Three Ways to Get Linux IP Address

ifconfig Command 

 ifconfig is the most widely used command for checking the IP address associated with a Linux computer. 

This command lists all the IP addresses configured in the networking layer of the computer for each network interface. In the above screenshot, you can see two interfaces, “ens5” and “lo”. 

The interfaces are configured with an IPv4 and IPv6 address, labeled as inet and inet6, respectively. The “ens5” is the LAN interface of the computer with IPv4 address as 172.31.12.224, and IPv6 address as f680:855:55ff:fe32:92f6.  

By default, the “lo” interface is always set to 127.0.0.1 which is a designated address meant for routing internal traffic within the computer, when both the client and server applications are hosted on the same computer. This is also known as a loopback address.

This command also provides stats about the number of packets transmitted, and received. Additionally, it also captures counts related to erroneous packet transfer, number of dropped packets, overruns, and collisions.   

ip address Command

The ip address command retrieves the IP address of the computer. 

You can see both the IPv4 and IPv6 addresses listed for the LAN and loopback interface. 

ip route command 

The ip route command is used to configure the routes for packets to and from the computer. 

The first entry of the command output shows the default route to the computer IP address via the gateway. 


Note: Both ip address and ip route are part of the ip command used for network administration and can alter the network configuration of the Linux computer. If you are using them for anything other than reading the IP address, then make sure you understand all the command options. You are also expected to be familiar with TCP & IP protocol. 

Bonus Tip: Find your IP Address using a GUI in Linux

If you are using one of the desktop versions of Ubuntu Linux, you can also check the IP address from the top left system menu where the Wi-Fi or Ethernet symbol is displayed. 

What is a Public/Private IP Address?

Generally, IP addresses are of two types. Public and private. Let’s understand this from a layperson’s perspective.

A public IP address is an address that identifies a device over the Internet. Therefore, any other device on the Internet can reach out to the device configured with a public IP address. A private IP address identifies a device on an intranet of a local area network. The three commands that you saw above were the private IP addresses of the computer.

This distinction arises because the Internet is considered a public network, whereas a local area network is considered a private network belonging to an organization or a home. It is akin to the use of public telephone numbers on the telephone / mobile network viz-a-viz, a local extension used to reach a phone connected to an office PBX or intercom.

Major Difference between Public and Private Addresses

Here are a few differences between public and private IP addresses.

Allocation of IP Address

Public IP addresses are allocated to internet service providers, which then lease them to internet subscribers. These addresses are assigned by the Internet Assigned Numbers Authority (IANA), a non-profit, standards organization that regulates and oversees the use of public IP addresses. Private IP addresses are allocated by the network administrator of the LAN. It does not need the consent of IANA or third-party organizations.

Address Numbering Limits

Private IP addresses have a pre-defined network prefix series, starting with 10, 172.16 to 172.31, and 192.166. The rules governing the use of private IP addresses are defined in the RFC 1918, which is the Internet Engineering Task Force’s (IETF) specification, for maintaining the requirements for all protocols used for communication over the Internet. 

A public IP address can be in any range within 0.0.0.0 to 255.255.255.255, barring the private IP address, the localhost address, and a few reserved ones. 

Activation

Public IP addresses are activated based on autonomous systems, which is a logical grouping of network prefixes for different Internet Service Provider (ISP) serving in various geographical regions. Due to the vastness of the Internet, the routing information for reaching the IP address is managed via a separate set of routing protocols, such as the Broader Gateway Protocol (BGP), which takes time to propagate across the entire Internet. Therefore, activation of public IP addresses for an autonomous system takes some time, depending upon the routing protocols.

Private IP addresses are activated on a computer connected to a LAN network. Because of its limited coverage, and the use of layer 2 MAC addressing, the activation of a private IP address is almost instant. 

Finding Your Public IP Address

Even though your Linux computer only has a private IP address for accessing the Internet, it must be connected to LAN with a gateway that has a public IP address. In most cases, the gateway is a router, with LAN side and WAN side network interfaces having distinct private and public IP addresses, respectively. It routes the packets across these two interfaces via a process of network address translation and serves all the computers in the LAN to connect to the Internet.

Here are a few ways in which you can get the public IP address associated with your Linux computer. 

curl Command

With the curl command, you can fetch your gateway's public IP address from a few websites. These websites track the IP address of any incoming request and return that as an HTTP response. 

Some of the popular websites for returning your pubic IP address are:

icanhazip.com

ifconfig.me

ipinfo.io/ip

dig command

The dig command is a DNS lookup utility. Querying for a DNS resolution with this command fetches the public IP address of the computer.

host command

 host is another utility command for DNS lookups. 

IP address on browser

If you have a desktop Linux system, you can check your public IP address by launching a web browser with the What is my IP address and location page.

Finding Additional Information Through the IP Address

Apart from its primary purpose of routing network traffic, an IP address contains a lot of additional information. This applies to any public IP address. Based on the allocations to ISPs of specific regions in the world, you can extract geo-location and ISP specific information for every public IP address.

Check out the Abstract IP Geolocation API to know more about a specific public IP address.

FAQ

What is an IP Address?

An IP address is a multi-part, numeric formatted number used for addressing a computer in a TCP/IP network. It is available in IPv4 or IPv6 address formats. Every computer must have a private IP address for connecting to a LAN. For accessing the Internet, it can be assigned a public IP address directly or must be reachable to a gateway configured with a public IP address.

How to check the IP address of a Linux system?

Linux system offers many commands for accessing the IP address-related details. One can use the ifconfig command, or the IP address command to know the IP address configured on the system. Additionally, using the curl command, it is possible to fetch the public IP address of a Linux computer through some websites, such as ifconfig.me or icanhazip.com

What is the difference between public and private IP addresses?

Public IP addresses are assigned to devices and computers that are directly connected to the Internet. They are reachable over the Internet, which means that any other computer can reach the computer configured with the public IP address over the Internet. These addresses are controlled by IANA and the local ISPs serving the various geographical regions. Private IP addresses are configured on the network interface of a computer that is attached to the LAN. They are reachable within the LAN, but not over the Internet.

Abstract's IP Geolocation API comes with libraries, code snippets, guides, and more.

Get started for free
Abstract's IP Geolocation API comes with libraries, code snippets, guides, and more.
Get started