How To Get Your Public IP Into A Linux Bash Script

Laptop on a blue background with a Linux command prompt.
fatmawati achmad zaenuri/Shutterstock.com

You can 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 like “dig @resolver1.opendns.com myip.opendns.com +short” to get your IP from a DNS server.

You need your external IP address if you want to connect to your computer remotely. It’s easy to find it manually, but here’s how to find it from a Linux script.

Internal and external IP addresses

Internet Protocol addresses are used to identify devices on a network. They are 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 via its IP address.

Your broadband modem has a built-in network router that routes network traffic over the network from device to device. It is also the local authority that assigns 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 people – names are easier to work with than lists of numbers.

Devices on the internet also have an IP address. Some also have names, such as websites. Domain Name Service providers look up website domain names and automatically exchange them for IP addresses.

Your broadband modem gets its own Internet or external IP address by your Internet provider (IS P). Regardless of how many devices you have in your home that are connected to the Internet, their combined traffic all goes through that one IP address.

If you’re out of town and want to connect to a service that runs on one of the computers in your house, you’ll need to use your external IP address to do so. Of course, your router must be set up to route your connection request to the correct device in your home.

Unless you pay a little extra each month to your ISP for a static IP Address Your external IP address may change from time to time. Restarting your broadband modem may cause you to get a different external IP address. So if you need to know your external IP address, you can’t check and save it once. You should periodically determine what it is.

Find your external IP address

To discover your external IP address, you need to talk to something outside of your network. In other words, accessing something on the Internet that can give us the information we want. We need to look into the void and see what’s looking back at us. And then ask for our external address.

There are two ways we can do this. One way includes websites. There are many websites that will show you what your external IP address is, as well as a lot of other information. You can open it in your browser or use a command line tool like curl that can make HTTPS requests.

The other way is to use a special command like dig. The dig command queries DNS servers to retrieve information.

Using a browser

Of course, using a browser is not a script-friendly way to get your external IP address. But looking at a website that provides that service can give us useful information. We used to advise ip4.me but the site is not 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.

The ifconfig.me website displaying an external IP address

This reports our external IP address as 178.238.11.140. As you browse the web page, you will find a list of commands that you can use to get information from the site.

The ifconfig.me website with some cURL commands

The examples they give are all useful curl to query the site. So let’s take a look at using curl.

Using curl

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

To install it on Ubuntu type:

sudo apt install curl

Install Curl on Ubuntu

The command on Manjaro is:

sudo pacman -S curl

Install Curl on Manjaro

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

curl ifconfig.me

Get an external IP address with the default action of the ifconfig.me website

Our external IP address is retrieved and displayed in the terminal window. The output is bare. Not even a newline character is printed after the string. The command prompt is 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 may get a different result back. To accommodate this, we can specify that we ask for our IP address by appending the “ip” identifier to the URL.

curl ifconfig.me/ip

Get an external IP address using the ifconfig.me website with the ip specification

This returns the IP address as before.

This illustrates the problem with using a website as the source of your IP address. Websites may close or they may change the way they work or the format of the information returned. These changes cause scripts that rely on these sites to fail or behave unpredictably.

Using a reputable and reliable source like a DNS server is a more robust way to get your external IP. To query a DNS server, we need to use the dig command.

RELATED: What is DNS and should I use a different DNS server?

Using the dig command

This time, dig was installed on Fedora and Ubuntu, and we only needed to install it on Manjaro.

The command is the usual pacman command, so no surprises there, but the package name isn’t what you’d expect.

sudo pacman -S bind-tools

Install Dig on Manjaro

To use dig to discover our external IP address, we need to point it to a DNS server. We use 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 want to retrieve. In this case it’s ‘myip’. The +short option ensures that we get a concise answer, not a comprehensive answer.

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

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

This time our IP address has a new line behind it. As we’ll see, this is printed after the IP address string, it’s not an integral part of the string itself.

RELATED: How to use the dig command on Linux

Using this in a script

There are many reasons why you might want to know your external IP address from a script. You may have a script that checks if your external IP address has changed and notifies you when that happens. Or maybe a server warns its customers when its address changes. Whatever your reasons, we can implement the commands we used on the command line in a script quite easily.

To get our external IP address and assign it to a variable we just need to put the command in a command substitution, $(...) and assign it to a variable, like this:

variable=$(...)

The command in parentheses is executed and the return value is replaced by the expression. In this example, the command has been simplified to ‘variable=return value’.

RELATED: Working with variables in Bash

Here it is in a script. We have the . added -s (silent) option to the curl command to prevent it from reporting data retrieval progress.

#!/bin/bash

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 with the chmod order.

chmod +x getex1.sh

Make a script executable with chmod

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

./getex1.sh

Running the getex1.sh script

To do the same with the more robust option of using a DNS server instead of a website, we just need to curl command with the dig a.

#!/bin/bash

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 with chmod.

chmod +x getex2.sh

Make a script executable with chmod

Let’s run this script.

./getex2.sh

Running the getex2.sh script

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

Go for reliability

It is always safer to use an authorized service that is reputable and has a predictable output format than to use an “unofficial” website. Just like everything else on the internet, pay close attention to who you get your information from.’

RELATED: Make Linux scripts detect that they are running in virtual machines

Add Comment