Quick Links
Summary
you may get your external IP from a website with the curl command like “curl -s ifconfig.me”. However, websites can change. For a more reliable answer, use the dig command instead like “dig @resolver1.opendns.com myip.opendns.com +short” to get your IP from a DNS server.
You’ll need your external IP address if you want to remotely connect to your computer. Finding it manually is easy, but here’s how to find your it from within a Linux script.

Internal and External IP Addresses
Internet Protocol addressesare used to identify devices on a network. They’re a unique label on the network, assigned to a single device. If another device wants to send data to that device, it can do so using its IP address.
Related:How Do IP Addresses Work?
Your broadband modem has a network router built into it that directs the network traffic around the network from device to device. It’s also the local authority that allocates IP addresses to devices when they join the network. It also maintains a table of network names and IP addresses. This allows you to give meaningful names to the computers on your network, because—for humans—names are easier to work with than lists of numbers.
Devices on the internet also have an IP address. Some of them have names too, such as websites.Domain Name Serviceproviders look up website domain names and swap them for IP addresses, automatically.

Your broadband modem is given its own internet-facing or external IP address by yourInternet Service Provider(ISP). Regardless of the number of devices you might have in your home that are connected to the internet, their combined traffic all goes out through that single IP address.
If you’re out of town and want to connect to a service you’ve got running on one of the computers in your home, you’ll need to use your external IP address to do so. Yourrouterwill need to be set up to route your connection request to the appropriate device inside your home, of course.

Unless you pay a little extra to your ISP each month for astatic IP addressyour external IP address may change from time to time. Rebooting your broadband modem might well result in your getting a different external IP address. So if you need to know your external IP address, you’re able to’t just check it once and store it. You’ll need to determine periodically what it is.
Finding Your External IP Address
To discover your external IP address means talking to something that is outside of your network. In other words, accessing something on the internet that can give us the information we want. We need to peer into the void and see what’s looking back at us. And then ask it for our external address.
There are two ways we can do this. One way involves websites. There are a lot of websites that will show you what your external IP address is, and a bunch of other information too. You can access these in your browser, or use a command-line tool likecurlthat can makeHTTPSrequests.
The other way is to use a dedicated command likedig. Thedigcommand interrogates DNS servers to retrieve information.
Using a Browser
Of course, using a browser isn’t a script-friendly way to obtain your external IP address. But looking at a website that delivers that service can give us useful information. We used to recommendip4.mebut the site hasn’t been updated to HTTPS. It still uses the older, insecureHTTP. The site still works, but there are better alternatives now.
Theifconfig.mesite provides a good set of information.
This is reporting our external IP as 178.238.11.140. Scrolling down through the webpage you’ll find a list of commands that you can use to retrieve information from the site.
The examples they give all usecurlto interrogate the site. So let’s take a look atusingcurl.
Using curl
On our test machines,Fedora 37already hadcurlinstalled. We needed to install it on our Ubuntu and Manjaro computers.
The command on Manjaro is:
We can try this out with the first command listed on theifconfig.mewebpage.
Our external IP address is retrieved and displayed in the terminal window. The output is bare-bones. There isn’t even a newline character printed after the string. The command prompt is butted right up against the IP address.
Related:How to Use curl to Download Files From the Linux Command Line
This command works because returning the IP address is the default action of the website. If the default action ever changes, we might get a different result returned to us. To cater to this, we can specify we are requesting our IP address by adding the “ip” identifier to the URL.
This returns the IP address as before.
This illustrates the problem with using a website as the source of your IP address. Websites can close down or they can change the way they operate, or the format of the returned information. These changes will makescriptsthat depend on these sites to either fail or behave unpredictably.
Using a reputable and dependable resource like aDNSserver is a more robust way to obtain your external IP. To query a DNS server we need to use the dig command.
Related:What Is DNS, and Should I Use Another DNS Server?
Using the dig Command
This time,digwas installed on Fedora and Ubuntu, and we only had to install it on Manjaro.
The command is the usualpacmancommand, so no surprises there, but the package name is not what you might expect.
Tousedigto discover our external IP address, we need to point it to a DNS server. We’re using theOpenDNS server, which is provided by Cisco.
We need to specify the name of the DNS server we want to use, preceded by an at sign “@.” We also need to name the record type we wish to retrieve. In this case it is “myip.” The+shortoption ensures we get a terse response, and not a verbose one.
This time, our IP address has a newline character printed after it. As we’ll see, this is printed after the IP address string, it isn’t an integral part of the string itself.
Related:How to Use the dig Command on Linux
Using These in a Script
There are many reasons why you might want to know your external IP address from inside a script. Perhaps you have a script that monitors whether your external IP address has changed, and it notifies you when that occurs. Or perhaps a server notifies its clients when its address changes. Whatever your reasons, we can implement the commands we’ve used on the command line inside a script quite easily.
To retrieve our external IP address and assign it to avariable, we only need to wrap the command inside a command substitution,$(…), and assign it to a variable, like this:
The command inside the parentheses is executed and the return value is substituted for the expression. In this example, the command is simplified to “variable=return value.”
Related:How to Work with Variables in Bash
Here it is in a script. We’ve added the-s(silent) option to thecurlcommand to prevent it from reporting the progress of the data retrieval.
extaddr=$(curl -s ifconfig.me)
echo “The external IP address is $extaddr (from cURL)”
Copy this script to an editor, and save it as “getex1.sh”, and make it executable withthechmodcommand.
Let’s execute the script and see what we get.
To do the same with the more robust option of using a DNS server rather than a website, all we need to do is substitute thecurlcommand with thedigone.
extaddr=$(dig @resolver1.opendns.com myip.opendns.com +short)
echo “The external IP address is $extaddr (from dig)”
Save this as script “getex2.sh” and make it executable withchmod.
Let’s execute this script.
We can see from the output of the two scripts that despite the commanddigprinting a newline character on the command line, in the script there is no newline added to theextaddrvariable.
Go For Dependability
It’s always safer to use a recognized service that is reputable and has a predictable output format than to use an “unofficial” website. Like everything else on the internet, have a good look at who you’re getting your information from.'
Related:How to Let Linux Scripts Detect They’re Running in Virtual Machines