How To Find Out The Connected State Of A Network Cable In Linux

Find Out The Connected State Of A Network Cable In Linux
Image source: Google Images

In this brief tutorial, we are going to see how to find out whether the network cable is connected or not with the Network interface card. You can directly go and check the physical network slot on the back of your system to find if the cable is connected or not, but it is not necessary though. We can do this using simple commands from where you are. There might be plenty of ways to do this. However, I find the following methods are way easier than others. If you ever wondered how to do this, please follow me.

Find out the connected state of a network cable in Linux

Method 1

To find out the connected state of a network cable in Linux, just run:

cat /sys/class/net/enp5s0/carrier

Sample output:

1

If you got output as “1” (Number one), It means that the network cable is connected with the network card.

Also, you can do this with the following command too:

cat /sys/class/net/enp5s0/operstate

Sample output:

up

As you in the above outputs, the network cable is connected with the Lan adapter. Here, “enp5s0” is my network card name. You can find out the name of your network card using “ifconfig” or “ip addr” commands.

Not only wired network card, we can also find the state of a wireless network card too.

To do so, run:

cat /sys/class/net/wlp9s0/carrier

Sample output:

1

Or,

cat /sys/class/net/wlp9s0/operstate

Sample output:

up

Now, just disconnect the network cable and check what would be the output.

cat /sys/class/net/enp5s0/carrier

Sample output:

0

Or,

cat /sys/class/net/enp5s0/operstate

Sample output:

down

See? We got results as “0” (zero) and “down”, which means that the cable is not connected. It doesn’t matter whether you configured the IP address to the network card or not. You can easily find out if the cable is connected or not using the above commands.

Method 2:

In case the above commands doesn’t help, there is another tool called “ethtool” to help you. ethtool is used to query and control network device driver and hardware settings, particularly for wired Ethernet devices.

Run the following command to find out the connected state of a network cable:

sudo ethtool enp5s0

Sample output:

Settings for enp5s0:
 Supported ports: [ TP MII ]
 Supported link modes: 10baseT/Half 10baseT/Full 
 100baseT/Half 100baseT/Full 
 Supported pause frame use: No
 Supports auto-negotiation: Yes
 Advertised link modes: 10baseT/Half 10baseT/Full 
 100baseT/Half 100baseT/Full 
 Advertised pause frame use: Symmetric Receive-only
 Advertised auto-negotiation: Yes
 Link partner advertised link modes: 10baseT/Half 10baseT/Full 
 100baseT/Half 100baseT/Full 
 Link partner advertised pause frame use: Transmit-only
 Link partner advertised auto-negotiation: Yes
 Speed: 100Mb/s
 Duplex: Full
 Port: MII
 PHYAD: 0
 Transceiver: internal
 Auto-negotiation: on
 Supports Wake-on: pumbg
 Wake-on: g
 Current message level: 0x00000033 (51)
 drv probe ifdown ifup
 Link detected: yes

To find out the state of wireless network card:

sudo ethtool wlp9s0

Sample output:

Settings for wlp9s0:
 Link detected: yes

Let us disconnect the cable, and see what happens.

sudo ethtool enp5s0

Sample output:

Settings for enp5s0:
 Supported ports: [ TP MII ]
 Supported link modes: 10baseT/Half 10baseT/Full 
 100baseT/Half 100baseT/Full 
 Supported pause frame use: No
 Supports auto-negotiation: Yes
 Advertised link modes: 10baseT/Half 10baseT/Full 
 100baseT/Half 100baseT/Full 
 Advertised pause frame use: Symmetric Receive-only
 Advertised auto-negotiation: Yes
 Link partner advertised link modes: 10baseT/Half 10baseT/Full 
 100baseT/Half 100baseT/Full 
 Link partner advertised pause frame use: Transmit-only
 Link partner advertised auto-negotiation: Yes
 Speed: 100Mb/s
 Duplex: Full
 Port: MII
 PHYAD: 0
 Transceiver: internal
 Auto-negotiation: on
 Supports Wake-on: pumbg
 Wake-on: g
 Current message level: 0x00000033 (51)
 drv probe ifdown ifup
 Link detected: no

As you see in the above output, the network cable is not plugged in.

That’s all for now. Got any other methods in mind to find out the connected state of a network cable? Feel free to let us know in the comment section below. If you find this guide useful, please share it on your social networks to support us.

I will be here with another interesting article soon. Until then, Stay tuned with OSTechNix.

Cheers!

Thanks for stopping by!

How can I benefit from this blog:

Have a Good day!!

You may also like...

  • helloworld

    ip a
    eth0: mtu 1500 qdisc mq state UP group default qlen 1000
    eth1: mtu 1500 qdisc noop state DOWN group default qlen 1000

  • TheRealTachyon

    Since not everyone has yet been plagued with yet another ridiculous systemd related change in long standing conventions, you should mention that the odd looking network device names in your examples are only for Linux distributions that have migrated to systemd’s “Predictable Network Interface naming” standard which throws out the “eth0” and “wlan0” type device names we’ve been using everywhere for decades.

    Another way to check for cable connection is, (from a root shell):
    ifconfig
    Look for “UP” in the output after the device you want to check (eg eth0)

  • Gurudatta N.R

    How to check the network cable is connected/patched in Linux

    You can test Link by it by running mii-tool

    Example

    # mii-tool
    eth0: negotiated 100baseTx-FD flow-control, link ok
    eth1: autonegotiation restarted, no link
    eth2: autonegotiation restarted, no link
    eth3: autonegotiation restarted, no link

    How to check the network cable is connected or not in the linux

    for cable in `ls /sys/class/net | grep ^eth`; do printf “$cable: “; cat /sys/class/net/$cable/carrier; done

    if 0(blank) = it would mean cables not connected or unplugged
    if 1 = cable connected, plugged

    more eth0/flags; more eth1/flags; more eth2/flags ; more eth3/flags
    0x1003 << connected
    0x1002
    0x1002
    0x1002