≡ Menu

linux ifconfig

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