How to check if a network adapter is present in Linux

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to check if a network adapter is present in your Linux system. We will explore three common methods to achieve this.

First, you will use the lshw command with the -C network option to list detailed information about your network hardware. Next, you will verify the presence of network interfaces by inspecting the contents of the /sys/class/net directory. Finally, you will utilize the lspci command to list PCI devices and filter for network controllers, providing another way to identify your network adapters. By completing these steps, you will gain practical skills in diagnosing network hardware presence on a Linux system.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/SystemInformationandMonitoringGroup(["System Information and Monitoring"]) linux(("Linux")) -.-> linux/RemoteAccessandNetworkingGroup(["Remote Access and Networking"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/FileandDirectoryManagementGroup -.-> linux/whereis("File/Command Finding") linux/SystemInformationandMonitoringGroup -.-> linux/ps("Process Displaying") linux/RemoteAccessandNetworkingGroup -.-> linux/ifconfig("Network Configuring") linux/RemoteAccessandNetworkingGroup -.-> linux/netstat("Network Monitoring") linux/RemoteAccessandNetworkingGroup -.-> linux/ip("IP Managing") subgraph Lab Skills linux/ls -.-> lab-558732{{"How to check if a network adapter is present in Linux"}} linux/whereis -.-> lab-558732{{"How to check if a network adapter is present in Linux"}} linux/ps -.-> lab-558732{{"How to check if a network adapter is present in Linux"}} linux/ifconfig -.-> lab-558732{{"How to check if a network adapter is present in Linux"}} linux/netstat -.-> lab-558732{{"How to check if a network adapter is present in Linux"}} linux/ip -.-> lab-558732{{"How to check if a network adapter is present in Linux"}} end

Check adapters with lshw -C network

In this step, we will explore how to identify the network adapters connected to your system using the lshw command. lshw stands for "list hardware" and is a powerful tool for getting detailed information about your system's hardware configuration.

To specifically list network adapters, we use the -C option followed by the class name, which is network.

Open your terminal if it's not already open. You can do this by clicking the Xfce Terminal icon on the left side of your desktop.

Now, type the following command and press Enter:

sudo lshw -C network

Let's break down this command:

  • sudo: As we learned before, this allows us to run the command with superuser privileges, which is often required to access detailed hardware information.
  • lshw: The command to list hardware.
  • -C network: This option filters the output to show only devices belonging to the "network" class.

You will see output similar to this (the exact details will vary depending on the virtual machine's configuration):

  *-network
       description: Ethernet interface
       product: Ethernet controller
       vendor: Red Hat, Inc.
       physical id: 3
       bus info: pci@0000:00:03.0
       logical name: eth0
       version: 00
       serial: 52:54:00:xx:xx:xx
       size: 1Gbit/s
       capacity: 10Gbit/s
       width: 32 bits
       clock: 66MHz
       capabilities: pm bus_master cap_list ethernet physical tp mii 10bt 10bt-fd 100bt 100bt-fd 1000bt-fd autoneg negotiated
       configuration: autonegotiation=on broadcast=yes driver=virtio_net driverversion=1.0.0 ip=172.18.0.x latency=0 link=yes multicast=yes port=twisted pair speed=1Gbit/s
       resources: irq:24 ioport:c040(size=32) memory:fe000000-fe000fff

This output provides a lot of information about your network adapter, including:

  • description: What the device is (Ethernet interface).
  • product and vendor: The manufacturer and model of the hardware.
  • logical name: The name the operating system uses for this interface (e.g., eth0). This is important for configuring network settings.
  • serial: The MAC address of the network adapter.
  • size and capacity: The current speed and maximum capability of the connection.
  • configuration: Details about how the interface is currently set up, including its IP address if assigned.

Understanding this output helps you identify your network interfaces and troubleshoot network issues.

Click Continue to proceed to the next step.

Verify interfaces in /sys/class/net

In Linux, the /sys filesystem provides a view into the kernel's device model. It's a virtual filesystem, meaning the files and directories within it are not stored on your disk in the traditional sense, but are generated by the kernel to represent system information.

Specifically, /sys/class/net is a directory that contains symbolic links to all the network interfaces detected by the system. The names of these links correspond to the logical names of the network interfaces, like eth0 that we saw in the previous step.

Let's list the contents of this directory to see the network interfaces. We'll use the ls command, which is used to list files and directories.

Type the following command in your terminal and press Enter:

ls /sys/class/net/

You should see output similar to this:

eth0  lo
  • eth0: This is the Ethernet interface we identified using lshw in the previous step.
  • lo: This stands for "loopback" interface. It's a special virtual interface that your computer uses to communicate with itself. It's essential for networking software to function correctly, even when your computer isn't connected to a physical network.

The presence of eth0 and lo in this directory confirms that the system recognizes these network interfaces. This directory is a quick way to see which network interfaces are available on your system.

Click Continue to move on.

Inspect hardware with lspci

In this step, we will use the lspci command to list information about PCI devices connected to your system. PCI (Peripheral Component Interconnect) is a standard bus that connects hardware devices to the computer's motherboard. Many devices, including network cards, graphics cards, and storage controllers, are connected via PCI or its successor, PCI Express (PCIe).

The lspci command is useful for quickly identifying connected hardware and their basic information.

Type the following command in your terminal and press Enter:

lspci

You will see a list of PCI devices. The output will look something like this:

00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE controller: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:01.2 USB controller: Intel Corporation 82371AB/EB/MB PIIX4 USB (rev 01)
00:01.3 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 03)
00:02.0 VGA compatible controller: Red Hat, Inc. QXL Paravirtual graphic driver
00:03.0 Ethernet controller: Red Hat, Inc. Virtio network device
00:04.0 Communication controller: Red Hat, Inc. Virtio console
00:05.0 SCSI storage controller: Red Hat, Inc. Virtio SCSI
00:06.0 Unclassified device [0000]: Red Hat, Inc. Virtio balloon
00:07.0 Audio device: Intel Corporation 82801AA AC'97 Audio Controller (rev 01)

Each line represents a PCI device. The format is typically Bus:Device.Function Class: Vendor Product.

  • Bus:Device.Function: Identifies the location of the device on the PCI bus.
  • Class: Describes the type of device (e.g., Host bridge, Ethernet controller, VGA compatible controller).
  • Vendor and Product: Identify the manufacturer and the specific model of the device.

Notice the line that says Ethernet controller: Red Hat, Inc. Virtio network device. This corresponds to the network adapter we saw with lshw and in /sys/class/net. lspci gives you a different perspective on the hardware, focusing on the PCI bus.

You can use lspci with options like -v for more verbose output or -nn to show vendor and device IDs in a numerical format, which is useful for looking up hardware information online.

For example, try:

lspci -v

This will provide much more detailed information about each device. Press q to exit the verbose output when you are done viewing it.

Click Continue to finish this lab.

Summary

In this lab, we learned how to check for the presence of network adapters in Linux using various command-line tools. We started by using the sudo lshw -C network command to list detailed information about network interfaces, including their description, vendor, product, and configuration details. This command provides a comprehensive overview of the hardware.

Next, we explored the /sys/class/net directory, which provides a symbolic link for each network interface present on the system, offering a quick way to verify their existence. Finally, we utilized the lspci command to inspect the PCI bus and identify network controllers by filtering the output for "Ethernet controller" or similar descriptions, providing another method to confirm the presence of network hardware. These steps collectively demonstrate different approaches to identifying network adapters in a Linux environment.