DEV Community

Bo
Bo

Posted on

Get your public IP address from command line

If you want to know your outgoing public IP address, you could go to google and search "what's my ip", google will show your IP address.

If you don't want to use google, you could just use the command line. Two ways listed below (tested in Linux and MacOS):

1st way:

$ dig +short myip.opendns.com @resolver1.opendns.com
Enter fullscreen mode Exit fullscreen mode

2nd way:

$ curl ifconfig.co/
Enter fullscreen mode Exit fullscreen mode

For faster access, you could just put an alias in your shell config, e.g. ~/.bashrc or ~/.zshrc:

alias myip='curl ifconfig.co/'
Enter fullscreen mode Exit fullscreen mode

Next time you can just open your terminal then run:

$ myip
$ 67.83.127.63
Enter fullscreen mode Exit fullscreen mode

Discussion (1)

Collapse
peter279k profile image
peter279k • Edited on

We can also use other third-party services to query the public IP on our host:

$ curl ifconfig.me
$ curl icanhazip.com
$ curl icanhazip.com
$ curl ipecho.net/plain
Enter fullscreen mode Exit fullscreen mode

And we can use the host command to resolve OpenDNS service on our host to get the current public IP address:

host myip.opendns.com resolver1.opendns.com | grep "myip.opendns.com has" | awk '{print $4}'
Enter fullscreen mode Exit fullscreen mode