The theme of knowing, or finding out the IP that we have is something recurrent. Let's see how to do it on a Linux device.
In this article I am going to teach you how to check the public IP in the browser, with the console and how to obtain it and save it in our .sh scripts with BASH
In addition to this, we will also see how to check our private IP and the difference between the two.
public vs private IP
The public or external IP is the IP that identifies us with the outside of our network. How the rest of the people would see our router.
On the other hand, the private, internal or local IP (call it what you want) is the one that the router assigns to each device that is connected to it.
Thus, it is the case that each device on a network has a different private IP but the same public IP that is assigned to the router.
How to see the public ip
There are different ways. Remember that the IP is like the address of our house. You should not facilitate it just because. For example, the ips that you see in the images of the article are not mine, I have changed it using TOR so that nobody knows my IP.
This is the traditional way. When you need to know your IP, in case you have been banned from a service, etc, etc. Search on Google What is my ip or What is my ip and when entering any of the first results they will give it to us.
Or enter one of these addresses.
From the terminal
With the curl command. As simple as calling certain websites that return the IP
Websites that we can call to get the IP back
There are many more if you are interested in knowing more I make a compilation.
And if you get an error because you don't have curl installed, you can install it with
sudo apt update sudo apt install curl
Another way to get the ip from the CLI, let's go to the terminal, is using the wget command. Just like with curl we can use
wget -qO- ifconfig.co
Save public IP in BASH
If you need to get and save its ip in a variable a .sh script in BASH you can use for example the following code
echo "Tu ip actual es" ip="$(curl --silent icanhazip.com)" echo $ip
and we will have the public ip in a variable ready to compare or do whatever we want.
How to see the private ip
We have already seen that the private IP is the one that the router assigns to each device on the network, so if we want to do any network work we will need to know our local IP. As always in Linux we have different options to get things. I leave the best known.
with host name
The most direct. type in the terminal
With this simple command
In the image you can see what the console returns and our private IP marked in red.
with ip route
Another option is to use
As with the ifconfig, I have highlighted the private IP in red, and of course, the two IPs obtained by different methods must be the same.
If you need anything else about IP, or want to share a method, leave a comment.