How to Find out the IP address assigned to eth0 and display IP only

by on August 30, 2006 · 37 comments· LAST UPDATED December 5, 2007

in

Q. I need to get the IP address assigned to eth0 Linux interface. How do I find out IP address only? I don't want other information displayed by Linux ifconfig command.

A. For shell script or may be for other cause you may need the IP address only. You can use ifconfig command with grep and other filters.

Default output of /sbin/ifconfig command is all interfaces:
$ /sbin/ifconfigOutput:

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:69527 errors:0 dropped:0 overruns:0 frame:0
          TX packets:69527 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:41559546 (39.6 MiB)  TX bytes:41559546 (39.6 MiB)
eth0      Link encap:Ethernet  HWaddr 00:17:9A:0A:F6:44
          inet addr:192.168.2.1  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::217:9aff:fe0a:f644/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:227614 errors:0 dropped:0 overruns:0 frame:0
          TX packets:60421 errors:0 dropped:0 overruns:0 carrier:0
          collisions:272 txqueuelen:1000
          RX bytes:69661583 (66.4 MiB)  TX bytes:10361043 (9.8 MiB)
          Interrupt:17
ra0       Link encap:Ethernet  HWaddr 00:50:56:C0:00:01
          inet addr:192.168.1.2  Bcast:192.168.2.255  Mask:255.255.255.0
          inet6 addr: fe80::250:56ff:fec0:1/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1024 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1320 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

Now you just select eth0 as follows:
$ /sbin/ifconfig eth0

Now you just wanted the IP address, use grep to get the IP:
$ /sbin/ifconfig eth0| grep 'inet addr:'Output:

inet addr:192.168.2.1  Bcast:192.168.2.255  Mask:255.255.255.0

To get IP address from use cut command:
$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2Output:

192.168.2.1  Bcast

Finally remove Bcast with awk
$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
Output:

192.168.2.1

See how to read UNIX/Linux system IP address in a shell script

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

{ 37 comments… read them below or add one }

1 Brijesh October 17, 2006 at 11:42 am

if u have idea to configar different server on linux and network plsase send me all informatios ….

with thanks

Reply

2 Skuutter October 24, 2007 at 8:17 am

another option is to use “ip” command:
ip addr list eth0 |grep inet |cut -d' ' -f6|cut -d/ -f1
which prints both ipv4 and ipv6 addresses, ipv4 addr can be printed using tighter grep expression:
IPv4:
ip addr list eth0 |grep "inet " |cut -d' ' -f6|cut -d/ -f1
IPv6:
ip addr list eth0 |grep "inet6 " |cut -d' ' -f6|cut -d/ -f1

Reply

3 gaw.in March 11, 2008 at 9:43 am

For those on OS X’s Terminal.app:

ifconfig en1 | grep 'inet ' | cut -d ' ' -f 2

Reply

4 doug January 10, 2011 at 4:10 pm

thank you, exactly what I’m looking for (now in my bash profile):
alias ip=”ifconfig en0 | grep ‘inet ‘ | cut -d ‘ ‘ -f 2″

Reply

5 feyd February 3, 2012 at 8:06 pm

for OS X this is much better.

ipconfig getifaddr en0

ipconfig getifaddr en1

Reply

6 Vicente August 12, 2008 at 4:15 pm

ifconfig eth0 | sed -n ‘s/.*dr:\(.*\) Bc.*/\1/p’

Reply

7 Bob October 13, 2008 at 10:57 am

$ hostname -i

Reply

8 MauRice December 6, 2009 at 12:01 pm

Bash-script: http://users.telenet.be/x86_64/Scripts/IP.check
Put it in your “.bashrc” file.
Output when you start a konsole/terminal:
WAN IP: xxx.yyy.zzz.aaa
LAN IP: bbb.ccc.ddd.eee

Reply

9 John April 26, 2010 at 6:10 am

Bob wins. The rest of you should go self-flagellate for being moronic punks.

Rules of thumb for efficient text processing:

a. Avoid the need to do so. If there’s a command that outputs exactly what you need, use it. (For example, in our case here, using ‘hostname -i’ instead of stupidly dicking around with ifconfig, cut, grep, sed, and awk). Use some common sense, for Christ’s sake. Think maybe nobody else ever needed to include their IP address in a command? Before you go monkey-spanking around with awk or perl, see if there’s an appropriate command.

b. if there’s a shell built-in that can do it, use that (your shell is already loaded into memory, and there is overhead in making a call to an external). For example, if you can parse a line effectively using the shell’s string substitution features, then do so. Don’t be a dumb-ass and waste your RAM and CPU. Read your shell’s man page.

b. If the shell can’t do it, use the simplest alternative that can. If cut or tr will work, use that. If not, use sed. If not use awk.

c. The biggest sin of all, an abomination of a command that pipes together more than one of the above, awking the output of grep, for example.

Reply

10 William July 15, 2010 at 1:09 pm

While I agree with your principles, Bob is actually wrong – hostname does a lookup into the /etc/hosts file and may or may not give you the IP address of eth0. It depends on what you have in your hosts file. For example, I have a machine where “hostname -i” returns 127.0.0.1, simply because there was no name associated with the IP address of eth0 (192.168.0.16).
Grep/awk/sed pipes are costly in regards to system resources as you outlined, but sometimes you have to use them. What would be better is if there were a command that simply went “get-ip-address eth0″ and returned the IP, but I have not found one as of yet.

Reply

11 bigdavejonnyt March 16, 2011 at 6:26 pm

John,

Wow, you’re rather fun, aren’t you? Let’s dismiss beginners and overlook the intrinsic value of getting a look at how cut, grep and the others work for those folk [incl. me] and let’s just show how you’ve happily traded ‘my linux is better than yours’ for people skills.

Reply

12 Fannar May 19, 2010 at 7:12 pm

Recommend “ip addr” for those with many VLAN IPs

“ip” is in the “iproute” package, “apt-get install iproute”

Reply

13 Laurent Hangard August 14, 2010 at 7:26 am

A small comment: in my ubuntu version 8.10, ifconfig gives inet adr,( and not inet addr).
Thanks for the tip.

Reply

14 thiagoc August 23, 2010 at 4:23 pm

eth0=($(ifconfig eth0 | grep “inet addr” | tr “:” ” “))
echo ${eth0[2]}

Reply

15 Alex December 30, 2010 at 4:29 pm

I am not agree with John,
with comments like this “Don’t be a dumb-ass and waste your RAM and CPU”.
It looks like you living in 19 century, you better group old man, this days RAM is cheap and CPU is big, and don`t jump on people who like to experiment with thinks.

Who like to experiment try this:
hostname -I | cut -d: -f2 | awk ‘{ print $1}’
or this
hostname -I | cut -d: -f2 | awk ‘{ print $2}’

Reply

16 Alex December 30, 2010 at 6:32 pm

even this will work
hostname -I | awk ‘{ print $1}’
or
hostname -I | awk ‘{ print $2}’

Reply

17 Christoph May 12, 2011 at 11:24 am

hostname -I works to

Reply

18 balucio July 7, 2011 at 3:04 pm

The ifconfig output in some system is localized and the awk version not work

Reply

19 Kaacz April 17, 2014 at 4:15 pm

Use LANG=C on top of shell script… :)

Reply

20 WHD-Maru August 17, 2011 at 6:25 pm

very useful tips,
In my case, I want to list all my server IPs assigned to eth0, the ifconfig results something like

###
eth0 Link encap:Ethernet HWaddr 00:16:3E:D8:2B:BF
inet addr:127.0.0.1 Bcast:192.168.0.127 Mask:255.255.255.128
inet6 addr: fe80::216:3eff:fed8:2bbf/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4311554 errors:0 dropped:0 overruns:0 frame:0
TX packets:3027306 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:944036536 (900.3 MiB) TX bytes:6041030741 (5.6 GiB)
Interrupt:24

eth0:0 Link encap:Ethernet HWaddr 00:16:3E:D8:2B:BF
inet addr:192.168.0.2 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:24

eth0:1 Link encap:Ethernet HWaddr 00:16:3E:D8:2B:BF
inet addr:192.168.0.3 Bcast:192.168.0.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:24
###

currently, for single IP, I can use this command
SERVERIP=`grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F= ‘{print $2}’`
echo $SERVERIP

How to list all of my IPs?
Please reply to my email :)
Many thanks

Reply

21 Jason October 11, 2011 at 4:54 am

I prefer doing it this way…

ifconfig eth0 | grep ‘inet addr:’ | cut -d”:” -f2 | cut -d” ” -f1

If hostname -i or hostname -I don’t give me what I want.

Reply

22 Kevin April 4, 2012 at 5:58 pm

I thought I’d post since I didn’t see this answer:

cat /sys/class/net/eth0/address

For my setup, hostname -i only gives me the loopback address (127.0.0.1).

Reply

23 Kevin April 4, 2012 at 6:04 pm

whoops that’s the MAC address.

Reply

24 niko April 27, 2012 at 5:59 pm
$ dnsdomainname -i

does the same thing

Reply

25 bulb June 13, 2012 at 3:29 pm

I try to avoid external programs for simple string task:

ipaddr=$(/sbin/ifconfig eth0) ; ipaddr=${ipaddr/*inet addr:/} ipaddr=${ipaddr/ */}
echo "$ipaddr"

Reply

26 xIN3N July 18, 2012 at 10:34 pm

Of course “dnsdomainname -i” really does the same thing but the discussion is about the details and understanding of how to get the proper value by chopping again and again…!

Reply

27 Dallas August 31, 2012 at 2:56 am

I hardcode my IP address in /etc/sysconfig/network-scripts/ifcfg-eth3; yet when I list it (ifconfig -a) I do not see my IP address that I hardcoded? please help.

Reply

28 balaji September 19, 2012 at 11:03 am

Mac: ifconfig en0 | grep inet[^6]| cut -d’ ‘ -f2

Reply

29 tomt March 10, 2013 at 10:51 pm

Actually, hostname does not work. I am running a LAMP server so hostname gives 127.0.0.0 . I have eth0 connected to a router and ONLY ifconfig gives me the correct IP. Bob is an idiot.

Reply

30 tomt March 10, 2013 at 10:52 pm

Correction:
John is an idiot for saying:

“Bob wins. The rest of you should go self-flagellate for being moronic punks.”

Sorry, Bob.

Reply

31 Stephan Eriksen March 19, 2013 at 9:23 am

You can do this easier and better:

ip=$(echo “$(hostname -I) | grep -o ‘^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}’)

This don’t care what interface you are using.

Reply

32 Mac April 3, 2013 at 2:40 am

I am still trying to learn how to use grep and do not really have firm grasp of ‘regex’ can someone recommend a book to start out with?

Reply

33 Ayush Joshi October 7, 2013 at 12:34 pm
#!/bin/bash
wifiip=$(ip addr | grep inet | grep wlan0 | awk -F" " '{print $2}'| sed -e 's/\/.*$//')
eth0ip=$(ip addr | grep inet | grep eth0 | awk -F" " '{print $2}' | sed -e 's/\/.*$//')
# report findings, only returning devices with IPs
if [ "$eth0ip" -eq " " ]
then
echo "$wifiip" | grep [0-9]$ > /home/pi/attendance/ip.txt
else
echo "$eth0ip" | grep [0-9]$ > /home/pi/attendance/ip.txt
fi

i am trying to place working interface ip-address into a text file but its throwing error on executing this script

ip.sh: 14: [: unexpected operator

Reply

34 MauRice October 8, 2013 at 6:00 pm

Joshi.

Change if [ “$eth0ip” -eq ” ” ] in to if [ “$eth0ip” = ” ” ]

Reply

35 John H February 26, 2014 at 9:34 am

Cuts can be funky, here is one with only grep and awk:

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

Reply

36 Kaacz April 17, 2014 at 4:16 pm

$ /sbin/ifconfig eth0 | grep ‘inet addr:’ | awk ‘{ print substr($1,5) }’

Reply

37 Berokor October 14, 2014 at 1:04 pm

ifconfig eth0 | grep ‘inet addr’ | awk ‘{print $2}’ | cut -d: -f2

or for german users (german system language):
ifconfig eth0 | grep ‘inet Adr’ | awk ‘{print $2}’ | cut -d: -f2

Reply

Leave a Comment

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

Previous Faq:

Next Faq: