1. Overview

Let’s imagine that we want to query the status of the Ethernet port of a system. In this tutorial, we’ll discuss three ways to discover its physical state. One is exploring the filesystem directories, which we can readily do. The second is the ip link command, which is the replacement of the old ifconfig. Finally, the ethtool utility also provides the information (although it may not be installed by default).

2. Directory Exploration

Everything is a file in Linux, from system information to hardware devices. The directory /sys/class/net contains information about the network devices connected to the system.

2.1. Available Outputs

Let’s assume that the network name follows the new Predictable Network Interfaces Names network naming scheme. We’ll look for the status of the network card named enp0s25 (instead of the old convention name eth0).

There are two files related to the connection status of a cable. We’ll use the cat command to see their content. We’ll consider the two possible states of the physical connector: plugged in or unplugged.

The first file is the operstate, which displays if the network is capable of sending or receiving packages. In a wired connection, this is equivalent to a connected cable. In that case, the file contains up:

$ cat /sys/class/net/enp0s25/operstate

When we have the cable disconnected, the file contains down:

$ cat /sys/class/net/enp0s25/operstate

The second file is the carrier, which we may encounter more often than operstate but works the same. If we have the cable connected, the output shows 1:

$ cat /sys/class/net/enp0s25/carrier

However, if the cable is disconnected, we get 0:

$ cat /sys/class/net/enp0s25/carrier

2.2. Invalid argument Error

The carrier file is empty if the network device is down. The network card can be down, for example, due to power-saving management.

Therefore, when checking the content of the argument carrier, we’ll get an error:

$ cat /sys/class/net/enp0s25/carrier
cat: /sys/class/net/enp0s25/carrier: Invalid argument

The attribute operstate shows that the network is down:

$ cat /sys/class/net/enp0s25/operstate

We can activate (set up) the network device with the ip link command and the interface name:

$ sudo ip link set enp0s25 up

Now, the carrier argument also displays the (disconnected) status:

$ cat /sys/class/net/enp0s25/carrier

2.3. Get More Network Properties at Once

When we want to retrieve not only the carrier, but also other attributes like operstate, we can use the grep command:

$ grep "" /sys/class/net/enp0s25/* -sH

This returns a nicely formatted list with the different pairs of key:value. They include the carrier, the operstate, and many other useful pieces of information such as the MAC address.

The ip link command can also show the status of network devices. When we use the show argument with the network device name, we get:

$ ip link show enp0s25
2: enp0s25: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN mode DEFAULT group default qlen 1000
link/ether ff:ff:ff:ff:ff:ff brd ff:ff:ff:ff:ff:ff

From that output, the relevant part is the NO-CARRIER flag, which shows that there is no cable connected to the port.

However, when we have one cable connected, the output looks like this:

$ ip link show enp0s25
2: enp0s25: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether ff:ff:ff:ff:ff:ff brd ff:ff:ff:ff:ff:ff

The flag LOWER_UP shows the physical layer information — specifically, it tells us whether an Ethernet cable is connected to the port.

4. ethtool Command

The ethtool may not be available in the device that needs to be diagnosed. If we install it, we can use it to return many parameters that provide more information about the network.

We need to use sudo to call the command to retrieve all the outputs (otherwise, we won’t get the wake-on attribute):

$ sudo ethtool enp0s25
Settings for enp0s25:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Supported pause frame use: No
Supports auto-negotiation: Yes
Supported FEC modes: Not reported
Advertised link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
Advertised pause frame use: No
Advertised auto-negotiation: Yes
Advertised FEC modes: Not reported
Speed: Unknown!
Duplex: Unknown! (255)
Auto-negotiation: on
Port: Twisted Pair
Transceiver: internal
MDI-X: Unknown (auto)
Supports Wake-on: pumbg
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: no

The tool returns many flags and their status. We have to look for the Link detected flag to see if there is a physical connection. We can use the grep command to get it:

$ sudo ethtool enp0s25 | grep "Link detected"
Link detected: no

If there’s a cable connected to the port, the output will show it:

$ sudo ethtool enp0s25 | grep "Link detected"
Link detected: yes

5. Conclusion

In this article, we’ve discussed three ways to know the state of a network connector. The first one uses the filesystem of /sys and different files that refer to the network devices. The second uses the ip link tool, and the third one uses the ethtool command to retrieve the information.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.