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.

/sbin/ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
 
## Example usage ##
/sbin/ifconfig eth0 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'
10.20.10.1

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

function int-ip { /sbin/ifconfig $1 | grep "inet addr" | awk -F: '{print $2}' | awk '{print $1}'; }
 
## Example usage ##
int-ip eth0
10.20.10.1

1.2 Get Every Interfaces IP Address

Returns every interface and IP address pairs.

/sbin/ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'
 
## Example output ##
eth0: 10.20.10.1
eth1: 10.20.1.168
lo: 127.0.0.1

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

function int-ips { /sbin/ifconfig |grep -B1 "inet addr" |awk '{ if ( $1 == "inet" ) { print $2 } else if ( $2 == "Link" ) { printf "%s:" ,$1 } }' |awk -F: '{ print $1 ": " $3 }'; }
 
## Example usage ##
int-ips
eth0: 10.20.10.1
eth1: 10.20.1.168
lo: 127.0.0.1

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.

lynx --dump http://ipecho.net/plain
 
## Example output ##
80.10.10.80

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

function ext-ip () { lynx --dump http://ipecho.net/plain; }
 
## Example usage ##
ext-ip
80.10.10.80

2.2 Get External IP Address Using Curl

Returns plain IP address.

curl http://ipecho.net/plain; echo
 
## Example output ##
80.10.10.80

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

function ext-ip () { curl http://ipecho.net/plain; echo; }
 
## Example usage ##
ext-ip
80.10.10.80
Follow If Not True Then False Updates!

28 Comments

  1. another way :
    curl ifconfig.me

    • Hi tommy,

      Really nice alternative url, thanks! :)

  2. For internal IP:

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

    and for external IP:

    curl -s icanhazip.com

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

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

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

  6. osam……..thank you

  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 checkip.dyndns.org’s structure is stable, unlike some other sites) —

    lynx -dump checkip.dyndns.org 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 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).

  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
    }
  12. How can I get a Server

    • Hi mahmoud,

      What server you are looking for?

  13. hostname -i

    • 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

  14. Thank You.. Its very usefull..

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

  16. 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]

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

  17. 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:

      lynx --dump http://ipecho.net/plain
  18. curl http://icanhazip.com/

Leave 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> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

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 …