Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

How can I assign the IP address of eth0 to an environment variable, say $ip, as easily as possible?

Update: Distro is Ubuntu Server 12.04 LTS.

share|improve this question

marked as duplicate by jasonwryan, Anthon, rahmu, manatwork, Evan Teitelman Jul 24 '13 at 13:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

A shorter (and I find more neat) way is hostname -i. No more hassle with ipconfig, ip, sed, awk and such.

share|improve this answer
+1, although note that my manpage says: -i: Display the network address(es) of the host name. Note that this works only if the host name can be resolved. Avoid using this option; use hostname --all-ip-addresses instead. – ire_and_curses Dec 19 '12 at 0:12
Unfortunately this only gives me This may be because machine is running behind NAT in VBox. Still a useful answer. Will upvote. – user204863 Dec 19 '12 at 8:35

Using ip address show is the way to go. Especially on any modern linux system where the interface you're querying could have multiple addresses that ifconfig wouldn't know about.

$ ip a s eth0 | awk '/inet / {print$2}'

And of course if you don't want the netmask, just pipe that to any number of things, e.g.:

cut -d/ -f1

Note: On the same system, ifconfig shows:

$ ifconfig em1 | awk '/inet / {print $2}'
share|improve this answer

Try doing this :

    ifconfig eth0 |
    perl -ne 'print $1 if /inet\s.*?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\b/'
echo "$ip"
share|improve this answer
Thank you very much, this was what I was looking for! :) – user204863 Dec 18 '12 at 23:10
If you think that the answer is useful, you can 'upvote' it. You can 'accept' the reply too by clicking the outline of the checkmark (will be green), this way, people searching stackexchange website will known that the question is well answered. That's how stackechange websites works, thanks ;) – Gilles Quenot Dec 18 '12 at 23:12
This only works if your ifconfig answers in English. For other languages, you need to change /inet\s+ad+r to what your version says – laurent Dec 19 '12 at 0:39
This didn't work for me at all. Why make it so complicated? – rsaw Dec 19 '12 at 1:29
@laurent, language don't change anything. Tested both in LANG=C and LANG=fr_FR.UTF-8. Moreover, tested OK on ubuntu 12.10 – Gilles Quenot Dec 19 '12 at 15:35

Not answer to your exact problem as you want the IP assigned to a defined interface but I thought it could be usefull to have listed here for future info the way to have your external IP (even if behind a NAT):

ip=`wget -qO- ipecho.net/plain`
share|improve this answer

As you can see, hostname -i can show (no NAT here (archlinux)), this is not what we want.

So I propose :

    ip a s dev $dev |
        awk '/inet /{gsub("/.*", "");print $2}'
echo "$ip"

Or if you have -P switch for grep :

ip a s dev eth0 | grep -oP 'inet\s+\K[^/]+'

I guess that's the shortest solution =)

share|improve this answer

Check out also:

ifconfig eth0 | awk '/inet /{print $2}' | cut -f2 -d':'

that will work even in Solaris and HP-UX (use appropriate net dev instead of eth0).

As for hostname -i command, try hostname -I if you have one configured interface (except loopback).

share|improve this answer

Yon can also try this,

IP=`wget -q -O- http://checkip.dyndns.org/index.html | grep 'IP'| html2text | cut -c 21-36`
share|improve this answer
Given that html2text is not a POSIX tool, could you specify from where we could take it? – manatwork Jul 24 '13 at 12:13

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