Bash Shell Command to Find or Get IP address

by on January 29, 2006 · 45 comments· LAST UPDATED May 28, 2011

in , ,

How do I find out my Linux / UNIX system ip address, subnet and related networking information from a command prompt?

To find out IP address of Linux/UNIX/BSD/Unixish system you need to use command called ifconfig. It is used to configure the kernel-resident network interfaces. It is used at boot time to set up interfaces as necessary. After that, it is usually only needed when debugging or when system tuning is needed. If no arguments are given to ifconfig command it displays the status of the current active interfaces. It displays Ethernet IP address, Mac address, subnet mask and other information. Type /sbin/ipconfig command to display IP address:
$ /sbin/ifconfig
OR type the following command:
$ /sbin/ifconfig | less
Under Solaris and other Unixish oses you may need to type ifconfig command with -a option as follows:
$ /sbin/ifconfig -a
Sample outputs:

eth0     Link encap:Ethernet  HWaddr 00:0F:EA:91:04:07
  inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
  inet6 addr: fe80::20f:eaff:fe91:407/64 Scope:Link
  UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
  RX packets:31167 errors:0 dropped:0 overruns:0 frame:0
  TX packets:26404 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:1000
  RX bytes:38338591 (36.5 MiB)  TX bytes:3538152 (3.3 MiB)
  Interrupt:18 Base address:0xc000
lo        Link encap:Local Loopback
  inet addr:127.0.0.1  Mask:255.0.0.0
  inet6 addr: ::1/128 Scope:Host
  UP LOOPBACK RUNNING  MTU:16436  Metric:1
  RX packets:1994 errors:0 dropped:0 overruns:0 frame:0
  TX packets:1994 errors:0 dropped:0 overruns:0 carrier:0
  collisions:0 txqueuelen:0
  RX bytes:188041 (183.6 KiB)  TX bytes:188041 (183.6 KiB)

In above example 192.168.1.2 is IP address of eth0 Ethernet interface.

Linux ip Command

It is recommend that you use the ip command under Linux based systems. The following command will show all ip address:
# ip addr show
To see information about eth0 ip address, enter:
# ip addr show eth0
Sample outputs:

2: eth0:  mtu 1500 qdisc mq state UP qlen 1000
    link/ether b8:ac:6f:65:31:e5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.100/24 brd 192.168.2.255 scope global eth0
    inet6 fe80::baac:6fff:fe65:31e5/64 scope link
       valid_lft forever preferred_lft forever

For more information please see howto read UNIX/Linux system IP address in a shell script.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 45 comments… read them below or add one }

1 Anonymous March 1, 2006 at 7:24 am

ipconfig usually doesn’t work outside of windows

Reply

2 nixcraft March 1, 2006 at 1:07 pm

Opps! Just corrected typo since I work on both UNIX and Windows Server and sometime I get confused :(.

Thanks, I appreciate your post :)

Reply

3 saeed July 23, 2007 at 2:17 pm

i worked in a company and the I.T man close the face book site what can i do

Reply

4 Criss November 21, 2011 at 11:03 pm

Work saeed and don`t be a facebook adict

Reply

5 Jiten Mistry April 25, 2008 at 12:28 pm

Use hostname -i
also find other parameters using man hostname.

Reply

6 Oscar June 29, 2008 at 6:32 am

hostname -i doen’t get the internet ip address:

?????:~ # hostname -i
127.0.0.2

it gives me the lo ip address.

I want to get the internet ip address from a command.

Regards
Oscar

Reply

7 Oscar June 29, 2008 at 9:27 am

I have found this way to get my ip public address (I have two IPs in my eth0 card for NAT):
xxxx:~ # ifconfig|sed -n “/inet addr:.*255.255.255.255/{s/.*inet addr://; s/ .*//; p}”

Regards,
Oscar

Reply

8 Oscar July 2, 2008 at 4:32 pm

I’m trying OpenSuSE LiveCD 64Bits (Linux linux 2.6.25.5-1.1-default #1 SMP 2008-06-07 01:55:22 +0200 x86_64 x86_64 x86_64 GNU/Linux)

and the previous command doesn’t work. I have different broadcast address, so I get it with the mask (that is the same):

linux:/home/linux # ifconfig eth0 | sed -n “/inet addr:.*255.255.248.0/{s/.*inet addr://; s/.*//; p}”
xxx.xxx.xxx.xxx
linux:/home/linux #

Reply

9 Alejandro Smith July 8, 2008 at 8:27 pm

Oscar, try this.

root@azrael:~# ifconfig eth0 | grep inet | awk ‘{print $2}’ | sed ‘s/addr://’

Where eth0 is the interface you want to know the ip address. In my case was eth0. This will retrieve the Ip Address from the specified interface. If you have ipv6 active and you only use ipv4 Address, then add a grep . to the end of the sentence.
it would be like this:
root@azrael:~# ifconfig eth0 | grep inet | awk ‘{print $2}’ | sed ‘s/addr://’ | grep .

Reply

10 Alejandro Smith July 8, 2008 at 8:30 pm

I use the above to retrieve the Ip address (dynamic one) from my ISP in order to forward the HTTP petitions to the web server (behind a firewall). Hope it works for you.

Reply

11 Oscar July 25, 2008 at 10:25 am

Is great!

Thanks a lot, Alejandro

Reply

12 Bastiaan November 18, 2008 at 12:33 pm

almost good:
ifconfig eth0 | grep ‘inet ‘ | awk ‘{print $2}’ | sed ‘s/addr://’

will work ->inet otherwise you will get an empty line from the inet6 line you grepped

Reply

13 Bastiaan November 18, 2008 at 12:34 pm

inet[space]

Reply

14 shweta December 29, 2011 at 11:58 am

is there any other option available to get ip address on linux

Reply

15 helloworldbr August 3, 2012 at 4:12 am

I simply issue a bash command:root@debian:~ echo $IP

Reply

16 Alejandro Smith November 18, 2008 at 1:53 pm

that’s why i typed two lines, the first one if you don’t have inet6 active on your server, and the second one if you do have it on.

root@azrael:~# ifconfig eth0 | grep inet | awk ‘{print $2}’ | sed ’s/addr://’ | grep . <– is not the best way to do it, but the “grep .” stuff works for that matter (at least it does for me) as i said it depends on the flavor of Linux you have and some other stuff.

Thanks for the tip on the “space after inet” i’ll try it out..

Reply

17 award September 24, 2010 at 4:07 pm

Hi,
in case someone still interested, i think easiest way to get local ip address is:
hostname -I (capital i)
this will return just the ip address with nothing else.

To get my external ip address (without use dyndns, noip & c) i use in a script:

wget www.whatismyip.com/automation/n09230945.asp -O /tmp/ip.txt -o /dev/null

This will write to /tmp/ip.txt my external ip and then i upload the text file with curl
to a directory on my web page

#!/bin/bash
wget www.whatismyip.com/automation/n09230945.asp -O /tmp/ip.txt -o /dev/null
extip=`cat /tmp/ip.txt`
echo "$extip" > /tmp/ip.txt
hostname -I > /tmp/intip.txt
exit 0

Bye
Award
——————
C u in /dev/null

Reply

18 rahul December 11, 2008 at 7:25 am

HOW TO CHANGE IP,SUBNETMASK,GATEWAY,PLS SEND INFORMATION IM TRYING /etc/inet/hosts
/etc/inet/ipnodes THIS COMMAND BUT IS NOT WORK PLS HELP ME

Reply

19 Aaron January 9, 2009 at 2:07 pm

Hi, I’m using RedHat Workstation 4 and would like to write a script to set the IP address, subnet mask and hostname of my machine. The same as Rahul.

How can i do this?
Thanks in advance.

Reply

20 Bill Gates January 19, 2009 at 7:10 am

A lot of the above example scripts/commands dont work corectly due to missformatting of the various Quote characters. Single quotes, double quotes and backticks are messed up.

` is a backtick – top left of the keyboard. Its used to go around a command thats ran.
export FOO=`ls`
puts the output of the ls command in the variable FOO

single Quote ‘ and double quotes ” are used in different places to go around arguments to commands. and to keep special chaacters from gettting parsed by the shell.

Reply

21 Bill Gates January 19, 2009 at 7:12 am

oops.. Watch out for the way this thread/forum alters the matching Quote to the single and double qupte if you cut and paste the examples. ie:

single Quotes ‘hello’

double quotes “hello”

back ticks `hello`

I had to manually alter the lines after i pasted them to use proper quoteing.

Reply

22 kishore February 12, 2009 at 4:18 am

how to find the ip address of an website in linux

Reply

23 vyagh March 2, 2009 at 10:47 am

Hi
we have server configured on Linux.
Is there any way to get the IP Address of the client which is conncetd to that server.

Reply

24 mrG June 29, 2010 at 10:15 pm

IP=`who -m –ips | awk ‘{print $6}’`

Reply

25 Bijoy Meethal May 28, 2009 at 12:13 pm

Kishore , go to Terminal, and say ‘ping yahoo.com’

Reply

26 Dave November 28, 2009 at 9:55 am

ifconfig eth0 | grep inet | sed -n “s/^\s*inet addr://p” | awk ‘{ print $1 }’

works on my system

Reply

27 wuhaa December 31, 2009 at 4:44 pm

This one I have used on my Ubunut 9.04 system

# ifconfig eth0|grep "inet addr:"|awk '{print $2}'|awk -F : '{print $2}'

Reply

28 Mike Godin June 8, 2010 at 4:09 pm

I like to use the simple and concise:
host $(hostname) | sed ‘s/.*has address //g’

It simply returns the IP address that the rest of the world sees for your machine.

Reply

29 /dev/movebo September 12, 2010 at 10:09 pm

@wuhaa @Alejandro\ Smith

Personally I prefer this version:
$ /sbin/ifconfig eth0 | sed '/inet\ /!d;s/.*r://g;s/\ .*//g'

Why, you may ask. Because of this:

$ time { /sbin/ifconfig eth0|grep "inet addr:"|awk '{print $2}'|awk -F : '{print $2}' ; }
192.168.1.8

real 0m0.027s
user 0m0.013s
sys 0m0.003s

$ time { /sbin/ifconfig eth0 | grep inet | awk '{print $2}' | sed 's/addr://' | grep . ; }
192.168.1.8

real 0m0.009s
user 0m0.011s
sys 0m0.010s

$ time { /sbin/ifconfig eth0 | sed '/inet\ /!d;s/.*r://g;s/\ .*//g' ; }
192.168.1.8

real 0m0.005s
user 0m0.005s
sys 0m0.004s

The fewer processes, the faster the execution.

Reply

30 mrG September 13, 2010 at 2:03 pm

brill score! well done.

Reply

31 Alejandro Smith September 13, 2010 at 2:51 pm

Excellent info on that.. thanks for updating this. That’s true, fewer processes translates into a faster execution.

Two years ago when i first posted on this faq with a “solution”, i was a new linux user, and i did not had any experience on this. I read some good information on bash scripting and came out with this solution for a problem i was having at that time.

I didn’t care back then if it was faster than other solutions, i just needed to solve the issue.. ;-) but of course, YOU are completely right with your point of view.

Thank you all for sharing your knowledge on this.

God bless you all.

Reply

32 Keith Kyzivat March 27, 2011 at 5:51 pm

Similar approach, but with Awk instead of sed:

time { ifconfig vmnet8 | awk ‘/inet addr:/ {print substr($2,6)}’ ; }

Do some *nix implementations put a space after ‘addr:’ ? If so, this will break in those cases.

Reply

33 Sandeep September 30, 2010 at 10:59 am

Hi Award

Excellent command to find the originating IP addresss
hostname -i

Thanks!!

Reply

34 Henrique November 24, 2010 at 7:44 pm

Simples…
Work For me (Debian Lenny 5.0)
# ifconfig eth0 | grep inet | grep -v “inet6″ | awk {‘print $3′}

Reply

35 Sérgio Luiz Araújo Silva December 6, 2010 at 8:03 pm

# paste this in your ~/.bashrc
myip (){
clear
echo
echo ” ip local: `hostname -I`”
echo ” router: `route -n | awk ‘/UG/ {print $2}’`”
echo ” external ip: `curl –connect-timeout 4 -s sputnick-area.net/ip`”
echo
read -sn 1 -p ” Press any key to continue…”
clear
}

Reply

36 radha December 22, 2010 at 7:19 am

ifconfig -a (now this works for Solaris 10)

Reply

37 Adi January 4, 2011 at 12:17 pm

To find the ip address of Unix box from Unix prompt:

nslookup

Reply

38 Dan August 3, 2011 at 3:25 pm

Here’s one that works (quickly) for me using cut:

time { ifconfig eth0|grep 192|cut -d: -f2|cut -d’ ‘ -f1 ; }
192.168.111.75

real 0m0.007s
user 0m0.000s
sys 0m0.002s

Reply

39 Thomas Arend January 28, 2012 at 9:43 pm

A computer may have multiple network interfaces and therefore more than one ip-address you should try:

/sbin/ifconfig [dev] | \
sed -n ‘/inet [Aa]d\{1,2\}resse\{0,1\}:/ { s# *inet [Aa]d\{1,2\}resse\{0,1\}:##; s# .*##p }’

If you omit [dev] this command will give you a list of all IP-address and should work with English and German localization.

Have a look here (German page).

hostname -i is unpredictable in its result and nslookup $(hostname) will give you only what DNS knows about your machine.

Reply

40 Paritoshik October 26, 2012 at 6:15 am

its realy grtt i found my way thankss..

Reply

41 zak September 28, 2013 at 2:23 pm

i have always the same result : inet iddr : 127.0.0.1

what could be the problem ) thank you

Reply

42 Abdoulaye Siby November 23, 2013 at 7:16 pm

Just running the following

# ip -f inet -o addr show eth0

The result will be (on one line)

2: eth0   inet 192.168.2.100/24 brd 192.168.2.255 scope global eth0

Then you can apply a simple filter on it to extract the IP

Reply

43 Abdoulaye Siby November 23, 2013 at 7:17 pm

Sorry for the typos. Unable to re-edit my post.

Reply

44 stoffel010170 March 18, 2014 at 10:45 am
ip -4 -o addr show dev eth0 |awk '{split($4,a,"/") ;print a[1]}'

Reply

45 s March 27, 2014 at 6:16 pm

I’m using cygwin on windows and a VPN. None of the above worked for me. I had to use the following command and some grep and sed commands
route print | egrep ” 255.255.255.255.*123.123.123.123″ | sed ‘s/\s*\(.*\)255.255.255.255.*/\1/g’

#I’ve used 123.123.123.123 as a fictional router address.

Explanation:
The vertical line, the pipe symbol, | , takes the output of everything on the left and feeds it to the command on the right. So the solution that works for me (the rest do not) involves essentially three commands – each of which that does something and parse things to the next step.

route print
the main command of many things – over many lines

egrep ” 255.255.255.255.*123.123.123.123
this selects a line that has the netmask at 255.255.255.255 then any characters as shown by the .* after the 255′s and then looks for my router’s address of 123.123.123.123.

sed ‘s/\s*\(.*\)255.255.255.255.*/\1/g’
sed command replaces/extracts text. basic use: ‘s/replace/withThis/g’ does a global search and replaces all references of replace with the string withThis.
This sed command extracts the part of the line before the 255 part. It ignores leading spaces indicated by the \s and also anything after the 255 part shown by .* which is a shorthand for any character or number (the . part) that is found zero of more times (the * part).
By placing part of the replace string in parentheses \(.*\) I am saying keep this part. So it’s the part after leading spaces and before the 255s. The \1 part is the replacement part of the sed command throws away all the other stuff and just keeps that part in parentheses and is displayed. This is the IP address on my machine.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: