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, […]

    2. another way :

      • Hi tommy,

        Really nice alternative url, thanks! :)

    3. For internal IP:

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

      and for external IP:

      curl -s

    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!

      • Hi Steve,

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

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

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

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

    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

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

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

        Works nice. Thanks for sharing this!

    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?

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

    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 netmask broadcast


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

      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/’


      CentOS 6.3… “”
      Fedora 17… ”″

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

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

      • Hi mahmoud,

        What server you are looking for?

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

        hostname -I

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.