How to find your public IP address with the Linux command line

by Sukrit Dhandhania on April 7, 2009

Linux

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 hope over to a service such as http://www.whatismyip.com or myip.dk 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.

I enter the following line into a Linux command line:

# curl -s myip.dk | grep '"Box"' | egrep -o '[0-9.]+'
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
curl -s myip.dk | grep '"Box"' | egrep -o ‘[0-9.]+'

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

# chmod +x /usr/bin/myipaddress

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

Although the above example should work most of the time, there might be times when the service that gives you your public IP address is down or has changed the format with which they display the data, thus breaking the script. Here’s a backup method in case the command shown earlier does not work for you. It used the service http://www.formyip.com/.

# links -dump http://www.formyip.com/ | awk "/IP is/{print $NF}"

You could also use this with lynx:

# lynx -dump http://www.formyip.com/ | awk "/IP is/{print $NF}"

Again, feel free to enter this command into a script like I showed you earlier. It will make using the command a breeze.

  • Pingback: Find your external IP from the CLI « Linux & Stuff

  • http://www.howtodividebyzero.com/ Mike

    When I copy-pasted the script into my own file, I saw that your quote marks don’t match up.

    … ‘[0-9.]+’

    I changed the first quote before the [ character to ', making the final product read:

    #!/bin/bash
    curl -s myip.dk | grep '"Box"' | egrep -o '[0-9.]+’

    By the way, I apologize for the slight messiness of this, but it’s very difficult to quote a quote mark. ;)

  • spuffler

    … evidently, the author doesn’t read our replies, otherwise, I’d have had a successful execution of the script file, because I would have copied corrected text. …

  • Tom

    Using ifconfig works just as well, you’ll still need to grep for the proper lines, and then possibly use sed to extract the actual IP address as this script does, and it doesn’t rely on a specific site actually being up…

  • http://sevenacross.com Sukrit D

    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/<.*$//'

  • Sionide21

    You can use
    http://www.whatismyip.org/
    To get just the ip in plaintext form.

  • ivan

    A very short way, based on previous posts:
    wget -q -O – whatismyip.org;echo

  • Nils Breunese

    Even shorter:

    curl whatismyip.org

  • coyotus

    curl ifconfig.me

    for useragent: curl ifconfig.me/ua

  • http://ms1.gotdns.com jonny rocket

    thanks for the info dude.

  • http://www.my-ip.org.uk/ James

    Great stuff, thanks

  • daz

    cool one liner.
    well done.
    really got my imagination running with that one..

    daz

  • raT

    curl -s myip.dk | grep ‘”ha4″‘ | egrep -o ‘[0-9.]+’ |tail -n1