Accessing Devices via Linux CLI
Accessing devices via a Linux command-line interface (CLI) can be a useful way to manage and interact with hardware components. The first step in accessing a device is identifying its name or device file, which is typically located in the /dev directory. This can be done using the lsblk or fdisk commands. Once the device file is known, it can be mounted to a specific location in the file system using the mount command. From there, the device can be accessed like any other file or directory on the system. For example, data can be copied to and from storage devices, or configuration settings can be modified for network adapters. Overall, accessing devices via the Linux CLI provides a powerful and flexible way to manage hardware components on a system.
/dev directory
One of the most significant directories on a Linux system is the /dev directory, which keeps information about the many devices connected to the system. When a device is linked to the system, it requires a device driver to operate efficiently. The device files, which are located in the /dev directory, can be used to interface with the device. These are special files, but they can be dealt with using ls, and cat in the same way as regular files can. It’s time to see /dev with the command shown below.
$ ls /dev
The high number of device files on the system may be seen. Some of the devices, such as /dev/null, have already been utilized. When you deliver output to /dev/null, the kernel understands that this device accepts all input and simply ignores it, so no output is returned. You can also find files like sda, sda2, and sda which are partitions of the hard drives.
Types of devices
Now, let’s take a deeper look into the /dev.
$ ls -l /dev
You can see lots of information is shown in the result. From left to right, the pieces of information tell about permissions, owner, group, major device number, minor device number, timestamp, and name of the device.
Character Device
One character is transferred at a time using these devices. Although these devices are not physically attached to the system, they help it to perform more effectively. As character devices, several pseudo devices are kept in the /dev/null directory. They are denoted as ‘c’ before the permissions block shown by the ls -l /dev command.
Block Device
They are also used in data transfer, but unlike character devices, they can handle large blocks of data. Hard drives and file systems use devices like these as they have to transfer a large amount of data. They are denoted as ‘b’.
Pipe device
These devices can handle several processes at the same time. These work similarly to character devices, except instead of sending output to a device, they send it to another process.
They are denoted as ‘p’.
Socket device
These devices provide similar functions to pipe devices in that they make the interaction between processes. They can, however, handle a high number of operations at once.
Characterization of the Device
A comma separates two sorts of numbers that define a device: the major device number and the minor device number. They are shown with commands 10, 122, and so on. The major device number identifies the device driver in use, whereas the minor device number notifies the kernel of which device is in this driver class.
Device Name
The most common device names are discussed below. So, stick with me.
SCSI Devices
The protocol utilized in mass storage on your system is called SCSI, which stands for small computer system interface. This protocol is used to provide interaction between your system’s disks, printers, and other peripherals. SCSI drives and hard disks are linked in the /dev directory. They use the sd prefix (SCSI disk). The common SCSI device files are /dev/sda, /dev/sdb etc.
Take your coding skills to the next level with our comprehensive guides, “Python Beginner to Advanced” and “Java Beginner to Advanced.” Whether you’re diving into Python or mastering Java, these books provide step-by-step guidance and in-depth knowledge to elevate your programming expertise.
Pseudo Devices
These devices are not physically connected to the system, and most of them are generally character devices. The most common pseudo devices are given below.
- /dev/null – it ignores all input and generates no output.
- /dev/zero – It takes and ignores all input and outputs a stream of NULL bytes indefinitely.
- /dev/random – it generates random numbers.
PATA Devices
You may see in the older systems that hard drives are being denoted with the hd prefix.
- /dev/hda – First hard disk
- /dev/hdb – Second hard disk
- /dev/hdd2 – 4th hard disk’s second partition
Sysfs
It’s a virtual filesystem that functions similarly to the /dev directory and is usually mounted on the /sys directory. It maintains more extensive information about devices than /dev. Both /dev and /sys are similar in certain ways, yet they differ in others. The /dev directory is straightforward and allows other applications to access devices; on the other hand, the /sys directory is used to see information and maintain devices. Let’s have a peek at the /sys directory now.
$ ls /sys/
$ ls /sys/block/
You can’t communicate with the devices from the /sys filesystem because the files you see here aren’t device nodes; instead, you’re managing them.
lsusb, lspci, lssci
These commands are like the ‘ls’ used for finding the list of devices. The lsusb command is used for displaying USB device information. Let’s run the command and see its functionality.
$ lsusb
The lspci command is used for displaying information about pci devices on your system.
$ lspci
The lsscsi command is used for viewing the information about SCSI devices. You may find that command is not found. In this case, you have to install it first by the command line below.
$ sudo apt install lsscsi
Now, run the command.
$ lsscsi