Knowing the IP address of our computer is a basic task to establish network connections. But remember that there is also a public one with which we access the Internet, and doing it from the terminal can be an interesting task. Today, you will learn how to know the external IP address using the terminal.
Public IP vs. Private IP
In a typical network, the router uses a public IP address to identify it to the rest of the Internet. However, this same home router allows many devices to connect to this same router by assigning it another range of IP addresses that are known as private.
So, the device has a private IP address to communicate with the internal network, but the router uses a different one to access the Internet. Here is the difference between the private IP and the public IP.
To know the private IP from the terminal, it would be enough to use the ipconfig command. But in the case of the external or public IP, the process is a little different.
I will show you several ways to do it.
Check External IP using the Linux Command Line
1.- Using the dig command
The reality is that there are many commands and ways to do this process. The first way to do this is by using the dig
command.
In this case, the dig
command will make a request to a website that will provide us with the external or public IP address.
dig +short myip.opendns.com @resolver1.opendns.com
The output on the screen will simply be, the IP address you are looking for. For example:
111.25.41.44
This process may take a while, depending on your Internet connection, but it should be quick.
2.- Using cURL
The cURL command is a utility for Linux systems that is a Swiss army knife for handling connections using the terminal.
To achieve this, just use it with an Internet service such as ifconfig.co/
.
First, install cURL. If you use Debian, Ubuntu, Ubuntu, Linux Mint or any derivative of the family.
sudo apt install curl
In case of RHEL, Rocky Linux, Alma Linux, Oracle Linux, Fedora, and CentOS
sudo dnf install curl
For users of Arch Linux, Manjaro and derivatives:
sudo pacman -S curl
And for openSUSE
sudo zypper in curl
Then, verify the external IP with cURL as follows:
curl ifconfig.co/
Sample Output:
111.12.87.44
To obtain the IPv6 address, you can use this command
curl -6 ifconfig.co/
3.- Using the wget command
Another option is to use the wget command that comes installed in almost all major Linux distributions out there. In this case, you would also use an external service.
wget -4 -qO - icanhazip.com
Sample output:
111.12.87.44
In case you also have an IPv6 address, then just use.
wget -6 -qO - icanhazip.com
Store your external IP address into a variable
For configuration scripting and other tasks, it can be quite useful to know how to store this information in a variable.
To achieve this, you would just have to choose one of the presented methods and store it in a variable of your choice.
For example:
myexternalip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
And then, display it
echo $myexternalip
Of course, you can change the variable name and use it in scripts and such.
Create an alias to quickly get the external IP address.
Another easy solution is to create a command alias to make it easier to remember and use.
Open the bashrc
file and add the alias
nano ~/.bashrc
And at the end of the file, add these lines
alias myip='curl ifconfig.co/'
Adjust the name of myip
to whatever you want. Save the changes and close the file.
And now just run myip
and you will have the address.
Conclusion
The Linux command line is an important utility that allows you to quickly do tasks such as verifying the IP address.
I hope you liked this post and help to spread it.