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

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 whatismyip.org 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

29 Comments

  1. 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).

    Reply
  2. But how reliable is ipecho.net???

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

    [code]

    curl http://ipecho.net/plain; echo

    !DOCTYPE HTML PUBLIC “-//IETF//DTD HTML 2.0//EN”
    html head
    title 503 Service Unavailable /title
    body
    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 ipecho.net Port 80

    [/code]

    Reply
    • 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.

      Reply
  3. 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?

    Reply
    • Hi Jim,

      Did you tried:

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

    Reply

Trackbacks/Pingbacks

  1. ipcpu » Blog Archive » Linux: Get IP Address on Command Line/Shell (internal/external) - [...] get internal IP address and external IP address on Linux Shell / Command Line. See more …

Submit a Comment

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">