How to Get IP Address of a Specific Interface (eth1) in Linux Using Bash Commands


1 views

When working with Linux systems, you often need to check the IP address assigned to a specific network interface. The eth1 interface is commonly used for secondary network connections.

The most reliable method is using the ip command from iproute2 package:

ip -4 addr show eth1 | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}'

This command:

  1. Shows IPv4 addresses for eth1
  2. Uses grep with Perl regex to extract just the IP

For systems without iproute2, you can use the older ifconfig:

ifconfig eth1 | grep -Eo 'inet [0-9.]+' | cut -d' ' -f2

For scripting purposes, you might want to store the IP in a variable:

IP_ADDR=$(ip -4 addr show eth1 | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}')
echo "Eth1 IP is: $IP_ADDR"

Other ways to get interface IP:

# Using hostname command
hostname -I | awk '{print $2}'

# Using nmcli (for NetworkManager systems)
nmcli device show eth1 | grep IP4.ADDRESS

Add error checking in your scripts:

if ! ip -4 addr show eth1 &>/dev/null; then
    echo "Error: eth1 interface not found" >&2
    exit 1
fi

If the interface has multiple IPs, this variant gets all of them:

ip -4 addr show eth1 | grep -oP '(?<=inet\\s)\\d+(\\.\\d+){3}' | paste -sd,

The most reliable way to get the IP address of a specific interface in modern Linux systems is:

ip -4 addr show eth1 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

In Linux, network interfaces like eth1 (Ethernet interface 1) can have multiple IP addresses assigned. When you need to programmatically retrieve this information in bash scripts, you have several options:

The ip command from iproute2 package has replaced ifconfig in most modern distributions:

# Get IPv4 address only
ip -4 addr show eth1 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'

# Get complete information
ip addr show eth1

For older systems still using net-tools:

ifconfig eth1 | grep -Eo 'inet [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk '{print $2}'

For a simple IP address display (only works for primary address):

hostname -I | awk '{print $1}'

For reusable code in your scripts:

get_interface_ip() {
    local interface=$1
    ip -4 addr show "$interface" | grep -oP '(?<=inet\s)\d+(\.\d+){3}' || echo "No IP assigned"
}

# Usage:
eth1_ip=$(get_interface_ip eth1)
echo "eth1 IP Address: $eth1_ip"

If your interface has multiple IPs, this script will list them all:

ip -4 addr show eth1 | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | while read ip; do
    echo "Found IP: $ip"
done

Before querying the IP, check if interface exists:

if ip link show eth1 >/dev/null 2>&1; then
    echo "eth1 exists"
    # Get IP code here
else
    echo "eth1 doesn't exist" >&2
    exit 1
fi

For scripts running frequently, ip is faster than ifconfig as it directly reads from kernel tables rather than parsing formatted output.

For maximum efficiency in high-performance scripts:

awk '/eth1$/{getline; print $2}' /proc/net/fib_trie | grep -vE '^127|0\.0'

Always include proper error handling in production scripts:

ip_output=$(ip -4 addr show eth1 2>&1)
if [ $? -ne 0 ]; then
    echo "Error: $ip_output" >&2
    exit 1
fi