How to Find out the IP address assigned to eth0 and display IP only

I need to get the IP address assigned to the eth0 Linux interface. How do I find out IP address only? I don’t want other information displayed by Linux ifconfig command OR ip command. How can I find out the IP address assigned to eth0 and display it on screen?

For shell script and maybe for other causes, you may need the IP address only. You can use the ifconfig command or ip command with grep command and other filters to find out an IP address assigned to eth0 and display it on screen.
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux with ifconfig and ip command
Est. reading time 2m

Finding ip address assigned to eth0 using ifconfig command

Warning: You may see “bash: ifconfig: command not found” error as most modern Linux distros removed the ifconfig in favour of the ip command. See below how to use the ip command to grab the IP address.

The default output of /sbin/ifconfig command is all interfaces as follows:
$ /sbin/ifconfig
Sample outputs:

lo        Link encap:Local Loopback
          inet addr:  Mask:
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:69527 errors:0 dropped:0 overruns:0 frame:0
          TX packets:69527 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:41559546 (39.6 MiB)  TX bytes:41559546 (39.6 MiB)

eth0      Link encap:Ethernet  HWaddr 00:17:9A:0A:F6:44
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::217:9aff:fe0a:f644/64 Scope:Link
          RX packets:227614 errors:0 dropped:0 overruns:0 frame:0
          TX packets:60421 errors:0 dropped:0 overruns:0 carrier:0
          collisions:272 txqueuelen:1000
          RX bytes:69661583 (66.4 MiB)  TX bytes:10361043 (9.8 MiB)

ra0       Link encap:Ethernet  HWaddr 00:50:56:C0:00:01
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::250:56ff:fec0:1/64 Scope:Link
          RX packets:1024 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1320 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)

Now you just select eth0 as follows:
$ /sbin/ifconfig eth0

Now you just wanted the IP address, use grep to get the IP:
$ /sbin/ifconfig eth0| grep 'inet addr:'
Here is information:

inet addr:  Bcast:  Mask:

To get the IP address from using the cut command:
$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2
Now I see:  Bcast

Finally, remove Bcast with awk as follows:
$ /sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'

Using hostname and ip command for finding out the IP address assigned to eth0

The syntax is as follows to display the network address(es) of the host name:
$ hostname -i
We can also use the following option to find out all network IP addresses of the host. This option enumerates all configured addresses on all network interfaces:
$ hostname -I
Of course we can use the combination as follows too along with the egrep command for br0 interface:

ifconfig br0 | egrep -o 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'  | cut -d' ' -f2
# get an IP and store to bash variable #
my_br_ip=$(ifconfig br0 | egrep -o 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'  | cut -d' ' -f2)
# display it #
echo "$my_br_ip"

Getting the IP address assigned to eth0 and display IP address using the ip command

As you know, ifconfig is deprecated in favor of iproute2, and the ip command. Hence, our final solution recommend using the ip command only instead of ifconfig:

# eth0 IPv4 #
ip a s eth0 | egrep -o 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d' ' -f2
# br0 IPv4 #
ip a s br0 | egrep -o 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | cut -d' ' -f2

Our final solution to grab an IP address depends upon bash feature called parameter substitution:

# Step 1. Grab the IP for br0 interface using the egrep  and store to the value #
value="$(ip a s br0 | egrep -o 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')"
# Step 2. Remove inet part from right side 
echo "${value##inet}"

Click to enlarge the image

Linux show interfaces ip address

I would use the following ip command:
$ ip -br addr show
It print only basic information in a tabular format for better readability.

lo               UNKNOWN ::1/128 
enp0s31f6        UP             
wlp82s0          DOWN           
br0              UP    
virbr0           DOWN  
virbr0-nic       DOWN           
lxdbr0           UP    fd42:87d0:ec52:7d50::1/64 fe80::216:3eff:fedd:4a6b/64 
vethd48ca772@if15 UP             
mum-wg0          UNKNOWN fd9d:bc11:4021::2/48 fe80::5dfb:6ed3:c11e:f614/64

The -br option is currently only supported by ip addr show and ip link show commands to list all interfaces:
$ ip -br link show

Wrapping up

This article explained how easy and straightforward it could be to grab an IP address for the interface such as br0 or eth0 using the ifconfig command on Linux. But, ifconfig is deprecated and replaced with a modern alternative called ip utility. Hence, I would suggest using ip command instead of hostname and ifconfig. See how to read UNIX/Linux system IP address in a shell script.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 53 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
53 comments… add one
  • Brijesh Oct 17, 2006 @ 11:42

    if u have idea to configar different server on linux and network plsase send me all informatios ….

    with thanks

    • helped Nov 24, 2014 @ 11:30

      Thank you.

    • Everton Nov 26, 2014 @ 21:24

      perfect! thanks

  • Skuutter Oct 24, 2007 @ 8:17

    another option is to use “ip” command:
    ip addr list eth0 |grep inet |cut -d' ' -f6|cut -d/ -f1
    which prints both ipv4 and ipv6 addresses, ipv4 addr can be printed using tighter grep expression:
    ip addr list eth0 |grep "inet " |cut -d' ' -f6|cut -d/ -f1
    ip addr list eth0 |grep "inet6 " |cut -d' ' -f6|cut -d/ -f1

  • Mar 11, 2008 @ 9:43

    For those on OS X’s

    ifconfig en1 | grep 'inet ' | cut -d ' ' -f 2

    • doug Jan 10, 2011 @ 16:10

      thank you, exactly what I’m looking for (now in my bash profile):
      alias ip=”ifconfig en0 | grep ‘inet ‘ | cut -d ‘ ‘ -f 2″

      • Keith Sep 30, 2015 @ 23:53

        I wouldn’t know your system but ip is a command that distributes with several linux distributions as /sbin/ip
        Alternatively you might try this:
        alias ip_addr="ifconfig en0 | grep 'inet ' | cut -d ' ' -f 2"

    • feyd Feb 3, 2012 @ 20:06

      for OS X this is much better.

      ipconfig getifaddr en0

      ipconfig getifaddr en1

  • Vicente Aug 12, 2008 @ 16:15

    ifconfig eth0 | sed -n ‘s/.*dr:\(.*\) Bc.*/\1/p’

  • Bob Oct 13, 2008 @ 10:57

    $ hostname -i

    • matthew Jun 26, 2016 @ 19:13

      Bob, sorry, hostname -i is a bit slow.

  • MauRice Dec 6, 2009 @ 12:01

    Put it in your “.bashrc” file.
    Output when you start a konsole/terminal:
    WAN IP:
    LAN IP: bbb.ccc.ddd.eee

  • John Apr 26, 2010 @ 6:10

    Bob wins. The rest of you should go self-flagellate for being moronic punks.

    Rules of thumb for efficient text processing:

    a. Avoid the need to do so. If there’s a command that outputs exactly what you need, use it. (For example, in our case here, using ‘hostname -i’ instead of stupidly dicking around with ifconfig, cut, grep, sed, and awk). Use some common sense, for Christ’s sake. Think maybe nobody else ever needed to include their IP address in a command? Before you go monkey-spanking around with awk or perl, see if there’s an appropriate command.

    b. if there’s a shell built-in that can do it, use that (your shell is already loaded into memory, and there is overhead in making a call to an external). For example, if you can parse a line effectively using the shell’s string substitution features, then do so. Don’t be a dumb-ass and waste your RAM and CPU. Read your shell’s man page.

    b. If the shell can’t do it, use the simplest alternative that can. If cut or tr will work, use that. If not, use sed. If not use awk.

    c. The biggest sin of all, an abomination of a command that pipes together more than one of the above, awking the output of grep, for example.

    • William Jul 15, 2010 @ 13:09

      While I agree with your principles, Bob is actually wrong – hostname does a lookup into the /etc/hosts file and may or may not give you the IP address of eth0. It depends on what you have in your hosts file. For example, I have a machine where “hostname -i” returns, simply because there was no name associated with the IP address of eth0 (
      Grep/awk/sed pipes are costly in regards to system resources as you outlined, but sometimes you have to use them. What would be better is if there were a command that simply went “get-ip-address eth0” and returned the IP, but I have not found one as of yet.

    • bigdavejonnyt Mar 16, 2011 @ 18:26


      Wow, you’re rather fun, aren’t you? Let’s dismiss beginners and overlook the intrinsic value of getting a look at how cut, grep and the others work for those folk [incl. me] and let’s just show how you’ve happily traded ‘my linux is better than yours’ for people skills.

    • Nikolas Britton Jan 18, 2017 @ 16:05

      Actually, hostname -i is a bad choice because you can’t be sure it will return the ip of eth0. It could return, or whatever is configured in /etc/hosts. Also hostname -I is a bad choice because on a multi-NIC system it will return more than one ip address.

      Another option is:
      ETH0_IP=$(ifconfig eth0 | awk ‘/inet / {print $2}’);

      I like the above solution because if an ip is not set then awk will return nothing, this is good because you can then do error checking, such as:

      if [[ -z ${ETH0_IP+x} ]]; then echo “eth0 ip not set.”; fi

      • Nikolas Britton Jan 18, 2017 @ 16:15

        Here is an example of error checking in action:

        if [[ -z ${HTTP_IP_ADDRESS+x} ]]; then
        HTTP_IP_ADDRESS=$(ifconfig br-ctlplane | awk '/inet / {print $2}');

        if [[ -z ${HTTP_IP_ADDRESS} ]]; then
        echo "Environment variable HTTP_IP_ADDRESS is not set, aborting.";
        exit 1;

  • Fannar May 19, 2010 @ 19:12

    Recommend “ip addr” for those with many VLAN IPs

    “ip” is in the “iproute” package, “apt-get install iproute”

  • Laurent Hangard Aug 14, 2010 @ 7:26

    A small comment: in my ubuntu version 8.10, ifconfig gives inet adr,( and not inet addr).
    Thanks for the tip.

  • thiagoc Aug 23, 2010 @ 16:23

    eth0=($(ifconfig eth0 | grep “inet addr” | tr “:” ” “))
    echo ${eth0[2]}

  • Alex Dec 30, 2010 @ 16:29

    I am not agree with John,
    with comments like this “Don’t be a dumb-ass and waste your RAM and CPU”.
    It looks like you living in 19 century, you better group old man, this days RAM is cheap and CPU is big, and don`t jump on people who like to experiment with thinks.

    Who like to experiment try this:
    hostname -I | cut -d: -f2 | awk ‘{ print $1}’
    or this
    hostname -I | cut -d: -f2 | awk ‘{ print $2}’

  • Alex Dec 30, 2010 @ 18:32

    even this will work
    hostname -I | awk ‘{ print $1}’
    hostname -I | awk ‘{ print $2}’

  • Christoph May 12, 2011 @ 11:24

    hostname -I works to

    • balucio Jul 7, 2011 @ 15:04

      The ifconfig output in some system is localized and the awk version not work

      • Kaacz Apr 17, 2014 @ 16:15

        Use LANG=C on top of shell script… :)

  • WHD-Maru Aug 17, 2011 @ 18:25

    very useful tips,
    In my case, I want to list all my server IPs assigned to eth0, the ifconfig results something like

    eth0 Link encap:Ethernet HWaddr 00:16:3E:D8:2B:BF
    inet addr: Bcast: Mask:
    inet6 addr: fe80::216:3eff:fed8:2bbf/64 Scope:Link
    RX packets:4311554 errors:0 dropped:0 overruns:0 frame:0
    TX packets:3027306 errors:0 dropped:0 overruns:0 carrier:0
    collisions:0 txqueuelen:1000
    RX bytes:944036536 (900.3 MiB) TX bytes:6041030741 (5.6 GiB)

    eth0:0 Link encap:Ethernet HWaddr 00:16:3E:D8:2B:BF
    inet addr: Bcast: Mask:

    eth0:1 Link encap:Ethernet HWaddr 00:16:3E:D8:2B:BF
    inet addr: Bcast: Mask:

    currently, for single IP, I can use this command
    SERVERIP=`grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 | awk -F= ‘{print $2}’`
    echo $SERVERIP

    How to list all of my IPs?
    Please reply to my email :)
    Many thanks

  • Jason Oct 11, 2011 @ 4:54

    I prefer doing it this way…

    ifconfig eth0 | grep ‘inet addr:’ | cut -d”:” -f2 | cut -d” ” -f1

    If hostname -i or hostname -I don’t give me what I want.

    • Keith Oct 1, 2015 @ 0:01

      hostname -I (capital i) works nice except on my linux router ware it returns 2 addresses.

  • Kevin Apr 4, 2012 @ 17:58

    I thought I’d post since I didn’t see this answer:

    cat /sys/class/net/eth0/address

    For my setup, hostname -i only gives me the loopback address (

    • Kevin Apr 4, 2012 @ 18:04

      whoops that’s the MAC address.

  • niko Apr 27, 2012 @ 17:59
    $ dnsdomainname -i

    does the same thing

  • bulb Jun 13, 2012 @ 15:29

    I try to avoid external programs for simple string task:

    ipaddr=$(/sbin/ifconfig eth0) ; ipaddr=${ipaddr/*inet addr:/} ipaddr=${ipaddr/ */}
    echo "$ipaddr"
  • xIN3N Jul 18, 2012 @ 22:34

    Of course “dnsdomainname -i” really does the same thing but the discussion is about the details and understanding of how to get the proper value by chopping again and again…!

  • Dallas Aug 31, 2012 @ 2:56

    I hardcode my IP address in /etc/sysconfig/network-scripts/ifcfg-eth3; yet when I list it (ifconfig -a) I do not see my IP address that I hardcoded? please help.

  • balaji Sep 19, 2012 @ 11:03

    ifconfig en0 | grep inet[^6]| cut -d' ' -f2

  • tomt Mar 10, 2013 @ 22:51

    Actually, hostname does not work. I am running a LAMP server so hostname gives . I have eth0 connected to a router and ONLY ifconfig gives me the correct IP. Bob is an idiot.

  • tomt Mar 10, 2013 @ 22:52

    John is an idiot for saying:

    “Bob wins. The rest of you should go self-flagellate for being moronic punks.”

    Sorry, Bob.

  • Stephan Eriksen Mar 19, 2013 @ 9:23

    You can do this easier and better:

    ip=$(echo "$(hostname -I) | grep -o '^[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')

    This don’t care what interface you are using.

  • Mac Apr 3, 2013 @ 2:40

    I am still trying to learn how to use grep and do not really have firm grasp of ‘regex’ can someone recommend a book to start out with?

  • Ayush Joshi Oct 7, 2013 @ 12:34
    wifiip=$(ip addr | grep inet | grep wlan0 | awk -F" " '{print $2}'| sed -e 's/\/.*$//')
    eth0ip=$(ip addr | grep inet | grep eth0 | awk -F" " '{print $2}' | sed -e 's/\/.*$//')
    # report findings, only returning devices with IPs
    if [ "$eth0ip" -eq " " ]  
    echo "$wifiip" | grep [0-9]$ > /home/pi/attendance/ip.txt
    echo "$eth0ip" | grep [0-9]$ > /home/pi/attendance/ip.txt

    i am trying to place working interface ip-address into a text file but its throwing error on executing this script 14: [: unexpected operator

  • MauRice Oct 8, 2013 @ 18:00


    Change if [ “$eth0ip” -eq ” ” ] in to if [ “$eth0ip” = ” ” ]

  • John H Feb 26, 2014 @ 9:34

    Cuts can be funky, here is one with only grep and awk:

    ifconfig eth0 | grep 'inet addr:' | awk '{print $2}' | awk -F':' '{print$2}'

  • Kaacz Apr 17, 2014 @ 16:16

    $ /sbin/ifconfig eth0 | grep ‘inet addr:’ | awk ‘{ print substr($1,5) }’

  • Berokor Oct 14, 2014 @ 13:04

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

    or for german users (german system language):
    ifconfig eth0 | grep 'inet Adr' | awk '{print $2}' | cut -d: -f2

    • SamTheViking Jul 9, 2015 @ 18:52

      Thanks, Berokor! For me, anyway, yours was the best solution and gave me exactly what I needed.

  • Dennis Gearon Apr 26, 2015 @ 2:32

    Thanks a goggleplex! Great scripting / text processing! Thanks for all your hours of learning and passing it on

  • weyasey Aug 2, 2015 @ 18:55

    Skuutter is the best option for me I’m using the ipv4 version he submitted

    ip addr list eth0 |grep "inet " |cut -d' ' -f6|cut -d/ -f1

    and it just works great for getting and inserting the ip address into a variable for the script I’m running.
    As to the complaints about complex commands if the n00bs need to find their ip addresses then just right click on the NetworkManager and chose connection details…Doooh

  • JB Sep 25, 2015 @ 21:47

    I came up with

    ip -4 addr show eth0 | grep -oP "(?<=inet).*(?=/)"

    But my vote still goes to Bob: hostname -i

  • ra Jan 10, 2016 @ 11:29

    ifconfig | grep ‘inet 192’ | awk ‘{print $2}’

  • b Feb 5, 2016 @ 18:03

    what about `ipconfig getifaddr en0`?

    might be os x only.

  • John Johnson Apr 20, 2016 @ 8:05

    I can’t say I fully understand why/how, but “bulb”‘s answer works for me on RHEL7:

    ipaddr=$(/sbin/ifconfig eth0) ; ipaddr=${ipaddr/*inet addr:/} ipaddr=${ipaddr/ */}
    echo $ipaddr

  • Jan 21, 2017 @ 12:13

    ifconfig | grep "inet.*broadcast" | grep -v | awk '{print $2}'


  • Vindolin Nov 24, 2020 @ 11:37

    My two cents:

    ifconfig eth0 | grep -oP '(?<=inet )([^ ]+) '

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum