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
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the |
|Conventions|| # - requires given linux commands to be executed with root privileges either directly as a root user or by use of |
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
grep thus to avoid any need for additional software installation. Take a look at the methods below to see how.
- Let's start by testing our
eth0network interface for a physical cable connection in a low-level and Linux distro-agnostic way:
# cat /sys/class/net/eth0/carrier 1The number 1 in the above output means that the network cable is physically connected to your network card's slot.
- Next, we will test a second network interface
# cat /sys/class/net/eth1/carrier cat: /sys/class/net/eth1/carrier: Invalid argumentThe above command's output most likely means the the
eth1network interface is in powered down state. This can be confirmed by the following linux command:
# cat /sys/class/net/eth1/operstate downThe 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 upAt this stage we can again check for a network card physical cable connection:
# cat /sys/class/net/eth1/carrier 0
- 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 eth0 eth1 lo wlan0Using 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 eth0:1 eth1:0 lo:1 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:
$ sudo apt install ethtool
$ sudo dnf install ethtool
$ 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.
- To check a single network card for a cable connection use the following command. As an example, let's check the
# ethtool eth1 | grep Link\ d Link detected: no
- 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: noThe 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
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.