Linux: Get IP (internal/external) on Command Line/Shell

terminal-logo-smallThis is quick tip, howto get internal IP address and external IP address on Linux Shell / Command Line. This guide also show, howto make useful Bash functions to get IP addresses quickly.

Note: All functions could be named as you wish and to make functions permanent, add functions to ~/.bashrc or /etc/bashrc. Also all awk commands should work also with gawk and nawk.

1. Get Internal IP Address(es) on Linux Shell / Command Line

1.1 Get Single IP Address by Interface

Returns plain IP address.

Create simple bash function (example int-ip) with following command.

1.2 Get Every Interfaces IP Address

Returns every interface and IP address pairs.

Create simple bash function (example int-ips) with following command.

2. Get External IP Address on Linux Shell / Command Line

I use here service.

2.1 Get External IP Address Using Lynx

Returns plain IP address.

Create simple bash function (example ext-ip) with following command.

2.2 Get External IP Address Using Curl

Returns plain IP address.

Create simple bash function (example ext-ip) with following command.

Share this post


  1. Thank You.. Its very usefull..

  2. Or, how about select the interface in the default route:

    Good for scripting against the most likely candidate to be the main IP address, not matter if you are on a machine that uses bonding (bond0) or the more simple, and often used in VMs, single interface setup (eth0).

  3. But how reliable is

    2013-11-07 15:34:43h UTC


    curl; echo

    html head
    title 503 Service Unavailable /title
    Service Unavailable
    The server is temporarily unable to service your
    request due to maintenance downtime or capacity
    problems. Please try again later.

    Apache Server at Port 80


    • Hi J G Miller,

      Maybe, there is not any 100% uptime web site/service with this info, so easiest way is do couple fallbacks, if you want to use this on some critical place.

      Or if you have own reliable server(s) online, then you can easily return this info.

  4. Hey guys I’m looking for some help. I can not find the external ip address of my Rasberry pi. any ideas on what I can do to find it?

    • Hi Jim,

      Did you tried:

    • If you have curl installed:
      $ curl

      This is the method I use for scripts that need my public ip.

  5. Reply
  6. On recent Debian (Wheezy), you should not match on ‘inet addr’ but ‘inet adr’.

  7. Thanks for the command. On Debian you have to do a grep for “inet adr” instead “inet addr”

    • You beat me to it… I was going to suggest

      wget -q -O – 2>/dev/null ;echo

  8. Try it supports more protocols:

  9. Here’s a shorter yet equally as effective way to get a single IP address by interface:
    /sbin/ifconfig $1 | perl -nle’/dr:(\S+)/ && print $1′


Submit a Comment

Your email address will not be published. Required fields are marked *