How to check network card information on Linux

Sometimes for a variety of reasons, you may need to configure additional network interfaces.

By default, you configure the primary network interface when you set up the server. This is part of everyone's build effort. Sometimes for a variety of reasons, you may need to configure additional network interfaces.

This can be provided through network binding / teaming, or it can be a separate interface for application requirements or backup.

To do this, you need to know how many interfaces computers have and how fast they are configured.

There are many commands to check for available network interfaces, but we only use ip commands. In the future, we will write another article to introduce all these tools.

In this tutorial, we will show you available network card (NIC) information, such as interface name, associated IP address, MAC address, and interface speed.

What is ip command

ip command Similar to ifconfig, it is used to assign static IP addresses, routes, default gateways, and so on.

# ip a

1: lo: mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether fa:16:3e:a0:7d:5a brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.101/24 brd 192.168.1.101 scope global eth0
    inet6 fe80::f816:3eff:fea0:7d5a/64 scope link
       valid_lft forever preferred_lft forever

What is the ethtool command

ethtool is used to query or control network drivers or hardware settings.

# ethtool eth0

1) How to check the available network interface with ip command on Linux

When you run an ip command without any parameters, it provides a lot of information, but if you only need the available network interface, use the following custom ip command.

# ip a |awk '/state UP/{print $2}'

eth0:
eth1:

2) How to use the IP command to check the IP address of the network interface on Linux

If you only want to see which interface the IP address is assigned to, use the following custom IP command.

# ip -o a show | cut -d ' ' -f 2,7
//or
ip a |grep -i inet | awk '{print $7, $2}'

lo 127.0.0.1/8
192.168.1.101/24
192.168.1.102/24

3) How to check MAC address of network card with ip command on Linux

If you want to view only the network interface name and the corresponding MAC address, use the following format.

Check MAC address of specific network interface:

# ip link show dev eth0 |awk '/link/{print $2}'
00:00:00:55:43:5c

Check the MAC address of all network interfaces and create the script:

# vi /opt/scripts/mac-addresses.sh

#!/bin/sh
ip a |awk '/state UP/{print $2}' | sed 's/://' | while read output;
do
  echo $output:
  ethtool -P $output
done

Run the script to obtain MAC addresses of multiple network interfaces:

# sh /opt/scripts/mac-addresses.sh

eth0:
Permanent address: 00:00:00:55:43:5c
eth1:
Permanent address: 00:00:00:55:43:5d

4) How to use ethtool command to check network interface speed on Linux

If you want to check the network interface speed on Linux, use the ethtool command.

Check the speed of a specific network interface:

# ethtool eth0 |grep "Speed:"

Speed: 10000Mb/s

Check all network interface speeds and create the script:

# vi /opt/scripts/port-speed.sh

#!/bin/sh
ip a |awk '/state UP/{print $2}' | sed 's/://' | while read output;
do
  echo $output:
  ethtool $output |grep "Speed:"
done

Run the script to get multiple network interface speeds:

# sh /opt/scripts/port-speed.sh

eth0:
Speed: 10000Mb/s
eth1:
Speed: 10000Mb/s

5) Shell script to verify network card information

Through this shell script, you can collect all the above information, such as the name of the network interface, the IP address of the network interface, the MAC address of the network interface, and the speed of the network interface. Create the script:

# vi /opt/scripts/nic-info.sh

#!/bin/sh
hostname
echo "-------------"
for iname in $(ip a |awk '/state UP/{print $2}')
do
  echo "$iname"
  ip a | grep -A2 $iname | awk '/inet/{print $2}'
  ip a | grep -A2 $iname | awk '/link/{print $2}'
  ethtool $iname |grep "Speed:"
done

Run the script to check the network card information:

# sh /opt/scripts/nic-info.sh

vps.2daygeek.com
----------------
eth0:
192.168.1.101/24
00:00:00:55:43:5c
Speed: 10000Mb/s
eth1:
192.168.1.102/24
00:00:00:55:43:5d
Speed: 10000Mb/s

via: https://www.2daygeek.com/linux-unix-check-network-interfaces-names-nic-speed-ip-mac-address/

Author: Magesh Maruthamuthu Topics: lujun9972 translator: geekpi Checked by: wxy

This paper is composed of LCTT Original compilation, Linux China Honor launch

Tags: network Mac Linux shell

Posted on Wed, 29 Apr 2020 17:51:17 -0700 by Alpha_One