---
title: "Get an IP Address on Linux : Discover the Many Ways"
description: "Discover the many ways and tools that are available to get an IP address on Linux and learn how to incorporate those tools into scripts!"
canonical: "https://adamtheautomator.com/get-an-ip-address-on-linux/"
---

# Get an IP Address on Linux : Discover the Many Ways

> Discover the many ways and tools that are available to get an IP address on Linux and learn how to incorporate those tools into scripts!

Source: https://adamtheautomator.com/get-an-ip-address-on-linux/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Get an IP Address on Linux : Discover the Many Ways](https://adamtheautomator.com/wp-content/uploads/2022/08/Discover-the-Many-Ways-to-Get-an-IP-Address-on-Linux.jpg)

# Get an IP Address on Linux : Discover the Many Ways

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)9 August 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Command Line](/tag/command-line/)[Curl](/tag/curl/)[Linux](/tag/linux/)[Networking](/tag/networking/)

Table of Contents

*   [Prerequisites](#prerequisites)
*   [Get an IP Address on Linux with the curl Command](#get-an-ip-address-on-linux-with-the-curl-command)
*   [Get an IP Address on Linux with the wget Command](#get-an-ip-address-on-linux-with-the-wget-command)
*   [Digging Your Public IP Address with the dig Command](#digging-your-public-ip-address-with-the-dig-command)
*   [Finding Your Private IP Address Using the ifconfig Command](#finding-your-private-ip-address-using-the-ifconfig-command)
*   [Writing a Bash Script to Find Your IP Address](#writing-a-bash-script-to-find-your-ip-address)
*   [Conclusion](#conclusion)

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)](https://adamtheautomator.com/windows-subsystem-for-linux/) – This tutorial uses Ubuntu 20.04 LTS, but other Linux distros will work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

*   [NET-3 networking toolkit](https://installati.one/ubuntu/20.04/net-tools/) installed.
    
*   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._

Related:[How to Set up the SSH Chrome Extension](https://adamtheautomator.com/ssh-chrome-extension/)

## **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.

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/08/image-45.png)

Finding the public IP address with the curl command

Related:[How to Search via Grep with Regex](https://adamtheautomator.com/grep-with-regex/)

## **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.

Related:[Install Python Wget and Automate your File Downloads](https://adamtheautomator.com/python-wget/)

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.

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/08/image-46.png)

Finding the public IP address using the wget command

Related:[How to SSH into Docker Containers](https://adamtheautomator.com/ssh-into-docker-container/)

## 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`](http://myip.opendns.com/) is the hostname that returns your public IP address.

```bash
dig +short myip.opendns.com @resolver1.opendns.com
```

![Finding the public IP address using the dig command](https://adamtheautomator.com/wp-content/uploads/2022/08/image-47.png)

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.

```bash
ping -i 10 IP_address
```

![Sending ping request](https://adamtheautomator.com/wp-content/uploads/2022/08/image-48.png)

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](https://manpages.ubuntu.com/manpages/trusty/man8/ifconfig.8.html) 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.

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/08/image-49.png)

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.

Related:[Your One and Only Linux Shell Scripting Tutorial](https://adamtheautomator.com/linux-shell-scripting-tutorial/)

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.

```bash
nano find_ip.sh
```

Related:[How to Edit Files with a Real PowerShell Text Editor](https://adamtheautomator.com/powershell-text-editor/)

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.

```bash
#!/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](https://adamtheautomator.com/install-jenkins/) or TravisCI._

Related:[Definitive Guide to Getting Started with Travis CI](https://adamtheautomator.com/travis-ci/)

3\. Finally, run the below [sh](https://www.cyberciti.biz/faq/run-execute-sh-shell-script/) command to execute the find\_ip.sh script to find your IP addresses.

```bash
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](https://adamtheautomator.com/wp-content/uploads/2022/08/image-50.png)

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](https://adamtheautomator.com/cloudflare-dynamic-dns/) for your home network?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fget-an-ip-address-on-linux%2F&text=Get%20an%20IP%20Address%20on%20Linux%20%3A%20Discover%20the%20Many%20Ways)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fget-an-ip-address-on-linux%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fget-an-ip-address-on-linux%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/01/How-To-Set-Up-WireGuard-VPN-on-Linux.jpg)

### [How To Set Up WireGuard VPN on Linux](/wireguard-vpn/)

Learn how to set up WireGuard VPN on Linux to securely connect and access your network, in this step-by-step tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Useful-Linux-Dig-Examples-for-the-Network-Admin.jpg)

### [Linux Dig for Network Admins: Practical Guide with Examples](/linux-dig/)

Become a network troubleshooting expert! This comprehensive Linux Dig guide empowers you with essential DNS query commands for immediate results.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
