How to Find Your Public IP Address with the Linux Command Line

This brief tutorial explains how you can quickly determine your IP address via the Linux command line.

Sometimes I need to find out what the public IP address of a particular machine is. If I have access to a web browser on that machine I just use a service like https://www.whatismyip.com to find this information. However, on a remote Linux server it is not practical to use the web browser method. So instead I use the following hack. Note: this also works in macOS if you install wget.

I enter the following line into a Linux command prompt:

# wget -q -O – checkip.dyndns.org|sed -e ‘s/.*Current IP Address: //’ -e ‘s/<.*$//'
122.172.9.222

Pretty useful stuff this. If you need to use this command often it might be a good idea to create a script with the command so that you need not have to remember it. Create a new file using your favorite text editor. Enter the following lines:

#!/bin/bash
wget -q -O – checkip.dyndns.org|sed -e ‘s/.*Current IP Address: //’ -e ‘s/<.*$//'

Now save the file as something like /usr/bin/myipaddress. Grant it executable privileges like this:

# chmod +x /usr/bin/myipaddress

If you’re using macOS, copy the myipaddress file to /usr/local/bin instead of /usr/bin/ and make sure to grant it executable privileges too.

Now you have a script that you can use by running myipaddress in the command line. It will output the current public IP address your Linux machine is using. This can be quite useful if you use things like IP-based authentication, or if you use a dynamic DNS service.


If this article helped you, I'd be grateful if you could share it on your preferred social network - it helps me a lot. If you're feeling particularly generous, you could buy me a coffee and I'd be super grateful :)

buy a coffee for simplehelp.net


Home » Linux » How to Find Your Public IP Address with the Linux Command Line

4 thoughts on “How to Find Your Public IP Address with the Linux Command Line”

  1. Sorry for the delayed reply. You can also use the following command to get your public IP:

    wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Leave a Comment

Your email address will not be published.