If Not True Then False

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. 1. Get Internal IP...


Leave a Comment

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

Input your comment.

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=""> <s> <strike> <strong>

Input your name.

Jonathan Kinney

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 Reply
J G Miller

But how reliable is ipecho.net???

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


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

html head
title 503 Service Unavailable /title
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


reply 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 Reply

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 Reply

Hi Jim,

Did you tried:

lynx --dump http://ipecho.net/plain
reply Reply

If you have curl installed:
$ curl ifconfig.me

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

reply Reply

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

reply Reply
Pi Home Server

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 Reply

Try ifcfg.me it supports more protocols:

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

reply Reply

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 Reply