How to capture the first IP address from a ifconfig command?
Solution 1
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 ' !/127.0.0.1/ && /inet/ { gsub(/\/.*/, "", $2); print "IP="$2 } '
Or better yet:
$ ip -o -4 address show | awk ' NR==2 { gsub(/\/.*/, "", $4); print $4 } '
192.168.1.249
Or, as you ask "IP="
#!/bin/bash
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 } '
IP=192.168.1.249
Normal output:
$ ip -o -4 address show
1: lo inet 127.0.0.1/8 scope host lo\ valid_lft forever preferred_lft forever
2: eth0 inet 192.168.1.249/24 brd 192.168.1.255 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, whileifconfig
relies on an ioctl system call.
Solution 2
Awk
solution:
ifconfig -a | awk 'NR==2{ sub(/^[^0-9]*/, "", $2); printf "IP=%s\n", $2; exit }'
Sample output:
IP=10.0.2.15
yael
Updated on September 18, 2022Comments
-
yael over 1 year
How to capture the first IP address that comes from
ifconfig
command?ifconfig -a enw178032: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 100.14.22.12 netmask 255.255.0.0 broadcast 100.14.255.255 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:
IP=100.14.22.12
-
Pedro about 6 yearsThe 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.
-
dev_ter about 6 yearsThis 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.
-
-
yael about 6 yearsI prefer not to set eth0 as default because this name changing on machines , can we provide flexible syntax
-
Rui F Ribeiro about 6 years@yael Changed, took out eth0. However Roman did better than me printing the "IP=" inside
awk
. Too much brainpower for me, still waking up. Avoidifconfig
, 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. -
A.B about 6 yearsJust 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/… ) -
Rui F Ribeiro about 6 years@A.B also -o as I use helps, thanks, will check it ou
-
A.B about 6 yearsAh, I didn't know about
-o
which indeed seems useful for scripts -
Rui F Ribeiro about 6 years@A.B My bad, added an explanation of
-o
to make it more evident. -
Rui F Ribeiro about 6 yearssee this:
ip addr | awk ' !/127.0.0.1/ && /inet/ { gsub(/\/.*/, "", $2); print "IP="$2 } '
-
Jeff Schaller about 6 yearsWhy are you grepping for the localhost IP 127.0.0.1?