Skip to content

How To: Get Your Public IP Address From The Command Line

    There have been a lot of circumstances where I wanted to get my public-facing IP from a computer that didnt have a web browser. In this How To Article, I will show you all the ways you can get your public IP from a great many shells.

    How To Get Your Public IP From PowerShell

    This one-line command will get you your puiblic IP by making an API call to https://api.ipify.org:

    (Invoke-WebRequest -uri "https://api.ipify.org/").Content

    Expected output is a single IP address.

    How To Get Your Public IP From Linux Shell

    I spend a ton of time in Linux. Linux is my love language. This works on any Linux distribution that has dig installed. This command uses Dig to ask OpenDNS what your public IP is. The command is as follows:

    dig +short myip.opendns.com @resolver1.opendns.com

    Output of the command is a single IP, like the PowerShell command from above.

    Conclusion

    The above two examples can be run from the command line as one-off commands, or they could be built into a script for various purposes. I have a library of onboarding scripts that I run for various tasks where I need to know what my public IP is, so I have both variations in my toolkit.

    Did this script work for you? Did you find it useful? Subscribe to our newsletter to get more scripts, alerts, tips and tricks!