Bash Script to List All IP Interfaces

marius

Administrator
Staff member
In this guide, we explain how to create a bash script that lists all IP interfaces on a Linux system using a simple command. The script uses the 'ip' command to fetch network interface details. Below, you'll find the code enclosed in a BB code block, instructions on how to run the script, and an example of its output.

Bash:
#!/bin/bash

# This script lists all network interfaces and their IP addresses

# Check if the 'ip' command exists
if ! command -v ip &> /dev/null; then
    echo "Error: 'ip' command not found. Please install iproute2 package."
    exit 1
fi

# List all IP interfaces using 'ip addr show'
ip addr show

exit 0

How to run the script:
1. Save the script above into a file, for example, list_interfaces.sh
2. Make the script executable by running:
Code:
chmod +x list_interfaces.sh
3. Execute the script by running:
Code:
./list_interfaces.sh

Example output (the actual output may vary based on your system configuration):
Code:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 12:34:56:78:9a:bc brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.10/24 brd 192.168.1.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::1234:56ff:fe78:9abc/64 scope link
       valid_lft forever preferred_lft forever
 
Back
Top