Devices: /dev, lsblk, udev
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- Files under
/devare devices: block devices (disks,b) and character devices (terminals,c). - Disk names:
sda,sdb(SATA/SCSI/virtual),nvme0n1(NVMe),vda(KVM virtio); partitionssda1,nvme0n1p1. lsblk -fshows disks, partitions, file systems and mount points as a tree.blkidgives each partition's UUID — a name can change, the UUID does not.- udev creates the
/devfile when a device is connected;dmesgandudevadm monitorshow what happened.
Everything is a file
A famous Linux principle: you work with devices as files too. Writing to a disk, sending text to a terminal, reading random numbers — all go through files under /dev.
ali@srv-web01:~$ ls -l /dev/sda /dev/sda1 /dev/tty1 /dev/null brw-rw---- 1 root disk 8, 0 Dec 1 08:55 /dev/sda brw-rw---- 1 root disk 8, 1 Dec 1 08:55 /dev/sda1 crw--w---- 1 root tty 4, 1 Dec 1 08:55 /dev/tty1 crw-rw-rw- 1 root root 1, 3 Dec 1 08:55 /dev/null
The first letter shows the type: b — a block device (reads data in blocks, disks), c — a character device (works as a stream: terminals, /dev/null). The 8, 0 shown instead of a size are the device numbers (major, minor).
| File | What it is |
|---|---|
/dev/sda, /dev/nvme0n1 | A whole disk |
/dev/sda1 | The disk's first partition |
/dev/null | A "black hole" — everything written is lost |
/dev/zero | Endless zeros |
/dev/urandom | Random bytes |
/dev/tty1, /dev/pts/0 | Console and SSH terminals |
The logic of disk names
| Name | Disk type | Partition example |
|---|---|---|
sda, sdb, sdc | SATA, SAS, USB, many virtual disks | sda1, sda2 |
nvme0n1, nvme1n1 | NVMe SSD | nvme0n1p1 (with p) |
vda, vdb | KVM/virtio virtual disk | vda1 |
sr0 | CD/DVD | — |
The letters follow the order in which disks are found — so after a reboot sdb and sdc can swap. That is why configuration uses the UUID, not the name (end of the lesson).
lsblk: a map of the disks
ali@srv-web01:~$ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS sda 8:0 0 20G 0 disk ├─sda1 8:1 0 1G 0 part /boot/efi ├─sda2 8:2 0 2G 0 part /boot └─sda3 8:3 0 17G 0 part / sdb 8:16 0 10G 0 disk └─sdb1 8:17 0 10G 0 part /var/www sdc 8:32 0 20G 0 disk
The picture is clear: the system is on sda, the site is on a separate disk sdb, and sdc has just been added and is still empty — no partition, no mount point. We will prepare it in the next lesson.
lsblk -f shows the file system and UUID too:
ali@srv-web01:~$ lsblk -f /dev/sdb NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS sdb └─sdb1 ext4 1.0 www 5f3c9a2e-8d41-4b6e-9a77-2c1e0b6d4f18 8.7G 7% /var/www
blkid and the UUID
ali@srv-web01:~$ sudo blkid /dev/sdb1 /dev/sdb1: LABEL="www" UUID="5f3c9a2e-8d41-4b6e-9a77-2c1e0b6d4f18" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="c1d2e3f4-01" ali@srv-web01:~$ ls -l /dev/disk/by-uuid/ | grep sdb1 lrwxrwxrwx 1 root root 10 Dec 1 08:55 5f3c9a2e-8d41-4b6e-9a77-2c1e0b6d4f18 -> ../../sdb1
The UUID is a unique number given when the file system is created. Even if the disk is plugged into another port and its name becomes sdc, the UUID stays the same. The /dev/disk/by-uuid/, by-label/ and by-id/ directories hold links from stable names to the real device.
udev: what happens when a device is connected
When a disk is added, the kernel detects it and sends an event. The udev service catches the event, creates the file in /dev, builds the by-uuid links and sets permissions according to its rules.
ali@srv-web01:~$ sudo dmesg | tail -n 4 [ 5521.304112] scsi 2:0:2:0: Direct-Access QEMU QEMU HARDDISK 2.5+ PQ: 0 ANSI: 5 [ 5521.306981] sd 2:0:2:0: [sdc] 41943040 512-byte logical blocks: (21.5 GB/20.0 GiB) [ 5521.307420] sd 2:0:2:0: [sdc] Attached SCSI disk ali@srv-web01:~$ sudo udevadm monitor
| Tool | What for |
|---|---|
dmesg | Kernel messages: new disks, USB, driver errors |
udevadm monitor | Watches device events live |
udevadm info /dev/sdc | What udev knows about a device |
lsusb, lspci | Lists of USB and PCI devices |
If a disk was hot-added to a virtual server and does not appear in lsblk, you can rescan the SCSI bus: echo "- - -" | sudo tee /sys/class/scsi_host/host*/scan.
Look at the server's disks with lsblk and blkid in the Linux simulator.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What is the first letter of /dev/sda in ls -l?
2What is the name of the first partition of an NVMe disk?
p comes before the partition number.3Why is the UUID used in configuration instead of the disk name?
sdb and sdc can swap depending on detection order.4Which command shows as a tree whether a newly added disk has a file system?
df only shows mounted file systems.5Which service creates the /dev file when a device is connected?
Comments
Sign in to comment.