Key Takeaways
There are multiple commands to list devices in Linux, each with variations in content and detail, catering to different use cases and preferences.
Many Linux distributions already include most of these commands, but certain installations may necessitate the use of additional commands such as procinf, lsscsi, hwinfo, lshw, and hdparm.
To install hwinfo, execute "sudo apt install hwinfo" on Ubuntu or "sudo dnf install hwinfo" on Fedora. After installation, run "hwinfo --short" to obtain a brief inventory of devices.
Find out exactly what devices are inside your Linux computer or connected to it. We'll cover 12 commands for listing your connected devices.
Why 12 Commands to List Devices?
There are numerous methods to identify the devices connected to or integrated within your Linux computer. We will present 12 of them, although there are countless others.
The differences in content and detail make them unique enough for individuals to have their preferences regarding the methods. The output format of one command may be well-suited for a specific purpose, while another command's format may be ideal for pipe-processing with grep or other methods.
However, the main objective is to ensure the article caters to a wide audience. Instead of selecting specific commands that may interest or benefit our readers, we aim to offer a comprehensive selection of available commands, allowing our readers to choose which ones they find useful and disregard the rest.
Installation Required
The majority of these commands come pre-installed in your Linux distribution. Ubuntu, Fedora, and Manjaro have been selected as representative examples from the main branches of the Debian, Red Hat, and Arch families.
All three distributions needed to install procinfo , which provides the lsdev command. The lsscsi command also needed to be installed on all three.
To install lsdev and lsscsi, use these commands.
On Ubuntu:
sudo apt-get install procinf
sudo apt-get install lsscsi
On Fedora:
sudo dnf install procinfo
sudo dnf install lsscsi
On Manjaro:
sudo pacman -Syu procinfo
sudo pacman -Syu lsscsi
Surprisingly, Manjaro — famous for being a bare-bones type of distribution — was the distribution that had most of the commands we're going to look at pre-installed.
Ubuntu and Fedora needed hwinfo installing, and Fedora also required lshw and hdparm installing.
On Ubuntu:
sudo apt-get install hwinfo
On Fedora:
sudo dnf install hwinfo
sudo dnf install lshw
sudo dnf install hdparm
1. The mount Command
mount
The output of the mount command can sometimes be longer than anticipated, particularly when the snap method has been used for software installation. Snap creates additional pseudo-filesystems that are displayed in the mount output. It is important to note that these pseudo-filesystems do not have any physical devices associated with them, thus only serving to obscure the actual information.
If you come across a genuine filesystem in the directory displayed on a hard drive, we can use grep to isolate it.
Hard drives are typically identified by a name that begins with "sd", followed by a letter denoting the drive's order, starting from "a" for the first drive, "b" for the second drive, and so forth. Partitions can be identified by adding a numeric suffix: "1" for the first partition, "2" for the second partition, and so on.
The initial hard drive is referred to as sda, and its initial partition is named sda1. Hard drives are connected via distinct device files (known as block files) in the /dev directory, and subsequently mounted in various locations within the filesystem tree.
This grep command is employed to extract the specific information related to any drive starting with "sd".
mount | grep /dev/sd
The output contains the single hard drive in the machine that was used to research this article.
The mount response reveals that the drive /dev/sda is currently mounted at the root (/) of the filesystem. It utilizes an ext4 filesystem and is in read-write mode.
Relatime is the chosen scheme employed by the file timestamp updating routines. It optimizes disk updates by only writing the access time to the disk if the modified time (mtime) or change time (ctime) of a file is newer than the last access time, or if the access time (atime) surpasses a threshold set by the system. This effectively minimizes the number of disk updates required for frequently accessed files.
The "errors=remount-ro" indicates that the filesystem will be remounted in read-only mode if there are severe errors.
To conveniently identify the mounted filesystems on devices, use the pipe command to redirect the output from mount to the less utility.
mount | less
Scroll through the output until you see filesystems that are connected to /dev special files.
2. The lsblk Command
The lsblk command lists the block devices, their mount point, and other information. Type lsblk at a command line:
lsblk
The output shows:
Name: the name of the block device
Maj:Min - The major number indicates the type of device, while the minor number represents the specific device within that type. For instance, 7:4 indicates the fourth loop device in the list.
RM - This field indicates whether the device is removable or not. A value of 0 signifies no, while a value of 1 signifies yes.
Size is the capacity of the device.
RM: Whether the device is read-only or not. 0 means no, 1 means yes.
Type: The type of the device, for example, loop, dir (directory), disk, rom (CD ROM), and so on.
Mountpoint: Where the filesystem of the device is mounted.
To de-clutter the output and remove the loop devices, we can use the -e (exclude) option and provide the number of the type of devices we wish to ignore.
This command will cause lsblk to ignore the loop (7) and cd room (11) devices.
lsblk -e 7,11
The results now only contain the hard drive sda.
3. The df Command
The df command reports on drive capacities and used and free space.
Type df on the command line and press Enter.
df
The output table shows:
Fileystem: The name of this filesystem.
1K-Blocks: The number of 1K blocks that are available on this filesystem.
Used: The number of 1K blocks that have been used on this file system.
Available: The number of 1K blocks that are unused on this file system.
Use%: The amount of space used in this file system given as a percentage.
File: The filesystem name, if specified on the command line.
Mounted on: The mount point of the filesystem.
To remove unwanted entries from the output, use the -x (exclude) option. This command will prevent the loop device entries from being listed.
df -x squashfs
The compact output is much easier to parse for the important information.
4. The fdisk Command
The fdisk command serves as a versatile tool for not only modifying the disk partition table, but also for gaining insights into the device configuration in a computer. By utilizing this functionality, we can effectively investigate the various devices present.
To list the partition tables, we will utilize the -l (list) option of fdisk. Considering the possibility of lengthy output, we will employ the pipe symbol to redirect the output from fdisk to less. However, since fdisk has the capability to modify disk partition tables, the sudo command must be used.
Please write the rewritten content.
By scrolling through less you will be able to identify the hardware devices. Here is the entry for hard drive sda. This is a physical hard drive of 10 GB.
Now that we know the identity of one of the hardware devices we can ask fdisk to report on that item alone.
sudo fdisk -l /dev/sda
We get an output of considerably reduced length.
5. The /proc Files
To access certain system information, it is possible to examine the pseudo-files located in the /proc directory. One specific file of interest is /proc/mounts, as it provides valuable details about the mounted filesystems. Simply using the "cat" command is sufficient to view the contents of this file.
cat /proc/mounts
The listing shows the special device file in /dev that is used to interface to the device and the mount point on the filesystem tree.
We can refine the listing by using grep to look for entries with /dev/sd in them. This will filter out the physical drives.
cat /proc/mounts | grep /dev/sd
This gives us a much more manageable report.
To expand our inclusiveness, we can utilize the grep command to search for devices with /dev/sd and /dev/sr special device files. By doing so, we encompass both hard drives and the CD ROM associated with this particular machine.
cat /proc/partitions | grep s[rd]
There are now two devices and one partition included in the output.
6. The lspci Command
The lspci command lists all of the PCI devices in your computer.
lspci
The information provided is:
Slot: The slot the PCi device is fitted in
Class: The class of the device.
Vendor name: The name of the manufacturer.
Device name: The name of the device.
Subsystem: Subsystem vendor name (if the device has a subsystem).
Subsystem name: If the device has a subsystem.
Revision number: The version number of the device
Programming interface: The programming interface, if the device provides one.
lsusb
This test computer has a Canon scanner attached to it as USB device 5, and an external USB drive as USB device 4. Devices 3 and 1 are internal USB interface handlers.
You can receive a more verbose listing by using the -v (verbose) option, and even more verbose version by using -vv.
8. The lsdev Command
The lsdev command displays information on all of the installed devices.
This command generates a lot of output, so we're going to pipe it through less.
lsdev | less
There are many hardware devices listed in the output.
9. The lshw Command
To obtain a list of devices connected to your computer, the lshw command is used. This command produces a significant amount of output, generating more than 260 lines of information on the test computer. To manage this output, it is recommended to utilize the less command by performing another pipe operation.
Note that you need to use sudo with lshw to get the most out of it. If you don't, it won't be able to access all devices.
sudo lshw | less
Below is the description of the CD ROM entry featuring a SCSI interface. The provided information pertaining to each device is remarkably comprehensive. Notably, lshw extracts a majority of its data from the diverse files stored in /proc.
If you want a shorter, less detailed output, you can use the --short option.
10. The lsscsi Command
As you would imagine by now, the lsscsi command lists the SCSI devices connected to your computer.
lsscsi
Here are the SCSI devices connected to this test machine.
11. The dmidecode Command
The DMI tables are decoded by the dmidecode commands, which allows for the extraction of hardware information both externally connected to the computer and inside the computer itself.
The DMI is also sometimes referred to as the SMBIOS (the System Management Basic Input/Output System) although they are really two different standards.
Again, we'll pipe this through less.
dmidecode | less
The dmidecode command can report on over 40 different hardware types.
12. The hwinfo Command
The hwinfo command is extremely verbose, making it necessary to use the piping through less. On the test computer, it produced a whopping 5850 lines of output! To mitigate this, you can begin by incorporating the --short option.
hwinfo --short
If you really need to see the finest-grained detail, repeat this and omit the --short option.
Wrap It Up
So, here's our dozen ways to investigate the devices within, or attached to, your computer.
Whatever your particular interest in hunting down this hardware may be, there'll be a method in this list that will enable you to find what you need.