Is there an easy way to programmatically extract IP address, without tedious parsing of ifconfig
? I would not mind simple command output processing using sed
to do it but not processing multiline files from /etc
some place. What I am trying to do is modify my .bashrc
to display the IP address of the host in the greeting message. I am using Ubuntu 12.04
but decided to post here instead of the Ubuntu forum because I consider this to be not distro-specific.

- 829,060

- 12,472
- 18
- 64
- 88
3 Answers
NOTES
NIC device handles
The examples below assume that the network interface is a wireless card named
wlan0
. Adjust this bit in the examples for your particular situation. For example if it's a wired NIC card, then it's likelyeth0
.IPv4 - (Internet Protocol version 4)
Also these examples are returning the IPv4 address. the "dotted quad" that most people identify as their "IP Address".
For example:
inet addr:192.168.1.20 Bcast:192.168.1.255 Mask:255.255.255.0
IPv6 - (Internet Protocol version 6)
If your system is configured to support IPv6 you'll see both the "dotted quad" as well as the IPv6 IP addresses in the
ifconfig
output.For example:
inet6 addr: fe80::226:c7ff:fe85:a720/64 Scope:Link
The commands below explicitly ignore this, but could be adapted quite easily to grab this information instead.
Solutions (using ifconfig
)
There are many ways to do this. You could for example use this awk
script to parse out the IP address of your wireless LAN NIC (wlan0):
$ ifconfig wlan0 | grep "inet " | awk -F'[: ]+' '{ print $4 }'
192.168.1.20
You can do this and make it more compact:
$ ifconfig wlan0 | awk '/t addr:/{gsub(/.*:/,"",$2);print$2}'
192.168.1.20
You could also do it using perl
:
$ ifconfig wlan0 | perl -nle'/t addr:(\S+)/&&print$1'
192.168.1.20
The Perl example is about as compact as it can get.
There are countless other ways to do this, these are just a couple of examples to get you started.
EDIT #1
Some additional notes and comments. @StephaneChazelas demonstrated that there is an even more compact version using grep
:
$ ifconfig wlan0|grep -Po 't addr:\K[\d.]+'
192.168.1.20
This solution makes use of grep
's ability in newer versions to make use of PCRE (Perl regular expressions), along with it's -o
switch to return just what matches the regular expression.
Solutions (using ip
)
As was also mentioned in the comments, ifconfig
can be troublesome to use on systems that have multiple IP addresses assigned to a network device, given it only returns the first one. So it's better to use the command ip
in these situations.
For example:
$ ip addr show wlan0 | grep -Po 'inet \K[\d.]+'
192.168.1.20
You can also tell ip
to only show you IPv4 information for a given network interface. In this case we're looking only at the interface named wlan0
:
$ ip -f inet addr show wlan0 | grep -Po 'inet \K[\d.]+'
192.168.1.20
References

- 369,824
-
PERFECT !! I just replaced your
wlan0
witheth0
and got the IP address – amphibient Aug 20 '13 at 16:24 -
2@amphibient - glad to hear it solved your problem. Thanks for the question. – slm Aug 20 '13 at 16:28
-
3Note that it returns the first IPv4 address of the interface. Interfaces can have more than one IP address. You need
ip addr show
to see all of them asifconfig
only shows one of them. – Stéphane Chazelas Aug 20 '13 at 16:44 -
2Shorter with GNU
grep
:ifconfig eth0|grep -Po 't addr:\K[\d.]+'
which has the advantage of returning a non-zero exit status if the interface has no address. – Stéphane Chazelas Aug 20 '13 at 16:46 -
-
-
It does depend on system configuration, but on most of my machines
hostname -i
works quite well. – Bananguin Aug 20 '13 at 20:42 -
@user1129682 - yeah that only works if the hostname the system is set to can resolve via DNS to an IP address. – slm Aug 20 '13 at 20:44
-
On Raspberry Pi this works:
ifconfig wlan0 | grep "inet " | awk -F'[: ]+' '{ print $3 }'
, it's3
and not 4 for rpi. – Pedro Lobito Mar 05 '18 at 17:45 -
$ hostname -I
For example:
$ hostname -I
192.168.1.18
Info. from manpages:
http://manpages.ubuntu.com/manpages/raring/en/man1/hostname.1.html
http://unixhelp.ed.ac.uk/CGI/man-cgi?hostname

- 242,166

- 319
-
"Invalid option" - the only flag available in
man hostname
for an IP address isi
and that only returns the local loop... – jasonwryan Aug 20 '13 at 21:44 -
You must be using a very very old version of hostname or have some kind of problem. In all new ubuntus that I have tried, and maybe olders too, it works perfectly well. – Robert Vila Aug 20 '13 at 21:59
-
1@mattdm: It is so clear that it needs no explanation It is clearer than all above explanations – Robert Vila Aug 20 '13 at 22:01
-
`SYNOPSIS
– Robert Vila Aug 20 '13 at 22:08hostname [-v] [-a|--alias] [-d|--domain] [-f|--fqdn|--long] [-A|--all- fqdns] [-i|--ip-address] [-I|--all-ip-addresses] [-s|--short] [-y|--yp|--nis] hostname [-v] [-b|--boot] [-F|--file filename] [hostname] hostname [-v] [-h|--help] [-V|--version]`
-
1
-I, --all-ip-addresses Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.
That is why it works. – Robert Vila Aug 20 '13 at 22:09 -
http://manpages.ubuntu.com/manpages/raring/en/man1/hostname.1.html http://manpages.ubuntu.com/manpages/lucid/en/man1/hostname.1.html http://unixhelp.ed.ac.uk/CGI/man-cgi?hostname old: http://manpages.ubuntu.com/manpages/hardy/en/man1/hostname.1.html – Robert Vila Aug 20 '13 at 22:15
-
3Ubuntu is one flavour of Linux: you should never assume it is the default... – jasonwryan Aug 22 '13 at 22:15
-
5Do not run this command on a Solaris host unless you want the hostname to be changed to
-I
. – geedoubleya Sep 15 '14 at 12:51 -
Here is another one using "ip" but this works better when your device might be connected with different interfaces, such as wired Ethernet sometimes and WiFi other times. I also used "sed" instead of "grep" or "perl", for variety.
This finds whatever source IP has a route to the Internet. Or to Google's DNS at any rate.
ip -o route get 8.8.8.8 | sed -e 's/^.* src \([^ ]*\) .*$/\1/'

- 597
- 1
- 6
- 14
-
1A bit silly yet effective way:
ip -o route get 8.8.8.8 | awk -F'src | uid' '{print $2}'
– Yaron Jul 22 '19 at 10:53 -
-
Even briefer, but probably not as robust:
cut -d' ' -f7 < <(ip -o route get 8.8.8.8)
– ikaerom Nov 17 '23 at 22:44 -
If you have a recent
iproute2
(with json output) and thejq
tool installed, this is a nice variant requiring no pattern-matching (to be a little more future-proof):ip -j route get 8.8.8.8 | jq -r '.[0].prefsrc'
– Rowan Thorpe Mar 27 '24 at 18:01 -
...and here is a version if you are in Bash shell and don't want to rely on any other executable than
ip
:ip="$(ip -j route get 8.8.8.8)"; ip="${ip##*\"prefsrc\":\"}"; ip="${ip%%\"*}"
– Rowan Thorpe Mar 27 '24 at 18:38
127.0.0.1
IPv4 address and a::1
IPv6 interface on the loopback network. Which one do you want to find? – Stéphane Chazelas Aug 20 '13 at 16:11eth0
section onifconfig
that is labeledinet addr:
– amphibient Aug 20 '13 at 16:15