How to capture the first IP address that comes from ifconfig command?

ifconfig -a
enw178032: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet  netmask  broadcast
        inet6 fe80::250:56ff:fe9c:158a  prefixlen 64  scopeid 0x20<link>
        ether 00:10:56:9c:65:8a  txqueuelen 1000  (Ethernet)
        RX packets 26846250  bytes 12068811576 (11.2 GiB)
        RX errors 0  dropped 58671  overruns 0  frame 0
        TX packets 3368855  bytes 1139160934 (1.0 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Expected result:

  • 3
    The question has been answered, either using awk or grep / cut, etc. But it's still a bad idea in general to use and grep from ifconfig. ip is better suited and better supported in modern Linux builds. – Pedro Feb 27 '18 at 9:35
  • 1
    This sounds like an X-Y problem. What information do you really want? The main network-facing IP address of the machine? Any network-facing IP address of the machine? The IP address of the first network adapter? Any IP address, doesn't matter what it is? There is probably a more direct, accurate, and portable way to get the information you need. – alex.forencich Feb 27 '18 at 23:08

It is better avoid using ifconfig for getting an IP address in a scriptas it is deprecated in some distributions (e.g. CentOS and others, do not install it by default anymore).

In others systems, the output of ifconfig varies according to the release of the distribution (e.g. the output/spacing/fields of ifconfig differs from Debian 8 to Debian 9, for instance).

For getting the IP address with ip, in a similar way you are asking:

ip addr | awk ' !/ && /inet/ { gsub(/\/.*/, "", $2); print "IP="$2 } '

Or better yet:

$ ip -o -4  address show  | awk ' NR==2 { gsub(/\/.*/, "", $4); print $4 } '

Or, as you ask "IP="

echo -n "IP="
ip -o -4  address show  | awk ' NR==2 { gsub(/\/.*/, "", $4); print $4 } '

Adapting shamelessly the idea from @Roman

$ ip -o -4  address show  | awk ' NR==2 { gsub(/\/.*/, "", $4); print "IP="$4 } ' 

Normal output:

 $ ip -o -4  address show 
1: lo    inet scope host lo\       valid_lft forever preferred_lft forever
2: eth0    inet brd scope global eth0\       valid_lft forever preferred_lft forever

From man ip:

-o, -oneline
output each record on a single line, replacing line feeds with the '\' character. This is convenient when you want to count records with wc(1) or to grep(1) the output.

See one example of why ifconfig is not advised: BBB: `bbb-conf --check` showing IP addresses as `inet` - ifconfig woes

For understanding why ifconfig is on the way out, see Difference between 'ifconfig' and 'ip' commands

ifconfig is from net-tools, which hasn't been able to fully keep up with the Linux network stack for a long time. It also still uses ioctl for network configuration, which is an ugly and less powerful way of interacting with the kernel.

Around 2005 a new mechanism for controlling the network stack was introduced - netlink sockets.

To configure the network interface iproute2 makes use of that full-duplex netlink socket mechanism, while ifconfig relies on an ioctl system call.

  • I prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax – yael Feb 27 '18 at 9:31
  • @yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside awk. Too much brainpower for me, still waking up. Avoid ifconfig, it has not future, and the position of IP address changes, there are at least two different versions/implementations(?) for Linux out there that I know off. – Rui F Ribeiro Feb 27 '18 at 9:35
  • Just for info, on newer versions of iproute2 (eg: not on CentOS7 nor Debian8) ip can take a -brief parameter and its output becomes easier to parse (2015-08-31: git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/… ) – A.B Feb 27 '18 at 12:18
  • @A.B also -o as I use helps, thanks, will check it ou – Rui F Ribeiro Feb 27 '18 at 12:40
  • 1
    Ah, I didn't know about -o which indeed seems useful for scripts – A.B Feb 27 '18 at 13:37

Awk solution:

ifconfig -a | awk 'NR==2{ sub(/^[^0-9]*/, "", $2); printf "IP=%s\n", $2; exit }'

Sample output:

ip addr | grep -v | grep 'inet ' | \
awk {'print $2'} | awk -F "/" {'print "IP="$1'}
  • 1
    see this: ip addr | awk ' !/ && /inet/ { gsub(/\/.*/, "", $2); print "IP="$2 } ' – Rui F Ribeiro Feb 27 '18 at 19:25
  • 1
    Why are you grepping for the localhost IP – Jeff Schaller Feb 27 '18 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.