The public IP address is what you see online when you access them from your computer. If you want to try to connect to your computer via a remote connection that isn’t on the same network, you’ll need the public IP address.
You can easily determine the IP address or addresses of your Linux system by using the hostname, ifconfig, or ip commands. But If you want to know the public IP address like 192.168.1.1, you can send an HTTP request to one of the following web servers with the help of either netcat or curl command.
Basically cURL command is designed to work where you don’t need any user interaction. cURL is an abbreviation for Client URL Request Library. cURL is used to transfer data from one place to another place. It is a command line tool for receiving and sending files using URL syntax. It consists of different cURL Commands and libraries which can work with different protocols.
To install curl, you can use the following command:
Command: sudo apt-get install curl
If your Linux machine is connected directly to the internet, the public and private IP addresses will be the same. However, in most cases they differ.
Here are few examples through which you can easily your public IP address by using the curl command.
Command 1: curl bot.whatismyaddress.com -w “\n”
Command 2: curl ip.tyk.nu -w “\n”
Command 3: curl wgetip.com -w “\n”
Command 4: curl icanhazip.com
Command 5: curl whatismyip.akamai.com -w “\n”
Command 6: curl ipecho.net/plain -w “\n”
Command 7: curl ident.me -w “\n”
Command 8: curl -s http://ifconfig.me -w “\n”
The below example is one of the fastest method to get the public IP address using curl.
ifconfig.me is a web service that displays information about your connection, including IP address, hostname and User Agent string.
Other commands are variations on this are:
- Command: curl ifconfig.me/host for your remote hostname
- Command: curl ifconfig.me/ua for your User Agent
If you want to return all data then the command is “curl ifconfig.me/all”
And in case, if you get the response in json format, then the command is “curl ifconfig.me/all.json”
Command 9: curl -s icanhazip.com
Command 10: curl -s http://whatismijnip.nl | cut -d ” ” -f 5
There are a lot of options of different servers providing the external IP especially via HTTP posted here or elsewhere.
There’s yet another way to determine your public IP address. Perform a DNS lookup against myip.opendns.com. It’s a service provided by OpenDNS.
Command 11: dig +short myip.opendns.com @resolver1.opendns.com
There are actually four resolvers that can be used this way:
You can also achieve the same result with netcat as shown below:
Command 12: netcat icanhazip.com 80 <<< $’GET / HTTP/1.1\nHost: icanhazip.com\n\n’ | tail -n1
Alternatively, you can also run the following command if you want to get the response from Amazon AWS which is one of the most popular provider and is unlikely to go offline anytime soon.
Command: curl https://checkip.amazonaws.com
And in case, if you want to know your public IP address in a dialog popup instead of command terminal output, then run the following command in your terminal:
Command: zenity –info –text “$(curl -s icanhazip.com)”
With http://pasteip.me Web interface, you can see your local interface IP address along with your public IP address.