Linux: How to Access Devices by Using Linux Command

Home /

Table of Contents

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


- Linux: How to Access Devices by Using Linux Command

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
CoMYtQLzfvn29p2TU5doql4mzyzAv8dep9nHJGZN2Urdju6nHfrS31OLVMn6Fx sp4KloTEqKj73IJ31oVbQCveuQxuI Uhv5WdMZCT0S hb4p5NCPvXFJL WdkgkM3UDUKa851oswgfEOk2fMP2dbCAXjAzUCx8W2SiV9zIZDu5WDz4sx0DOT e - Linux: How to Access Devices by Using Linux Command

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.

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/

 HrvAZLi5fgi3bOsV46YVVdt2ryeSknVWuplOpxVLuwjEehOu0sEMo4jsJ7xrgAjA4V2z3fUtGoKeFORU36fF1gqEc0KKIKFV4NziIddgK4RrNr Fd6QGMkklKtDJLM GFaro4xip8UHTlNMBcba91LiiAlH1FjuDHT1zt2mo3PZ5oyMuq77 pbTS - Linux: How to Access Devices by Using Linux Command

$ ls /sys/block/
- Linux: How to Access Devices by Using Linux Command

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
- Linux: How to Access Devices by Using Linux Command

The lspci command is used for displaying information about pci devices on your system. 

$ lspci
2gz6kK25X717mA3lsfkYs0TZoDUo3BEwRAIQ7fU6si9Bgta Is6hMWQGcrsiwiOz5Gy W1Y4Pwg11HzvxxXhAy7GTINIaPJiYuQR5YE7dqr8YSfd5qa6uw8bd9gx 3NDUlLoQ6 hNw9IcTQX ha7kHWRnVFK uzm3UQPdPEljvbiVnYQsRoSUnZ3 - Linux: How to Access Devices by Using Linux Command

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


iLEuNlt7 4s7qQXS6Lfu2PZbTFAndD2D5spAIMTLythJGa5hYMT8dTTefpSWyXZeYPOCBRnJipU4IL4xZj27GiWfu lyY8unHegoUhY25JspZ cJFao0y6jzDKZvqbgPELyN43eeDRnxgLr0sw53rjLtaGaV7wwYCZQdI - Linux: How to Access Devices by Using Linux Command

Now, run the command.

$ lsscsi

YF5cjQWzeU aj1tFfPJX 6yYUbTCrUiwQpdE4809hifjc01old5OmseFjRcCjwEHt0SlnjBsyDv3pV49ake1qUZIlMFax0Ac3U23UTAxBUUmkPvuwRClzj4BK0m257T xq7xWBVJI9XBSULyZlBzwon1ZuG1 zS84nf4M5CsnJ w7c5GLCY UORw - Linux: How to Access Devices by Using Linux Command

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit
Other Recommended Article