Read UNIX/Linux system IP address in a shell script

by nixcraft · 19 comments

Reading an IP address in shell script required many time. However, different Linux distribution stores IP address in different files. If you are looking to run script under different UNIX like OSes such as Solaris or FreeBSD then you need to use ifconfig command. ifconfig not just use to configure a network interface but it can be use to obtained information such as network IP, netmask etc. Here are different command for different UNIX/Linux/BSD OSes in shell scripts to read IP address:

Linux:

ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' |
cut -d: -f2 | awk '{ print $1}'

FreeBSD/OpenBSD:

ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' |
awk '{ print $2}'

Solaris:

ifconfig -a | grep inet | grep -v '127.0.0.1' |
awk '{ print $2}'

See read-ip-address.bash script which reads/finds an IP address from diffrent UNIX/Linux OSes.

How it works?
Here you used something called pipeline. A pipeline is a set of processes chained by their standard streams, so that the output of each process ("stdout") feeds directly as input ("stdin") of the next one (see pipe at wikipedia for more information).

  1. ifconfig command list all network interfaces.
  2. From the output of ifcomfing command, find out IPv4 IP address using grep command (grep 'inet addr:').
  3. Next, you do not need loopback IP address (127.0.0.1) so again with the help of grep –v you invert the sense of matching, to select all non-matching lines.
  4. Finally, awk command selects an IP address. (awk '{print $2}')

Featured Articles:

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our daily email newsletter to make sure you don't miss a single tip/tricks. Subscribe to our weekly newsletter here!

{ 1 trackback }

PC Thoughts
02.10.09 at 6:53 am

{ 18 comments… read them below or add one }

1 Anonymous 01.17.06 at 9:43 pm

The process you listed would only give you the local address, if you are behind a firewall that uses NAT.

To get your public IP address try out the following bash script that I wrote a while ago:

#!/bin/bash

wget whatismyip.org
read T1 http://www.suramya.com

2 nixcraft 01.18.06 at 12:42 am

Suramya,
Script is ok, but you can do that with one line command:

lynx –dump http://whatismyip.org/

Or better create a alias:

alias myip=’lynx –dump http://whatismyip.org/

3 Anonymous 01.18.06 at 12:48 am

True, This script is actually a portion of another script that sends out an email when my public IP changes. I just copied the portion that actually gets the IP and pasted it here.

That being said your version of the script is a lot cleaner. Mind if I ’steal’ it for use in my scripts? ;)

Thanks,
Suramya

4 nixcraft 01.18.06 at 8:53 am

Mind if I ’steal’ it for use in my scripts? ;)

Heh no problem, use it in your script :)

5 Anonymous 01.19.06 at 1:27 pm

@nixcraft. nice tip/hint :D I would like to see more shell scripting stuff here. keep it up good work

6 Michael Gauthier 06.15.06 at 3:24 am

If you don’t have lynx installed and want to use wget without messy temporary files, you can use:

wget -qO – http://whatismyip.org/

7 Clue 12.18.07 at 12:52 am

Hi there,

I’m new to all this and I was wondering, how do those websites get our IP and is there not a shell script that can be written to find out your public IP depending on your private IP without having to use these external websites?

8 TJB 01.16.08 at 6:41 pm

Dude, you just saved me 15 minutes of relearning awk and the like just to pull out the IP addy. Double thanks!

9 lex 01.18.08 at 10:36 pm

@Clue
no. for as far as I know you’ll always need some external server to ’see’ who you are.
But if someone knows another way to do this, the n please let me know.

10 igor 01.30.08 at 6:18 pm

if you use a different locale, consider this version for linux:

LC_ALL=C ifconfig | grep ‘inet addr:’| grep -v ‘127.0.0.1′ | cut -d: -f2 | awk ‘{ print $1}’

11 Oscar 02.01.08 at 9:42 pm

@the suggestion of using whatsmyip.

That is a great idea, but if you’re looking to find your ip address on a box that has no internet access then that would not work and the original script(s) should always work. If you’re on a private lan for example with no web access then you can’t see whatsmyip, or ipchicken, etc.

This quick how-to is not so much on how to find your external IP address, but more on the actual address your system has been assigned either DHCP, or statically.

12 Mohan 02.27.08 at 2:33 pm

This should do the work

ifconfig eth0| awk ‘NR==2 {print $2}’| awk -F: ‘{print $2}’

13 Alan Haggai Alavi 11.26.08 at 2:16 pm

curl whatismyip.org does the same too. No need of extra parametres. :-)

14 Paul Colby 02.10.09 at 7:01 am

My current favourite:
ifconfig | sed -n -e 's/:127\.0\.0\.1 //g' -e 's/ *inet addr:\([0-9.]\+\).*/\1/gp'

;)

15 Aaron Kushner 05.13.09 at 9:37 pm

@mohan, you can have multiple separators and not have to rerun awk.
@paul, do you like yours because it has a sed abomination in it? Make it simple!

ifconfig eth0 | awk -F”[: ]+” ‘NR==2 {print $4}’

16 Mika Marttila 10.08.09 at 2:14 pm

Thanks! My virtual Ubuntu server is having problems keeping the same IP from DHCP. I need it to start bind my Django app for whole network so this tip came in handy.

17 Marko Rauhamaa 11.24.09 at 10:03 am

On linux:

hostname -i

18 foo bar 01.18.10 at 12:50 pm

OMFG, guys, please, you *do* realize anybody can hack into whatismyip.org and INSTANTLY gain access to your boxes ?!!!! And you’re not even using HTTPS – so any monkey in the middle can 0wn your boxes too !!!!
Are you dumb enough to run those lynx/curl scripts as root as well ???

NEVER EVER put this much trust into 3rd party systems, or you WILL get screwed !

it would be funny watching you after whatismyip.org returns `rm -rf /`

use ifconfig / ip show… and don’t be so freaking lazy

Leave a Comment

You can 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>

Previous post:

Next post: