Get an IP Address on Linux : Discover the Many Ways

Published:9 August 2022 - 6 min. read

Nicholas Xuan Nguyen Image

Nicholas Xuan Nguyen

Read more tutorials by Nicholas Xuan Nguyen!

Meet Active Directory and Windows Server auditing, security and compliance needs with ManageEngine ADAudit Plus. Download Free Trial!

Your IP address is like your home address, and it’s how the internet knows where to send the data you’ve requested. Knowing your IP address is handy in many ways, like remotely managing your server. But how do you get an IP address on Linux?

In this tutorial, you’ll learn many ways to find your IP address on Linux, whether using the command line or checking through system settings.

Read on and pick off how you’d like to get your IP address!

Prerequisites

This tutorial will be a hands-on demonstration. If you’d like to follow along, be sure you have the following:

  • A Linux machine – You can use a virtual machine, a dedicated server, or even your local Windows 10 computer with Windows Subsystem for Linux (WSL) – This tutorial uses Ubuntu 20.04 LTS, but other Linux distros will work.
  • This tutorial assumes you’ve already logged/SSH into the system as a user with sudo privileges – This tutorial uses the root user to execute all the commands.

If you’re not logged in as a root user, prepend sudo to this tutorial’s commands.

Get an IP Address on Linux with the curl Command

A popular tool called cURL lets you transfer data specified with URL syntax. This tool supports protocols like DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, etc.

But the important question is, can you get your public IP address with the curl command? Yes! Getting your public IP address is just one of the cURL’s powerful features.

Related: placeholder of this one

Run the below command to perform the following in finding your public IP address:

  • curl – Uses the Google Search API to fetch the first result of the query “what is my ip address.”
  • The -s option tells curl to perform a silent request. The output is then piped to grep, which extracts only the IP address from the response using a regular expression.
  • The -oE portion of the grep command stands for “only match,” which outputs only the matching IP address and not the whole response.
  • The -m1 option tells grep to stop after the first match. Why? Google Search API returns a bunch of HTML code along with the IP address; you don’t need that.
curl <https://www.google.com/search?q=what+is+my+ip+address> -s | grep -oE "\\b([0-9]{1,3}.){3}[0-9]{1,3}\\b" -m1

Below, you can see your public IP address printed out.

Finding the public IP address with the curl command
Finding the public IP address with the curl command

Get an IP Address on Linux with the wget Command

Like cURL, Wget is a free utility that lets you retrieve files using the most widely-used Internet protocols (HTTP, HTTPS, and FTP).

Wget lets you download single files, whole websites, or even mirror an entire website, and of course, find your IP address.

Run the below wget command to find, and display your public IP address to the terminal with the following:

  • The wget command sends an HTTP request to https://ipecho.net/plain and retrieves the content of that page.
  • The -qO option tells wget to be quiet and print as little output as possible to the terminal.
  • The output of the wget command is piped to the xargs echo command to display the output in a single line.
wget -qO- [<https://ipecho.net/plain>](<https://ipecho.net/plain>) | xargs echo

Now, you can share the IP address with your users so that they can connect to the server via SSH.

Finding the public IP address using the wget command
Finding the public IP address using the wget command

Digging Your Public IP Address with the dig Command

The Domain Information Groper (dig) command is used for querying DNS servers, but you can also use this command to find your public IP address.

Related: Placeholder

Suppose you sent a ping request to your web server’s public IP address. If you received the ping response, you know that your web server is running and accessible from the outside world. If not, you may have to troubleshoot the issue to identify the problem, and your first step is to find your public IP address.

Run the below dig command to query your web server’s public IP address.

  • The below command uses OpenDNS nameservers (resolver1.opendns.com) to find your public IP address. OpenDNS servers are a good idea as they are reliable and offer better security features, such as phishing protection and content filtering.
  • The +short option instructs dig to print the output in a short format, while myip.opendns.com is the hostname that returns your public IP address.
dig +short myip.opendns.com @resolver1.opendns.com
Finding the public IP address using the dig command
Finding the public IP address using the dig command

Now, run the ping command below to send a ping request every 10 seconds. Replace IP_address with your web server’s actual public IP address.

ping -i 10 IP_address
Sending ping request
Sending ping request

Finding Your Private IP Address Using the ifconfig Command

At this point, you already know how to find your public IP address. But in some cases, you may need to know the private IP address of your Linux server to connect to it from another device on the same network.

How to get your private IP address? The ifconfig command will do the trick.

Run the ifconfig command below to display all (-a) network adapters on your system and their configuration, including your private IP address.

ifconfig -a

As you can see below, one system can have more than one network adapter. Each adapter can have a different IP address.

Look for the inet entry under the ethh0 or wlan0 adapter to find your private IPv4 address, as shown below.

Finding Private IP Address Using ipconfig Command
Finding Private IP Address Using ipconfig Command

Writing a Bash Script to Find Your IP Address

Perhaps you need a way to find your IP address automatically. If so, writing a Bash script is an excellent choice to find your public and private IP addresses.

Why use a script? You can use the same script on multiple systems, saving yourself the hassle of running the commands on each system.

1. Open a new find_ip.sh file in your preferred text editor.

nano find_ip.sh

2. Add the following code to the find_ip.sh file, save the file and close the editor.

The code below finds and stores your private and public IP addresses on the lanIp and wanIp variables and prints them on the terminal.

#!/bin/bash
 
# Find your private IP address and store the value to the lanIp variable
lanIp="$(ip -4 -o -br addr|awk '$0 ~ /^[we]\w+\s+UP\s+/ {str = gsub("/[1-9][0-9]*", "", $0); print $3}')";
# Find your public IP address and store the value to the wanIp variable
wanIp="$(curl https://ipinfo.io/ip 2>/dev/null)";
 
# Print the values of the lanIp and wanIp variables
echo "Your private ip is: ${lanIp}";
echo "Your public ip is: ${wanIp}";

Storing your server’s public IP address in an environment variable is handy for a CD/CI tool, such as Jenkins or TravisCI.

3. Finally, run the below sh command to execute the find_ip.sh script to find your IP addresses.

sh find_ip.sh

You will see an output similar to the following, showing your public and private IP addresses.

Finding IP addresses using a Bash script
Finding IP addresses using a Bash script

Conclusion

In this tutorial, you’ve learned how to find/get your IP address on Linux using a few different commands, like the curl and ifconfig. You also wrote a script to automate finding IP addresses on multiple systems.

Now that you know how to find your IP address on Linux, you can use this information for different tasks. Perhaps to configure a Cloudflare dynamic DNS for your home network?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!