Internal and External IP Addresses

Internet Protocol addresses are 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.

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 Service providers 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 your Internet 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. Your router will 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 a static IP address your 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 can’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 like curl that can make HTTPS requests.

The other way is to use a dedicated command like dig. The dig command 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  recommend ip4.me but the site hasn’t been updated to HTTPS. It still uses the older, insecure HTTP. The site still works, but there are better alternatives now.

The ifconfig.me site 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 use curl to interrogate the site. So let’s take a look at using curl.

Using curl

On our test machines, Fedora 37 already had curl installed. We needed to install it on our Ubuntu and Manjaro computers.

To install it on Ubuntu type:

The command on Manjaro is:

We can try this out with the first command listed on the ifconfig.me webpage.

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.

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 make scripts that depend on these sites to either fail or behave unpredictably.

Using a reputable and dependable resource like a DNS server 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, dig was installed on Fedora and Ubuntu, and we only had to install it on Manjaro.

The command is the usual pacman command, so no surprises there, but the package name is not what you might expect.

To use dig to discover our external IP address, we need to point it to a DNS server. We’re using the OpenDNS 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 +short option 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 a variable, 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 the curl command to prevent it from reporting the progress of the data retrieval.

Copy this script to an editor, and save it as “getex1.sh”, and make it executable with the chmod command.

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 the curl command with the dig one.

Save this as script “getex2.sh” and make it executable with chmod.

Let’s execute this script.

We can see from the output of the two scripts that despite the command dig printing a newline character on the command line, in the script there is no newline added to the extaddr variable.

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