How to display IP address of eth0 interface using a shell script?

99,831

Solution 1

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 172.20.20.15/25
  • cut -d/ -f1 only takes the IP address portion.

In a script:

#!/bin/bash
theIPaddress=$(ip addr show eth0 | grep "inet\b" | awk '{print $2}' | cut -d/ -f1)

Solution 2

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>

#!/bin/bash
ifconfig eth0 | grep "inet addr"

being more accurate to get only number showing IP address:

#!/bin/bash
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}'

Solution 3

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

hostname -i

However that may return a local ip address (127.0.0.1), 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.

Solution 4

@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.

Solution 5

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}'
192.168.1.166/24  

As a script:

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

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 
192.168.1.166  

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

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

As a script:

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

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
192.168.1.166 
$ echo $MYV6IP 
2601:7c1:103:b27:352e:e151:c7d8:3379
Share:
99,831

Related videos on Youtube

user43389
Author by

user43389

Updated on September 18, 2022

Comments

  • user43389
    user43389 over 1 year

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

  • Dawngerpony
    Dawngerpony over 7 years
    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
  • Amos Shapira
    Amos Shapira over 7 years
    @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.
  • Dawngerpony
    Dawngerpony over 7 years
    I completely agree, Amos. Thanks for your solution!
  • TiloBunt
    TiloBunt about 7 years
    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
  • Byte Commander
    Byte Commander almost 7 years
    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.
  • muru
    muru over 6 years
    If you're going to parse the output of ip, use the -o option.
  • thang
    thang over 5 years
    of course this doesn't work in the latest ubuntu. the latest ifconfig returns "inet <ip>" instead of "inet addr <ip>"
  • thang
    thang over 5 years
    this solution actually works!
  • ThorSummoner
    ThorSummoner over 5 years
    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
  • TamusJRoyce
    TamusJRoyce almost 5 years
    ip -4 ... and ip -6 ...! Thank you!
  • Andrei Radulescu
    Andrei Radulescu over 4 years
    you can just use grep "inet"
  • Maxime Ancelin
    Maxime Ancelin about 4 years
    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
  • Edward Torvalds
    Edward Torvalds about 4 years
    @MaximeAncelin you should add new answer for this
  • ThorSummoner
    ThorSummoner about 4 years
    ip route | grep default | grep -oP 'dev \S+' | cut -d' ' -f 2 | xargs ip addr show dev | grep -oP 'inet6? \S+'
  • Artem Russakovskii
    Artem Russakovskii almost 4 years
    ip addr show eth0 | grep -P "inet6?\b" | awk '{print $2}' | cut -d/ -f1 will print both IPv4 and IPv6.
  • Haris
    Haris almost 4 years
    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.
  • ignacio
    ignacio over 3 years
    quick update for Ubuntu 18.04: ifconfig wlan0 | grep "inet " | awk '{print $2}'