Bash Shell Command to Find or Get IP address

by Vivek Gite · 22 comments

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

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

Output of above commands:

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. For more information please see Howto read UNIX/Linux system IP address in a shell script.

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!

{ 22 comments… read them below or add one }

1 Anonymous 03.01.06 at 7:24 am

ipconfig usually doesn’t work outside of windows

2 nixcraft 03.01.06 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 :)

3 saeed 07.23.07 at 2:17 pm

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

4 Jiten Mistry 04.25.08 at 12:28 pm

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

5 Oscar 06.29.08 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

6 Oscar 06.29.08 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

7 Oscar 07.02.08 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 #

8 Alejandro Smith 07.08.08 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 .

9 Alejandro Smith 07.08.08 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.

10 Oscar 07.25.08 at 10:25 am

Is great!

Thanks a lot, Alejandro

11 Bastiaan 11.18.08 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

12 Bastiaan 11.18.08 at 12:34 pm

inet[space]

13 Alejandro Smith 11.18.08 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..

14 rahul 12.11.08 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

15 Aaron 01.09.09 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.

16 Bill Gates 01.19.09 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.

17 Bill Gates 01.19.09 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.

18 kishore 02.12.09 at 4:18 am

how to find the ip address of an website in linux

19 vyagh 03.02.09 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.

20 Bijoy Meethal 05.28.09 at 12:13 pm

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

21 Dave 11.28.09 at 9:55 am

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

works on my system

22 wuhaa 12.31.09 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}'

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 FAQ:

Next FAQ:

nixCraft FAQ PDF Collection Now Available To All