Linux: Get IP / IPv6 (internal/external) on Command Line/Shell - Comment Page: 2

This 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. [inttf_post_ad1] 1. Get Internal IP Address(es) on Linux Shell / Command Line 1.1 Get IP and IPv6 Address by Interface Returns plain IP addresses. /sbin/ifconfig $1 | grep "inet\|inet6" | awk -F' ' '{print $2}' | awk '{print $1}' ## Example usage ## /sbin/ifconfig eth0 | grep "inet\|inet6" | awk -F'...

36 comments on “Linux: Get IP / IPv6 (internal/external) on Command Line/Shell - Comment Page: 2

1 2
    1. Thank You.. Its very usefull..

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


      /sbin/ifconfig $(/sbin/route | awk '/default/ {print $8}') | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'

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

      Reply
      • Hi Jim,

        Did you tried:

        
        lynx --dump http://ipecho.net/plain
        
        Reply
      • If you have curl installed:
        $ curl ifconfig.me

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

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

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

      ifconfig eth0 | grep "inet adr" | awk -F: '{print $2}' | awk '{print $1}'

      Reply
    7. Try ifcfg.me it supports more protocols:


      curl ifcfg.me
      nslookup . ifcfg.me
      telnet ifcfg.me
      ftp ifcfg.me
      finger @ifcfg.me

      Reply
    8. 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′

      Reply
1 2

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close