How can I display the IP address of an interface?

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:

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 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:

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