31

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

14 Answers 14

18

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.

3
  • 2
    worked also ifconfig eth1| awk -F ' *|:' '/inet addr/{print $4}'
    – user47556
    Oct 27, 2010 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, 2011 at 18:08
  • 1
    ip addr show eth1| grep inet|awk '{print $2;}'
    – navaho
    Aug 16, 2011 at 22:18
25

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}'

4
  • This answer is also fit if you need only IPv4-addresses.
    – red0ct
    Nov 11, 2020 at 14:54
  • ip -f inet6 addr show eth1 | sed -En -e 's/.*inet6 ([0-9a-fA-F:]+).*/\1/p' for ipv6 Jun 30, 2022 at 14:39
  • ip -f inet addr show lo | awk '/inet / {print $2}' gives me 127.0.0.1/8, not the desired 127.0.0.1.
    – Abdull
    Oct 31, 2022 at 10:40
  • ip -4 -o addr show eth0 | awk '{print $4}' | cut -d "/" -f 1 if you don't like sed
    – MoonRaiser
    Feb 13, 2023 at 12:02
16

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}'
2
  • 2
    From the hostname(1) man page: Avoid using this option; use hostname --all-ip-addresses instead.
    – bleater
    Nov 14, 2017 at 1:06
  • amazing tips loving it thank you very much
    – Kiwy
    Jun 21, 2021 at 10:37
10

To obtain both IPv4 and IPv6 IP addresses with netmasks just try:

ip a l eth1 | awk '/inet/ {print $2}'

Or without netmasks (can't imagine why you need an IP address without a mask):

ip a l eth1 | awk '/inet/ {print $2}' | cut -d/ -f1
4
  • 5
    "can't imagine why you need an IP address without a mask" Simple, there's very few clients that support it. You can't ping 1.1.1.1/32. 1.1.1.1/32 would return a 404. You can't point an A record to it, nor insert it into a reverse proxy config, nor tunnel to it, nor put it into /etc/hosts.
    – copycat
    Mar 3, 2021 at 1:46
  • @copycat What is the need to ping your own interface?
    – red0ct
    Jan 18, 2022 at 13:14
  • "l" stands for list (not documented in inline help (iproute2 version 5.5.0), only documented in manpage).
    – 3r1d
    Jul 27, 2023 at 3:33
  • @red0ct for anyone writing a script where the IP needs to be an input for example. Pairing an IP to a Mac address, the cidr range is already known by the system that wants this information. Passing the cidr range would require more processing somewhere down the line, so it's just a matter of where you want to do that work. Feb 28 at 20:10
9

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]*"
2
  • 1
    ifconfig has the advantage of existing on systems that aren't Linux...
    – voretaq7
    Dec 7, 2012 at 3:59
  • 1
    NB: This is only about IPv4-addresses.
    – red0ct
    Nov 11, 2020 at 14:51
6

Works well on various linux:

ip -brief address show eth0 | awk '{print $3}' | awk -F/ '{print $1}'
2

maybe this will help

ifconfig eth1
0
2

on Ubuntu 20.04

$ sudo apt install moreutils
$ ifdata -pa eth0
192.168.1.152

2

On recent OS and tools versions, using JSON interface should work too, e.g.:

 ip -json addr show docker0 \
 | jq '.[].addr_info[] | select(.family == "inet").local'

Which yields on my machine:

 "172.17.0.1"
2
  • This is actually my favorite, thanks! Feb 28 at 19:46
  • To make it run from a subprocess: local_ips = subprocess.check_output(["ip -json addr show eth0 | jq -r '.[].addr_info[] | select(.family == \"inet\" | @sh).local'"], shell=True, encoding='utf8') Feb 28 at 20:07
1

ip addr show dev eth0 if you want to see all addresses for one interface

2
  • Welcome to the server fault community. How does this answer differ from previously given answers?
    – John K. N.
    Jan 22 at 9:58
  • Thanks for the welcome @JohnK.N. The previously given answers have a lot of grep, sed and awk that felt unnecessary for answering the simple question asked. The accepted answer uses a deprecated command. If you feel my answer isn't valid or helpful I can certainly remove it.
    – bryvo01
    Jan 23 at 13:06
0

One liner with sed:

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

Replace wlan0 with the desired interface.

0

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.

1
  • 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. May 10, 2018 at 19:30
0

This is what I use:

ip r s | awk '/eth0/ {print $7}'

will print, for example: 192.168.0.1

0

On MacOS I use ipconfig getifaddr <interface-name>
The command is listed under the BSD System Manager's Manual.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .