Home Linux Networking How To Find The Connected State Of A Network Cable In Linux From Commandline

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

By sk
Published: Updated: 14K views

In this brief tutorial, we are going to learn how to find 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 easily and quickly find the connected state of a network cable in Linux without leaving desk.

Find the connected state of a network cable in Linux

There could be plenty of ways to do find the state of a network cable connectivity. Here, I have listed three easy ways to check if the cable is connected or not.

Method 1

First, you need to know your network card's name in-order to find the connectivity state. You can find out the name of your network card using "ifconfig" or "ip addr" commands.

To view the list of available network interfaces, check the following guide.

After finding the network interface, you can find the connected state of a network cable in Linux, just run:

$ cat /sys/class/net/enp5s0/carrier

Replace "enp5s0" in the above command with your own network card name.

Sample output:

1

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

You can also check if the cable is connected to the network card using the following command too:

$ cat /sys/class/net/enp5s0/operstate

Sample output:

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

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
Find the state of a wireless network card
Find the state of a wireless network card

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 - Using ethtool

In case the above commands doesn't help, there is another tool called "ethtool" to help you.

The 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
Find the connected state of a network cable using ethtool
Find the connected state of a network cable using ethtool

To find out the state of wireless network card:

$ sudo ethtool wlp9s0

Sample output:

Settings for wlp9s0:
 Link detected: yes
Check the state of wireless network card
Check the state of wireless network card

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
View the connected state of a network card in Linux
View the connected state of a network card in Linux

Download your free EBook - Free eBook: "Network and Computer Systems Administrators - Occupational Outlook"

Method 3 - Using ip Command

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 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.

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. By using this site, we will assume that you're OK with it. Accept Read More