Home > All, Linux > What’s my IP? Check your IP address from the command line.

What’s my IP? Check your IP address from the command line.

In an attempt to get out of writer’s block, I’m going to post my way of quickly looking up my public IP address from the command line. I know that there is a million ways to do this and thousands out there have a shorter line blah blah… so again, this is just my preference. This came in handy at work the other day when I shared it with a colleague who dropped it into a function in a file under /etc/profile.d/ on mutiple systems we work with. This way, any user of any system can have access to it.

echo -e "\n`wget -nv -O - http://checkip.dyndns.org/ 2>&1 | \
sed -n -e 's/.*\(.*\).*/\1/p'`\n"

Sample output of the above would be:

Current IP Address: 109.169.49.31

And of course, if you prefer to get just the IP address itself, it’s a simple modification, just change the sed part to:

sed -n -e 's/.*\(.*\)\s\(.*\).*/\2/p'`\n"

Alternate output:

109.169.49.31

UPDATE: What is my IP address from the command line, revisited.

Categories: All, Linux
  1. February 21st, 2011 at 20:02 | #1

    WhatIsMyIp.com, by default, scrambles the IP address to prevent all those scripts from killing the site’s front page. However, if one looks in the source code of the page, there is an HTML comment, which suggests:

    Please set your code to scrape your IP from http://www.whatismyip.com/automation/n09230945.asp For more info, please see our “Recommended Automation Practices” thread in the Forum.

    The URL provided in the comment contains only the IP address without any HTML formatting. This simplifies the script to the following:

    wget -q -O – http://www.whatismyip.com/automation/n09230945.asp

  2. Casey
    May 20th, 2011 at 17:22 | #2

    This is definitely a great substitute to the typical IP Checker tool online such as IP Elk. I do have other offline commands that I have in mind but I definite found yours to be more efficient and newbie friendly. Plus, it reminds me of php codes, which I have quite a familiarity of so thanks!

    Regards,
    Casey

  3. Chris Ergatides
    June 16th, 2011 at 06:12 | #3

    Leonid,

    If you click on your own link now, you will see why I choose do it my way as opposed to yours πŸ˜‰

  4. June 16th, 2011 at 10:21 | #4

    @Chris Ergatides They changed URL for automated scripts to a separate sub-domain. Your solution can have the same problem as it still relies on external URL (checkip.dydns.org), which you have no control off. πŸ™‚

  5. June 16th, 2011 at 10:23 | #5

    The updated solutions is:

    wget -q -O – http://automation.whatismyip.com/n09230945.asp

  6. Chris Ergatides
    June 16th, 2011 at 10:32 | #6

    You are barking up the wrong tree.

    DynDNS’s checkip tool has been accessible at the very same URL for at least 10 years.

    It is so stable that it is even built into certain devices.

  7. June 16th, 2011 at 11:27 | #7

    @Chris Ergatides Some Internet Service Providers block access to dynamic DNS services. And if you need a reliable solution which will never change the URL – nothing bits your own solution. PHP, as pretty much any other language, can do it in a single line of code. πŸ™‚

  8. July 13th, 2011 at 16:02 | #8

    Whats My IP is a great free site to easily locate your IP address

  9. December 31st, 2012 at 12:46 | #9

    Why not:

    curl http://smart-ip.net/myip

    I guess it’s shorter,no?

  10. August 3rd, 2013 at 12:11 | #10

    hi, any windows command for the same purpose will be helpful if someone can let me know

  11. February 9th, 2014 at 15:52 | #11

    Juul, goed artikel: lekkere brain verfrissing.
    We want more

  12. chaand
    March 5th, 2014 at 06:10 | #12

    I would like to add another command line to find out an external ip address :

    curl http://ip-details.com/plain; echo

  1. No trackbacks yet.