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

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: 1

1 2
    1. […] get internal IP address and external IP address on Linux Shell / Command Line. See more here: Linux: Get IP Address on Command Line/Shell (internal/external) Tags: address-and, command, command-line-, guide, guide-also, howto-get, howto-make, […]

      Reply
    2. another way :
      curl ifconfig.me

      Reply
      • Hi tommy,

        Really nice alternative url, thanks! :)

        Reply
    3. For internal IP:

      ifconfig eth0 | sed ‘/inet addr/!d;s/.*addr:\(.*\) Bcast.*$/\1/’

      and for external IP:

      curl -s icanhazip.com

      Reply
    4. Nice tips, thanks! Beware that “ifconfig” might not be found in everyone’s PATH – in my distro here (RHEL5) it’s “/sbin/ifconfig” and “/sbin” isn’t in a non-root user’s PATH by default. Hence it’s safest to always specify the full command pathname as suits your distro.
      Just pointing out so as to avoid those irksome “command not found” errors!

      Reply
      • Hi Steve,

        Nice note and yes you are right, “/sbin” isn’t in a non-root user’s PATH by default on RHEL 5.

        Reply
    5. Awesome blog. It really helps me a lot. Keep it up. Thanks for sharing knowledge.

      Reply
      • Hi Ankit and thanks! Nice to hear that this helped you. :)

        Reply
    6. thanks for the information, at least i can now know my Linux IP.

      Reply
    7. There’s also a neat little utility that allows tracking the IP addresses of the machine it’s installed on. You can get the IP addresses via Email or have them written to a MySQL database. Here’s the link

      Reply
    8. Here’s the one I’ve used for years (thankfully checkip.dyndns.org’s structure is stable, unlike some other sites) —

      lynx -dump checkip.dyndns.org 2>&1 | awk '{print $4}' | grep ^[0-9]
      Reply
      • Hi Steve and nice to see you here. :)

        Works nice. Thanks for sharing this!

        Reply
    9. ifconfig eth0 | sed ‘/inet addr/!d;s/.*addr:\(.*\) Bcast.*$/\1/’ ??? Does not work on Fedora 17! Maybe something is getting wacked in the text/font translation on this web page?

      Reply
      • Hi Schorschi,

        
        /sbin/ifconfig eth0 | sed '/inet addr/!d;s/.*addr:\(.*\) Bcast.*$/\1/'
        

        Command works for me, but you should select right device, like eth0, eth1, wlan0 etc.

        Reply
    10. Works fine on CentOS 6.3. But on Fedora 17 returns nothing. Fedora does not reference ‘addr’ substring, in fact Fedora 17 has changed a lot things, including not using UDEV rules for NIC interface assignment.

      Fedora 17…

      eth0: flags=4163 mtu 1500
      inet 192.168.1.2 netmask 255.255.255.0 broadcast 192.168.1.255

      CentOS…

      eth0 Link encap:Ethernet HWaddr 00:0C:29:1D:6C:C8
      inet addr:192.168.1.57 Bcast:192.168.1.255 Mask:255.255.255.0

      For Fedora 17, I tweaked it… not knowing regular expressions well, I am not sure this is optimal?

      /sbin/ifconfig eth0 | sed ‘/inet /!d;s/.*inet \(.*\) netmask.*$/\1/’

      What would be great is an expression that works on both? I sure there is a way to do it, just way above my knowledge of regular expressions. Maybe something like…

      /sbin/ifconfig eth0 | sed ‘/inet /!d;s/.*[addr:|inet]\(.*\) [netmask|Bcast].*$/\1/’

      Results…

      CentOS 6.3… “192.168.1.2”
      Fedora 17… ” 192.168.1.2″

      But the above leaves a leading space for IP address on Fedora 17. But I don’t see an obvious way to fix that (again I am not a regular expression guru).

      Reply
    11. 
      showips() {
      names=(`ifconfig | grep "lo\|eth" -A 1 | awk -F" " '{print $1}' | grep -v "inet\|-" | sort -u`)
      
      for i in "${names[@]}"
      do
       echo $i interface has ip address: `ifconfig | grep $i -A 1 | grep addr | awk -F" " '{print $2}' | awk -F":" '{print $2}'`
      done
      }
      
      Reply
    12. How can I get a Server

      Reply
      • Hi mahmoud,

        What server you are looking for?

        Reply
      • I found that the -i flag only returned 127.0.0.1, but a capital i (-I) flag returned the correct internal ip address of the active interface:

        hostname -I

        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