How can I display the IP address shown on eth0 using a script ?

17 Answers 17


Note: This answer is for older systems. If this does not work for you please consider other answers. This answer is not incorrect.

save this in a file and then run bash <filename>

ifconfig eth0 | grep "inet addr"

being more accurate to get only number showing IP address:

ifconfig eth0 | grep "inet addr" | cut -d ':' -f 2 | cut -d ' ' -f 1

Update: If this doesn't works for you, try the other answer

Update: For Ubuntu 18+, try: (don't forget to replace eth0 with interface you need the IP for. Thanks to @ignacio )

ifconfig eth0 | grep "inet " | awk '{print $2}'
| improve this answer | |
  • 4
    of course this doesn't work in the latest ubuntu. the latest ifconfig returns "inet <ip>" instead of "inet addr <ip>" – thang Nov 14 '18 at 23:10
  • 2
    you can just use grep "inet" – Andrei Radulescu Nov 10 '19 at 15:11
  • here is an updated solution for ifconfig version "net-tools 2.10-alpha" : ifconfig eth0 | grep "inet" | grep -v "inet6" | sed -e 's/^[[:space:]]*//' | cut -d ' ' -f 2 – Maxime Ancelin Feb 13 at 9:03
  • 1
    @MaximeAncelin you should add new answer for this – Edward Torvalds Feb 13 at 9:57
  • 1
    quick update for Ubuntu 18.04: ifconfig wlan0 | grep "inet " | awk '{print $2}' – ignacio Oct 28 at 16:55

For the sake of providing another option, you could use the ip addr command this way to get the IP address:

ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1
  • ip addr show eth0 shows information about eth0
  • grep "inet\b" only shows the line that has the IPv4 address (if you wanted the IPv6 address, change it to "inet6\b")
  • awk '{print $2}' prints on the second field, which has the ipaddress/mask, example
  • cut -d/ -f1 only takes the IP address portion.

In a script:

theIPaddress=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)
| improve this answer | |
  • this solution actually works! – thang Nov 14 '18 at 23:10
  • ip -4 ... and ip -6 ...! Thank you! – TamusJRoyce May 22 '19 at 18:12
  • ip addr show eth0 | grep -P "inet6?\b" | awk '{print $2}' | cut -d/ -f1 will print both IPv4 and IPv6. – Artem Russakovskii Jun 4 at 8:14

Taken from https://stackoverflow.com/a/14910952/1695680

hostname -i

However that may return a local ip address (, so you may have to use, and filter:

hostname -I

From hostname's manpages:

-i, --ip-address

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.

-I, --all-ip-addresses

Display all network addresses of the host. This option enumerates all configured addresses on all network inter‐faces. 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.

| improve this answer | |
  • 1
    for the record, I like ip addr show label 'enp*' better, but I is annoying parse, something like ip addr show label 'enp*' | grep -oP inet\ \\S+ | cut -d' ' -f2 can work... how pretty – ThorSummoner Nov 20 '18 at 23:37
  • ip route | grep default | grep -oP 'dev \S+' | cut -d' ' -f 2 | xargs ip addr show dev | grep -oP 'inet6? \S+' – ThorSummoner Apr 11 at 1:43

@markus-lindberg 's response is my favourite. If you add -o -4 to ip's flags then you get a much more easily parsable (and consistent) output:

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

-o stands for --oneline, which is meant to help in exactly this kind of situations. The -4 is added to limit to the IPv4 address, which is what all the other responses imply.

| improve this answer | |
  • Love the ip flags. Using cut rather than advanced awk wizardry: ip -o -4 addr show eth0 scope global | awk '{print $4;}' | cut -d/ -f 1 – Dawngerpony Dec 16 '16 at 14:03
  • @DuffJ it's probably down to a matter of personal taste. I "discovered" cut way after I learned about awk, and I like minimising the number of commands on my pipelines. Nice suggestion in any case. – Amos Shapira Dec 17 '16 at 6:55
  • I completely agree, Amos. Thanks for your solution! – Dawngerpony Dec 19 '16 at 12:34
  • Very helpful. This should be the accepted answer, and this is not using the deprecated ifconfig which needs you to explicitly install net-tools in Ubuntu. – Haris Jun 11 at 17:28

You should use ip (instead of ifconfig) as it's current, maintained, and perhaps most importantly for scripting purposes, it produces a consistent & parsable output. Following are a few similar approaches:

If you want the IPv4 address for your Ethernet interface eth0:

$ ip -4 -o addr show eth0 | awk '{print $4}'  

As a script:

$ INTFC=eth0  
$ MYIPV4=$(ip -4 -o addr show $INTFC | awk '{print $4}') 
$ echo $MYIPV4

The output produced above is in CIDR notation. If CIDR notation isn't wanted, it can be stripped:

$ ip -4 -o addr show eth0 | awk '{print $4}' | cut -d "/" -f 1  

Another option that IMHO is "most elegant" gets the IPv4 address for whatever interface is used to connect to the specified remote host ( in this case). Courtesy of @gatoatigrado in this answer:

$ ip route get | awk '{ print $NF; exit }'

As a script:

$ RHOST=  
$ MYIP=$(ip route get $RHOST | awk '{ print $NF; exit }')
$ echo $MYIP

This works perfectly well on a host with a single interface, but more advantageously will also work on hosts with multiple interfaces and/or route specifications.

While ip would be my preferred approach, it's certainly not the only way to skin this cat. Here's another approach that uses hostname if you prefer something easier/more concise:

$ hostname --all-ip-addresses | awk '{print $1}'  

Or, if you want the IPv6 address:

$ hostname --all-ip-addresses | awk '{print $2}'  

As a script:

$ MYV4IP=$(hostname --all-ip-addresses | awk '{print $1}') 
$ MYV6IP=$(hostname --all-ip-addresses | awk '{print $2}')
$ echo $MYV4IP 
$ echo $MYV6IP 
| improve this answer | |

Here are some oneliners.....


ifconfig eth0 | awk '/inet addr/{split($2,a,":"); print a[2]}'

split function in the above awk command splits the second column based on the delimiter : and stores the splitted value into an associative array a. So a[2] holds the value of the second part.


ifconfig eth0 | sed -n '/inet addr/s/.*inet addr: *\([^[:space:]]\+\).*/\1/p'

In basic sed , \(...\) called capturing group which is used to capture the characters. We could refer those captured characters through back-referencing. \([^[:space:]]\+\) captures any character but not space one or more times.


ifconfig eth0 | grep -oP 'inet addr:\K\S+'

\K discards the previously matched characters from printing at the final and \S+ matches one or more non-space characters.


ifconfig eth0 | perl -lane 'print $1 if /inet addr:(\S+)/'

One or more non-space characters which are next to the inet addr: string are captured and finally we print those captured characters only.

| improve this answer | |
  • @edwardtorvalds added some explanation. I think this would be helpful for future readers. Feel free to ask any questions from the above commands... :) – Avinash Raj Dec 12 '14 at 8:07
ip addr|awk '/eth0/ && /inet/ {gsub(/\/[0-9][0-9]/,""); print $2}'

This only use ip addr which is a replacement for ifconfig and awk combined with substitution (gsub).

Stop using too many processes for simple tasks

| improve this answer | |

Just one more option that can be useful if you don't have awk (as it is the case in some embedded devices):

ip addr show dev eth0 scope global | grep "inet\b" | cut -d/ -f 1 | egrep -o "([[:digit:]]{1,3}[.]{1}){3}[[:digit:]]{1,3}"
| improve this answer | |

Here's a good one, only uses grep as secondary command:

ip addr show eth0 | grep -oP 'inet \K\S[0-9.]+'

I don't see why you should use more commands than needed

| improve this answer | |

ifconfig eth0|grep 'inet '|awk '{print $2}'

| improve this answer | |

here's for IPv4:

ip -f inet a|grep -oP "(?<=inet ).+(?=\/)"

here's for IPv4 & particular dev (eth0):

ip -f inet a show eth0|grep -oP "(?<=inet ).+(?=\/)"

for IPv6:

ip -6 -o a|grep -oP "(?<=inet6 ).+(?=\/)"

| improve this answer | |

I suggest using a python library like netifaces that is specifically designed for this purpose.

sudo pip install netifaces
python -c "import netifaces; print netifaces.ifaddresses('eth0')[netifaces.AF_INET][0]['addr']"

To obtain the default network interface that is in use.

default_inf = netifaces.gateways()['default'][netifaces.AF_INET][1]
| improve this answer | |

this can be used with a normal user too.

ip addr show eth0 | grep "inet " | cut -d '/' -f1 | cut -d ' ' -f6
| improve this answer | |
  • he ask for eth0, this version of your script could help (also show loopback tho) ip addr show | grep "inet " | cut -d '/' -f1 | cut -d ' ' -f6 – TiloBunt Mar 25 '17 at 17:01
  • This is pretty much the same answer as askubuntu.com/a/560466/367990, just using cut twice instead of a combination of awk and cut to parse the output. Next time you should better check out all other answers first and ensure you don't post a duplicate solution. In this case here, I think it's arguable whether it's a duplicate or just similar, so please take it as a general hint. Thanks. – Byte Commander Jul 7 '17 at 19:49

Yet another way (assuming you don't want a CIDR address and want IPv4):

$ ip -br -4 addr show dev eth0 | awk '{split($3,a,"/"); print a[1]}'
  • Uses the ip command which is not deprecated
  • Uses only one command for filtering
| improve this answer | |

This is the shortest way I could find:

ip -f inet addr show $1 | grep -Po 'inet \K[\d.]+'

Replace $1 with your network interface.

ip -f inet tells ip to only return values for the inet (ipv4) family.

grep -Po tells grep to interperate the next value as a perl-regex, and only print the matching values.

The regex \K[\d.]+ says "throw away everything up to this point (\K), and match as many numeric values followed by a dot in a row as possible". This will therefore only match the IP address and ignore everything after it, including the shortform \XX subnet mask.

| improve this answer | |

in these days with multiples interfaces (eg if you use a docker) and naming interface by ETH is not anymore the norms

I use this command to extract the IP/Mask :

IPMASK=$(ip a s|grep -A8 -m1 MULTICAST|grep -m1 inet|cut -d' ' -f6)

So whatever how many interfaces I'll have and whatever their name, GREP will only grab the first having the MULTICAST option.

I use this command to extract only the IP without the mask :

IP=$(ip a s|grep -A8 -m1 MULTICAST|grep -m1 inet|cut -d' ' -f6|cut -d'/' -f1)

I use these command on different BDS & NIX it never fail ;)

| improve this answer | |
  • If you're going to parse the output of ip, use the -o option. – muru Oct 26 '17 at 8:31

In my script I'm using something like that:

if [[ $(ip addr show eth0) =~ $re ]]; then
    echo ${BASH_REMATCH[1]}
    echo "Cannot determin IP" 1>&2

It doesn't spawn any process.

| improve this answer | |

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