How to Find out the IP address assigned to eth0 and display IP only
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
E-mail this to a friend
Printable version
Related Other Helpful FAQs:
- Linux: Find my IP address using Perl at a shell prompt
- Redhat network interface configuration
- PHP Howto Read IP address of remote computer/browser
- Linux creating or adding new network alias to a network card (NIC)
- Linux vmware VPS server routing problem
Discussion on This FAQ
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: broadcast, collisions, encap, find ip address, grep, ifconfig eth0, inet addr, ip address, linux ifconfig, linux interface, loopback, multicast, rx packets, shell script, tx packets


October 17th, 2006 at 11:42 am
if u have idea to configar different server on linux and network plsase send me all informatios ….
with thanks
October 24th, 2007 at 8:17 am
another option is to use “ip” command:
ip addr list eth0 |grep inet |cut -d' ' -f6|cut -d/ -f1which 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/ -f1IPv6:
ip addr list eth0 |grep "inet6 " |cut -d' ' -f6|cut -d/ -f1March 11th, 2008 at 9:43 am
For those on OS X’s Terminal.app:
ifconfig en1 | grep 'inet ' | cut -d ' ' -f 2August 12th, 2008 at 4:15 pm
ifconfig eth0 | sed -n ’s/.*dr:\(.*\) Bc.*/\1/p’