Linux - How to detect physical cable is connected to network card or not, Find physical cable connection to Network slot

How to detect physical cable is connected to network card or not

There are various tools to find the network cable link of network card connected to a server, however in linux all are files and the connection status can also able be determined by simple cat command. /sys/class which say more
# ls -l /sys/class/net/

lrwxrwxrwx 1 root root 0 Aug 11 04:08 eth0 -> ../../devices/xxxxx:00/0000:00:xx.0/0000:aa:00.x/net/eth0
lrwxrwxrwx 1 root root 0 Aug 11 04:08 eth1 -> ../../devices/xxxxx:00/0000:00:yy.0/0000:bb:00.y/net/eth1
The output 1 means the connection is fine and good, if there is no connection the you will see the error an error message. Here it means that physical connection of eth0 network interface is connected.

 # cat /sys/class/net/eth0/carrier
   1

 # cat /sys/class/net/eth0/operstate
   up
Here the network interface is in powered down state
 # cat /sys/class/net/eth1/carrier 
   cat: /sys/class/net/eth1/carrier: Invalid argument

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

Before we can check for a physical cable connection we need to turn it up:
 # ifconfig eth1 up
Now lets check the connection again for a network card physical cable connection
 # cat /sys/class/net/eth1/carrier 
  0
From the above output it is clear that physical cable is disconnected from the network slot.

Command to check available network interface
 # for i in $( ls /sys/class/net ); do echo $i; done
   eth0
   eth1
   lo

Now lets check whether a network cable is connected for all network interfaces
# for i in $( ls /sys/class/net ); do echo -n $i: ; cat /sys/class/net/$i/carrier; done

  eth0:1
  eth1:0
  lo:1

Test connection using ethtool
 # for i in $( ls /sys/class/net ); do echo -n $i; ethtool $i | grep -i 'Link \d'; done
   
   eth0	   Link detected: yes
   eth1	   Link detected: no
   lo	   Link detected: yes
Using ethtool to test connection
 # ethtool eth0 | grep -i 'Link\ d'
        Link detected: yes

 # ifconfig eth0 down

 # ethtool eth0 | grep -i 'Link\ d'
        Link detected: no

 # ifconfig eth0 up

 # ethtool eth0 | grep -i 'Link\ d'
        Link detected: yes

The topic on Linux - How to detect physical cable is connected to network card or not is posted by - Ramesh

Hope you have enjoyed, Linux - How to detect physical cable is connected to network card or notThanks for your time

Tech Bluff