1. Overview

In the realm of system administration, effectively managing network interfaces is a critical task. In essence, network interfaces serve as the connection points between the system and the network, making it essential to identify which interfaces are currently active and transmitting data.

By leveraging the ifconfig command, administrators can quickly ascertain which network interfaces are operational and troubleshoot any potential issues.

In this tutorial, we’ll delve into how we can use ifconfig to display only active interfaces, ensuring a focused and efficient approach to network management.

2. Understanding Network Interfaces

In this section, we’ll understand what are network interfaces and what is the difference between an active interface and an IP-assigned interface.

2.1. What Is a Network Interface?

Network interfaces act as gateways for a computer to connect to a network, whether a local area network (LAN) or the broader internet. Each interface corresponds to a hardware component or a software-defined virtual interface. Typically, a network interface has an associated IP address and a subnet mask, enabling communication between devices.

Network interfaces come in various types:

  • Network Interface Cards (NICs): these are physical components you insert into your computer’s motherboard to establish a physical connection to the network
  • Virtual Network Interfaces (VNIs): these are software-based interfaces that mimic the behavior of a physical NIC, often used in virtualized environments
  • Loopback Interfaces: these are virtual interfaces that create a connection within your computer itself, primarily for testing and troubleshooting purposes

2.2. Active and IP-Assigned Interfaces

Basically, an active interface is considered active when it’s marked as “up” and capable of communication, even if it’s not actively transmitting data at the moment. In addition, this status is indicated by the UP flag in the ifconfig output. Notably, an interface can be active without having an IP address assigned, such as in cases where it is waiting to be configured or is part of a bonding setup.

On the other hand, an IP-assigned interface is an interface that is assigned an IP address, it’s not only active but also capable of communicating within a network. The presence of an IP address allows the interface to send and receive packets to other devices on the network. Accordingly, interfaces without an IP address might still be active but are not routable on a network.

Understanding this distinction is crucial for network troubleshooting and configuration, as it helps us identify whether an interface is truly functional in the context of the network or simply up but not configured for network communication.

3. Understanding the ifconfig Command

The ifconfig command is a fundamental utility in Unix-like operating systems for configuring network interfaces. It allows us to view and modify the network configuration of an interface. By utilizing  ifconfig, we can:

  • assign IP addresses to interfaces
  • enable or disable an interface
  • view detailed information about each interface, including the hardware address (MAC), IP address, and the interface status

When we execute ifconfig without any arguments, it displays the status of all network interfaces, both active and inactive:

$ ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.2  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe4e:66a4  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:4e:66:a4  txqueuelen 1000  (Ethernet)
        RX packets 10043  bytes 1048592 (1.0 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 9012  bytes 1203954 (1.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 200  bytes 16000 (16.0 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 200  bytes 16000 (16.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

eth1: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 08:00:27:53:8b:dc  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

As the output shows, this command outputs detailed information about each interface, regardless of its status. Here, we see information for three interfaces: eth0, lo, and eth1. However, only eth0 and lo are active, as indicated by the UP flag.

While this provides comprehensive information, the output can be overwhelming, especially in systems with numerous interfaces. Focusing on active interfaces only can streamline the troubleshooting process and make the output more relevant to immediate needs.

4. Filtering Output to Show Only Active Interfaces

In this section, we’ll explore different methods to explain how to show the active interfaces.

4.1. Using grep

To focus on active interfaces, we can leverage the grep command. The idea is to filter the output of ifconfig to display only those interfaces with the UP flag:

$ ifconfig | grep -B1 "UP"
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

By analyzing the output, we can detect that the grep -B1 “UP” command searches for lines containing the string “UP” and includes the line before each match. Hence, this effectively shows the interface name and its status.

4.2. Using awk for Advanced Filtering

We can use the awk command for more sophisticated output formatting. For instance, the following command displays only the names of active interfaces:

$ ifconfig | awk '/flags=/{if ($2 ~ /UP/) print $1}'
eth0:

Let’s explain the command above:

  • /flags=/ searches for lines containing the string “*flags=*“
  • if ($2 ~ /UP/) checks if the second field, which includes the interface flags contains “UP
  • print $1 outputs the first field, which is the interface name

This succinctly lists the active interfaces, which in this case is only eth0.

4.3. Using a Script to Display Active Interfaces

To automate the process of displaying active interfaces, we can create a simple Bash script. Moreover, this script can be run periodically or incorporated into larger automation tools to continuously monitor active interfaces.

Let’s check out the example below:

#!/bin/bash
echo "Active Network Interfaces:"
ifconfig | awk '/flags=/{if ($2 ~ /UP/) print $1}' | while read -r interface; do
    echo "$interface"
done

In the first line, we added a shebang, indicating that the script should be run using the Bash shell. Next, we used the echo command to print a header for the output. Afterward, the ifconfig command is piped to awk, which filters and prints the names of active interfaces.

Next, the loop reads each active interface name and prints it. The loop is useful if additional processing is needed within the script. Finally, we use the echo command to print out the interface name in the output by instructing the shell to print the variable named interface.

Next, let’s attempt to run the script:

$ chmod +x show_active_interfaces.sh
$ ./show_active_interfaces.sh
eth0

To run the script, we saved it to a file, for example, show_active_interfaces.sh, and then we executed it. Upon execution, the script will output the names of active interfaces, making it easier to quickly identify which interfaces are currently operational.

4.4. Exploring the netstat Command

Another tool that we can utilize alongside ifconfig is netstat. While ifconfig provides detailed information about interfaces, netstat focuses on network connections, routing tables, and interface statistics. It’s particularly useful for identifying active network connections and listening ports.

Furthermore, netstat displays network-related information, such as active TCP/UDP connections, routing tables, and interface statistics. Unlike ifconfig, netstat provides insights into the current network activity rather than just the interface status.

To view the active network interfaces using netstat, we can use the following command:

$ netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500   10043   0      0      0      9012   0      0      0      BMRU
lo         65536  200     0      0      0      200    0      0      0      LRU

Next, let’s understand the output of this command:

  • The Iface column lists the interfaces
  • RX-OK and TX-OK indicate the number of successfully received and transmitted packets, respectively
  • The Flg column lists flags, such as BMRU for eth0 and LRU for lo, indicating the interface status (for example, Broadcast, Multicast, Running, and Up)

While ifconfig is more focused on interface configurations, netstat offers a broader view of network activity. For instance, netstat can reveal if an interface is actively handling network traffic, which complements the information provided by ifconfig.

In addition, netstat can be particularly valuable when troubleshooting connectivity issues or monitoring network performance, as it shows real-time statistics about packet transmission and reception. Hence, it’s a powerful tool to use in conjunction with ifconfig for comprehensive network monitoring and management.

5. Conclusion

In this article, we’ve explored various methods to display active interfaces, ranging from simple command-line filters with grep and awk to more sophisticated scripts that automate this task. Additionally, we’ve examined how the netstat command can provide complementary insights into network activity.

These not only simplify the management process but also help pinpoint issues related to network connectivity.