- Get an IP Address on Linux : Discover the Many Ways
- Prerequisites
- Get an IP Address on Linux with the curl Command
- Get an IP Address on Linux with the wget Command
- Digging Your Public IP Address with the dig Command
- Finding Your Private IP Address Using the ifconfig Command
- Writing a Bash Script to Find Your IP Address
- Conclusion
- How to Find or Check Your IP Address in Linux
- Find Your IP Address From the Command Line
- Using the hostname Command
- Using the ip addr Command
- Using the ifconfig Command
- Finding Your IP Address in Linux With a GUI
- How to Find Public IP Address
- What is a Public/Private IP Address?
Get an IP Address on Linux : Discover the Many Ways
Ransomware recovery test drive: This technical workshop is designed to take you behind the scenes and shows you how to adopt strategies to automate recovery, ensuring you’re ready to become a recovery hero. REQUEST YOUR LAB
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.
- NET-3 networking toolkit 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.
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 -s | grep -oE "\\b(9.)9\\b" -m1
Below, you can see your public IP address printed out.
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.
Now, you can share the IP address with your users so that they can connect to the server via SSH.
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.
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
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.
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.
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.
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.
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+/ ')"; # 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: $"; echo "Your public ip is: $";
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.
You will see an output similar to the following, showing your public and private IP addresses.
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.
How to Find or Check Your IP Address in Linux
An IP address is a codename assigned to a computer on a network. It works as a postal address, ensuring that network traffic is sent to the correct computer.
In this tutorial you will learn how to find your IP address using a Linux operating system.
Find Your IP Address From the Command Line
There are a few different commands you can use to check your IP address. To run these commands, start by opening a terminal window/command line.
Using the hostname Command
One way to check your IP address from the command-line is by using the following command:
The system will display your internal IP address.
Using the ip addr Command
Check your ip address with the ip addr command:
The system will scan your hardware, and display the status for each network adapter you have. Look for an entry that says link/ether. Below it, you should see one of the following:
inet6 fe80::a00:27ff:fe76:1e71/64
The entries will include one for a wired (Ethernet) adapter and a wireless (Wi-Fi) adapter. You may also have an entry for a virtual adapter. Generally, only one entry will have an IP address listed – that is the one you will want.
Note: The number after the slash – /24 and /64 – specifies the size of the network, and it helps with scanning and mapping network size.
Using the ifconfig Command
The third method to find your IP address involves using the ifconfig command. In the command line, enter the following:
The system will display all network connections – including connected, disconnected, and virtual. Look for the one labeled UP, BROADCAST, RUNNING, MULTICAST to find your IP address. This lists both IPv4 and IPv6 addresses.
Note: When you check your IP address, you may notice the term loopback. This refers to an IP address that returns traffic to the same computer. Usually, the loopback address is 127.0.0.1 . If you see that address listed, either you are looking at the wrong line, or you are not connected to the network.
Read phoenixNAP’s ultimate Linux IP command tutorial with 31 examples to learn more!
Finding Your IP Address in Linux With a GUI
If you are using a point-and-click interface to run your Linux system, you can check your IP address by following these steps:
1. Go to the Application menu and type Settings into the search bar.
2. Click on the Settings icon that appears among the results, as in the image below:
3. Next, find the Network tab in the Settings Menu and click on the Advanced Wired Settings icon.
4. This opens a new pop-up window with details on wired settings. Here you can find both your IPv4 address and your IPv6 address.
How to Find Public IP Address
To find your public IP address, reach out to an external website.
If you are using a browser, navigate to:
If you are using a command-line or terminal, use a retrieval command like curl or wget to display the external IP address:
wget -O - -q https://checkip.amazonaws.com
Note: Did you know that when you use curl to connect to an insecure website, the output responds with an error? To resolve it, visit our guide on making curl ignore certificates.
What is a Public/Private IP Address?
Each computer in a network is required to have its own IP address. If two systems have the same IP address, it will generate errors on the network. Most modern networks will detect the problem and prompt you to fix it. However, older networks might get confused, trying to route traffic to both computers.
Most systems will have two different IP addresses:
- Private IP address: This is the address used on the network. Also, it is the one you will want to use for configuring routing tools. On smaller networks, the internal IP address typically starts with 192.168.x.x. It is dynamically assigned to your computer whenever you connect.
- Public IP address: This is the IP address that registers on a website when you visit it. When you connect to a small network, your computer connects to a router using an internal IP address. That router, in turn, connects to a bigger network (like an Internet Service Provider), which has its own IP address system.
Note: Learn everything you need to know about public and private IP addresses in our article Public vs. Private IP Address.
Now you know how to find a private and public IP address in Linux using multiple methods.
Each option strikes a balance between speed and complexity. Now you have the flexibility to choose, if you only need your internal IP address, you can use the hostname method. If you need more detailed information, view our article on 31 Linux IP Commands with examples.