How can I display the IP address of an interface?

44,534

Solution 1

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.

Solution 2

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

Solution 3

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

Solution 4

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]*"

Solution 5

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
Share:
44,534

Related videos on Youtube

user47556
Author by

user47556

Updated on September 17, 2022

Comments

  • user47556
    user47556 over 1 year

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

  • user47556
    user47556 over 13 years
    worked also ifconfig eth1| awk -F ' *|:' '/inet addr/{print $4}'
  • voretaq7
    voretaq7 almost 13 years
    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)
  • navaho
    navaho almost 13 years
    ip addr show eth1| grep inet|awk '{print $2;}'
  • voretaq7
    voretaq7 over 11 years
    ifconfig has the advantage of existing on systems that aren't Linux...
  • bleater
    bleater over 6 years
    From the hostname(1) man page: Avoid using this option; use hostname --all-ip-addresses instead.
  • Server Fault
    Server Fault about 6 years
    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.
  • red0ct
    red0ct over 3 years
    NB: This is only about IPv4-addresses.
  • red0ct
    red0ct over 3 years
    This answer is also fit if you need only IPv4-addresses.
  • copycat
    copycat about 3 years
    "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.
  • Kiwy
    Kiwy almost 3 years
    amazing tips loving it thank you very much
  • red0ct
    red0ct over 2 years
    @copycat What is the need to ping your own interface?
  • Admin
    Admin almost 2 years
    ip -f inet6 addr show eth1 | sed -En -e 's/.*inet6 ([0-9a-fA-F:]+).*/\1/p' for ipv6