If I want to display the IP address that is assigned to eth1, how can I do this in Bash?


Try this (Linux)

/sbin/ifconfig eth1 | grep 'inet addr:' | cut -d: -f2| cut -d' ' -f1

or this (Linux)

/sbin/ifconfig eth0 | awk -F ' *|:' '/inet addr/{print $4}'

or this (*BSD)

ifconfig bge0 | grep 'inet' | cut -d' ' -f2

or this (Solaris 10)

ifconfig e1000g0 | awk '/inet / {print $6}'

Obviously change the interface name to match the one you want to get the information from.

  • 2
    worked also ifconfig eth1| awk -F ' *|:' '/inet addr/{print $4}' – user47556 Oct 27 '10 at 11:37
  • 1
    On *BSD systems the ifconfig output is a bit different - ifconfig bge0 | grep 'inet' | cut -d' ' -f2 will work (substitute your appropriate interface name in place of bge0, obviously) – voretaq7 Aug 15 '11 at 18:08
  • ip addr show eth1| grep inet|awk '{print $2;}' – navaho Aug 16 '11 at 22:18

A better way: get ip adress from command "ip", because "ifconfig" is out of date. Otherwise you will get a problem on using "ifconfig", because the output of ifconfig is language dependend.

I use this command to get all IPs (IPv4):

ip addr show | grep -o "inet [0-9]*\.[0-9]*\.[0-9]*\.[0-9]*" | grep -o "[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"
| improve this answer | |
  • 1
    ifconfig has the advantage of existing on systems that aren't Linux... – voretaq7 Dec 7 '12 at 3:59

On a Linux system:

hostname --all-ip-addresses

will give you only the IP address.

On a Solaris system use:

ifconfig e1000g0 | awk '/inet / {print $2}'
| improve this answer | |
  • 1
    From the hostname(1) man page: Avoid using this option; use hostname --all-ip-addresses instead. – bleater Nov 14 '17 at 1:06

As @Manuel mentioned, ifconfig is out of date, and ip is the recommended approach going forward.

ip -f inet addr show eth1

and to use @bleater's sed or @Jason H.'s awk to filter the output (depending on if you want the mask)

ip -f inet addr show eth1 | sed -En -e 's/.*inet ([0-9.]+).*/\1/p'

ip -f inet addr show eth1 | awk '/inet / {print $2}'

| improve this answer | |

maybe this will help

ifconfig eth1
| improve this answer | |

One liner with sed:

ifconfig wlan0 | sed -En -e 's/.*inet ([0-9.]+).*/\1/p'

Replace wlan0 with the desired interface.

| improve this answer | |

I tried the accepted answer on CentOS 7, but it does not work.

/sbin/ifconfig eth0 | grep 'inet ' | tr -s ' ' | cut -d" " -f3

worked for me, in case someone else is also running into the same problem.

| improve this answer | |
  • 1
    As mentioned by @Manuel and @pstanton, ifconfig should be avoided. It's actually removed in some newer distros. This is probably why your answer was downvoted. – Server Fault May 10 '18 at 19:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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