However, websites can change.

You’ll need your external IP address if you want to remotely connect to your rig.

Finding it manually is easy, but here’s how to find your it from within a Linux script.

Laptop on a blue background showing a Linux command prompt.

fatmawati achmad zaenuri/Shutterstock.com

Internal and External IP Addresses

Internet Protocol addressesare used to identify devices on a web link.

They’re a unique label on the web connection, assigned to a single machine.

If another equipment wants to send data to that equipment, it can do so using its IP address.

The ifconfig.me website displaying an external IP address

Related:How Do IP Addresses Work?

It’s also the local authority that allocates IP addresses to devices when they join the connection.

It also maintains a table of internet names and IP addresses.

The ifconfig.me website displaying a et of cURLcommands

Devices on the internet also have an IP address.

Your broadband modem is given its own internet-facing or external IP address by yourInternet Service Provider(ISP).

Rebooting your broadband modem might well result in your getting a different external IP address.

Installing curl on Ubuntu

You’ll need to determine periodically what it is.

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.

Installing curl on Manjaro

And then ask it for our external address.

There are two ways we can do this.

One way involves websites.

Fetching an external IP address using the ifconfig.me website’s default action

you might access these in your internet tool, 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.

Fetching an external IP address using the ifconfig.me website with the ip specfier

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.

Installing dig on Manjaro

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.

Retrieving an external IP address from a DNS server with the dig command

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.

Making a script executable with chmod

We needed to install it on our Ubuntu and Manjaro computers.

Our external IP address is retrieved and displayed in the terminal window.

The output is bare-bones.

Running the getex1.sh script

There isn’t even a newline character printed after the string.

The command prompt is butted right up against the IP address.

If the default action ever changes, we might get a different result returned to us.

Making a script executable with chmod

This returns the IP address as before.

This illustrates the problem with using a website as the source of your IP address.

These changes will makescriptsthat depend on these sites to either fail or behave unpredictably.

Running the getex2.sh script

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?

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 also need to name the record pop in 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.

Or perhaps a server notifies its clients when its address changes.

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.

Let’s execute the script and see what we get.

Let’s execute this script.

Related:How to Let Linux Scripts Detect They’re Running in Virtual Machines