dig
1dig +short myip.opendns.com @resolver1.opendns.com
This asks the IP address of myip.opendns.com
from the name server resolver1.opendns.com
(something you trust), which will return your external IP address.
curl
works over HTTP, and therefore less efficient than the direct DNS query with dig.
curl
You can curl an endpoint for your external public IP, like so:
1curl http://ipecho.net/plain; echo
There are plenty of services that give you your public IP address by going to a URL. You can then curl that URL to see your external public IP in CLI.
Some alternatives are:
1curl https://icanhazip.com/
2curl http://ifconfig.me/ip
3curl http://ip.appspot.com/
4curl http://ip-addr.es/
5curl http://eth0.me/
6curl http://curlmyip.com/
7curl http://checkip.dyndns.org/
8curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+" # get onlt the IP digits
You can also get other details as well. For example:
1curl ifcfg.me/all
will give you your ISP and Country as well
icanhaz
should be the most preferred since it works over SSL (https). It also seems to be well monitored for uptime.1https://wtfismyip.com/json
OR
1curl https://wtfismyip.com/json