How to detect whether a physical cable is connected to network card slot on Linux

If you’ve ever needed to know whether a physical cable is connected to a network port on your Linux system, you don’t necessarily need to be right in front of the computer or server to look and see. There are several methods we can use from the Linux command line in order to see if a cable is plugged into a network slot.

There are a few reasons why this could come in handy. For one, it shows you whether the system itself detects that there’s a cable plugged in. This could be an essential troubleshooting step if you know for a fact that the cable is properly plugged in, yet the system is not detecting it. It’s also helpful on remote systems or if you’re just too lazy to look at the back of the computer and see if the cable is plugged in.

Check out some of the examples below where we go over various commands that check whether a physical network cable is plugged in or not.

In this tutorial you will learn:

  • How to detect physical network cable connectivity with Bash commands and ethtool

Various commands used to detect a connected network cable on Linux

Various commands used to detect a connected network cable on Linux

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software ethtool
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Detect if a physical cable is connected

Various tools can be used to detected a physical cable carrier state. However, the easiest accomplish this task is by using basic native tools like cat or grep thus to avoid any need for additional software installation. Take a look at the methods below to see how.

  1. Let’s start by testing our eth0 network interface for a physical cable connection in a low-level and Linux distro-agnostic way:
    # cat /sys/class/net/eth0/carrier 

    The number 1 in the above output means that the network cable is physically connected to your network card’s slot.

  2. Next, we will test a second network interface eth1:
    # cat /sys/class/net/eth1/carrier 
    cat: /sys/class/net/eth1/carrier: Invalid argument

    The above command’s output most likely means the the eth1 network interface is in powered down state. This can be confirmed by the following linux command:

    # cat /sys/class/net/eth1/operstate 

    The network cable can be connected but there is no way to tell at the moment. Before we can check for a physical cable connection we need to put the interface up:

    # ip link set dev eth1 up

    At this stage we can again check for a network card physical cable connection:

    # cat /sys/class/net/eth1/carrier 

  3. Based on the above output we can say that a physical cable is disconnected from the network card’s slot. Let’s let’s see briefly how we can automate the above procedure to check multiple network interfaces at once. The below command will list all available network interfaces on your Linux system:
    # for i in $( ls /sys/class/net ); do echo $i; done

    Using a bash for loop we can now check whether a network cable is connected for all network interfaces at once:

    # for i in $( ls /sys/class/net ); do echo -n $i: ; cat /sys/class/net/$i/carrier; done
    wlan0:cat: /sys/class/net/wlan0/carrier: Invalid argument

Test for physical cable connection with ethtool

Now, if you really want to get fancy you can do the above task using the ethtool command. Here’s how to install the software on major Linux distributions:

To install ethtool on Ubuntu, Debian, and Linux Mint:

$ sudo apt install ethtool

To install ethtool on CentOS, Fedora, AlmaLinux, and Red Hat:

$ sudo dnf install ethtool

To install ethtool on Arch Linux and Manjaro:

$ sudo pacman -S ethtool

Now that it’s installed, you can use one or more of the following commands below to test the network connection of a physical cable.

  1. To check a single network card for a cable connection use the following command. As an example, let’s check the eth1 interface:
    #  ethtool eth1 | grep Link\ d
    	Link detected: no
  2. Or we can use bash for loop again to check all network interfaces it once:
    # for i in $( ls /sys/class/net ); do echo -n $i; ethtool $i | grep Link\ d; done
    eth0	Link detected: yes
    eth1	Link detected: no
    lo	Link detected: yes
    wlan0	Link detected: no

    The only problem with the above ethtool output is that it will not detect a connected cable if your network interface is down. Consider the following example:

    # ethtool eth0 | grep Link\ d
            Link detected: yes
    # ip link set dev eth0 down
    # ethtool eth0 | grep Link\ d
            Link detected: no

Closing Thoughts

In this guide, we saw how to detect whether a physical cable is connected to a network card slot on Linux. This is handy to check the connections on a remote machine or just as a troubleshooting step to see if your system is detecting a physical cable or not. If you have a cable plugged in but your system is not detecting it, it could mean you’re missing a network driver or have a faulty network card altogether.

Comments and Discussions
Linux Forum