How to get your IP Address on Linux

Public and Private IP Addresses

IP addresses are an essential part of modern networked communications. In this guide, we will show you how to find your own IP address. These instructions will work for most of the various Linux distributions like Ubuntu, Debian and Linux Mint, among others. BSD systems like FreeBSD and NetBSD, as well as Mac OS, might also be able to read their IP addresses using these instructions.

What is an IP Address?

Understanding the TCP/IP Protocol

IP addresses arose as part of what's known as the TCP/IP protocol. This is a networking standard that defines how data is packaged and transferred between computers that are joined together in a computer network.

The TCP/IP protocol was invented in 1978 by Bob Kahn, Vint Cerf, and others.

TCP stands for Transmission Control Protocol, which is the protocol responsible for the reliable transmission of data over networks. In particular, TCP checks integrity of transmissions, and provides for re-transmission if packets have not been delivered correctly.

The IP protocol, on the other hand, deals with the routing of packets (like those sent by TCP). IP stands for Internet Protocol, which defines rules that let packets be sent from an origin to a destination computer. Under the IP protocol, packets are routed by a path of computers that gets progressively closer to the packet's intended recipient.

Each computer or device on the network is identified by an IP address, which is a numeric identifier unique to that device on the network.

Differences Between Public and Private Addresses

There is a difference between IP addresses on the public internet and private IP addresses inside a local computer network. Internet Service Providers (ISPs) assign public IP addresses, which is akin to your public home address. When computers connect to the private network, they each get a private IP address to be used within just that network. Private IP addresses are assigned by your router or another device within the network. In turn, the router can route incoming packets to the correct device on the network using network address translation (NAT).

How to Find your Public IP Address

You can discover your public IP address using a variety of commands that connect to the internet to run queries for you.

Getting your Public IP Address with the dig Command

The dig command is a DNS lookup utility for Linux. Using dig, you can look up your public IP address by connecting to OpenDNS servers. OpenDNS hosts DNS servers that help discover the IP addresses of networks on the internet. Run the following command inside your bash, sh, or other terminal.

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

As output you should get back your public IP address from OpenDNS resolvers. Your IP address will be a series of groups of digits of the format 216.58.216.164 (which is one of the many IP addresses for Google), or a variation of this sort.

Using Curl to Return your Public IP Address

The curl command is another networking utility that allows you to interact with servers on the internet. You can query servers to return your public IP using the following options:

$ curl http://ifconfig.me/ip

OR

$ curl http://icanhazip.com

The output of each command will be your public IP as seen by these servers.

There are many other servers that can return your public IP address besides these two given above.

Using wget Instead of curl

wget is a commonly available utility for Linux that you can use instead of curl to download data and interact with servers. You can use wget to retrieve your public IP using the command:

$ wget -qO- icanhazip.com

Using a Browser

If you have a browser available, you can visit one of the many IP websites out there:

or you can also check the other websites we reached with curl to see your IP in the browser. There are cases, such as when you are logged on to a Linux server, where you do not have access to a graphical user interface. In such cases, use the shell commands.

Get your Private IP Address with the ifconfig Command

You have multiple ways to get your private IP address. One way is to use the ifconfig command. ifconfig is a command line program that configures network interfaces on Linux.

You can retrieve your IP address using the ifconfig command coupled with various flags that filter for your private IP address. Run the following command inside your shell:

$ ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'

The above command checks all the active network interfaces, then filters for the TCP/IP interface, and finally filters the output for the local IP address. The final output is your private IP address. This will look similar to 192.168.1.2 or some variant.

We can filter the ifconfig output using sed instead, which is a utility for parsing and transforming streams of text. Run the following command to get your private IP address:

$ ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'

In this example, we are filtering ifconfig output to get your IP address under the TCP/IP protocol. The above commands may fail if you do not have sed or ifconfig installed. In case the command fails, try the hostname command and follow the method below.

How to Find your Private IP Address using Hostname Command

The https://linux.die.net/man/1/hostname command returns the DNS information of the machine. You can find your private IP address by executing the following command in your shell:

$ hostname -I

The above command enumerates all your configured addresses on all network interfaces, including your private IP address.

Get your Private IP Address with the IP Command

We can also get the private IP address of a Linux machine using the ip command. The ip command shows and manipulates routing, devices, policy routing and tunnels.

We can use the following variations of the ip command with flags to return our private IP address.

$ ip route get 1 | awk '{print $NF;exit}'
$ ip route get 8.8.8.8 | head -1 | cut -d' ' -f8
$ ip route get 8.8.8.8 | head -1 | awk '{print $7}'

These commands print out the routing table entries for sending a request to alternate servers. These involve our private IP address as the source of the requests. We are filtering for that source using the Linux commands head, awk and cut to extract our private IP address.

Finding your Private IP Address from Network Connection Settings

Besides the command line and the browser, we can also use Linux administrative applets. You may need to adjust these instructions to suit your exact Linux distro.

  • Navigate to your "Menu"
  • Find the "Preferences" tab
  • Navigate to "Network" or the equivalent for managing your network information
  • Select the network type, (i.e. Wi-Fi, Ethernet, or other)
  • You can view your private IP address under the printed information about your IP

This will not work in a strictly command-line environment but will work on desktop Linux systems. If you are on a command-line only login, then you will need to try the other command-based methods given above.

Numerous ways to get IP Address on Linux

As we saw, there are multiple ways to get your IP address on a Linux system. There are in fact two types of IP addresses, a public IP address and a private IP address. The public IP address identifies your computer or network to the outside world. Your private IP address identifies your machine inside your private network. To get your IP addresses, you can use a mix of commands such as ifconfig, ip or hostname, or make use of graphical environment apps.

Author image
Tendai Mutunhire started out doing Java development for large corporations, then taught startup teams how to code at the MEST incubator, and is in the process of launching a few startups of his own.