Home Command line utilities How To Find Out The Connected State Of A Network Cable In Linux
Find Out The Connected State Of A Network Cable In Linux

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

By sk
1749 Views

In this brief tutorial, we are going to learn 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 could be plenty of ways to do this. However, I find the following methods are way easier than others. If you ever wondered how to find out the connected state of a network cable, here is how you can do it on Unix-like operating systems.

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
Find Out The Connected State Of A Network Cable

Find Out The Connected State Of A Network Cable

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. For more details to know the list of available network interfaces, check the following guide.

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.

Method 3:

Another good method suggested by one of our readers is using "ip" command.

$ ip a

Sample output would be:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 inet 127.0.0.1/8 scope host lo
 valid_lft forever preferred_lft forever
 inet6 ::1/128 scope host 
 valid_lft forever preferred_lft forever
2: enp5s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
 link/ether 24:b6:fd:37:8b:29 brd ff:ff:ff:ff:ff:ff
3: wlp9s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
 link/ether c0:18:85:50:47:4f brd ff:ff:ff:ff:ff:ff
 inet 192.168.43.192/24 brd 192.168.43.255 scope global wlp9s0
 valid_lft forever preferred_lft forever
 inet6 fe80::c218:85ff:fe50:474f/64 scope link 
 valid_lft forever preferred_lft forever

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. I will check and update this list accordingly.

More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You May Also Like

3 comments

helloworld September 5, 2016 - 3:03 pm

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

Reply
TheRealTachyon September 7, 2016 - 3:35 am

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)

Reply
Gurudatta N.R December 28, 2016 - 5:48 am

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

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More