CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
  • Week 1
    • 1.Operating Systems and the Kernel: What Linux Does
    • 2.The Birth of Linux: From Unix to Linus
    • 3.Free Software and Open Source: What a License Decides
    • 4.Distributions: The Unit That Bundles the Kernel and Tools for Sale
    • 5.Week 1 Review: The Identity Behind the Name Linux
  • Week 2
  • Week 3
  • Week 4
  • Week 5
  • Week 6
  • Week 7
    • 1.Package Management (1): RPM and YUM
    • 2.Package Management (2): DEB, APT, and Source Installation
    • 3.Device Files and /dev
    • 4.Using Peripherals: Printers · Sound · Scanners
    • 5.Week 7 Comprehensive Review: Packages and Devices
  • Week 8
  • Week 9
  • Week 10
  • Week 11
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← Linux Master 2/Week 7/Day 3

Based on the Linux Master 2 exam syllabus·Report an error

Linux Master 2· AssociateWeek 7 · Day 3~21 min read

Day 3 - Device Files and /dev

📌 Key Takeaways

  • Linux follows "everything is a file" — keyboards, disks, and printers are all handled as files under /dev.
  • Devices are divided into block devices (b) and character devices (c). Disks are block; keyboards and printers are character.
  • Device files are told apart by a major number and a minor number. These two numbers appear in place of a size.
  • udev detects devices and automatically creates and deletes the files under /dev.
  • Hardware is queried with lsblk (block) · lsusb (USB) · lspci (PCI) · dmesg (kernel messages).

Devices Are Files Too

ls -l /dev/sda /dev/tty0
# brw-rw---- 1 root disk 8, 0 Sep  3 09:00 /dev/sda
# crw--w---- 1 root tty  4, 0 Sep  3 09:00 /dev/tty0
#  │                      │  │
#  │                      │  └── minor number
#  │                      └───── major number
#  └──────────────────────────── b = block, c = character
  • Where a regular file shows its size, two numbers appear. These are the major and minor numbers.
NumberMeaning
Major numberWhich driver is responsible (the kind of device)
Minor numberWhich device (the nth one) that driver handles

Seeing the Numbers Side by Side

$ ls -l /dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/null /dev/zero
brw-rw----. 1 root disk 8,  0 Sep  3 09:00 /dev/sda
brw-rw----. 1 root disk 8,  1 Sep  3 09:00 /dev/sda1
brw-rw----. 1 root disk 8,  2 Sep  3 09:00 /dev/sda2
brw-rw----. 1 root disk 8, 16 Sep  3 09:00 /dev/sdb
brw-rw----. 1 root disk 8, 17 Sep  3 09:00 /dev/sdb1
crw-rw-rw-. 1 root root 1,  3 Sep  3 09:00 /dev/null
crw-rw-rw-. 1 root root 1,  5 Sep  3 09:00 /dev/zero
  • The five disk lines all have major number 8. That means the same driver (sd) handles them, and the minor number decides which partition of which disk it is.
  • The sd driver sets aside 16 minor numbers per disk. So sda starts at 0 and sdb at 16, and partitions add their number to that (sdb1 = 16 + 1 = 17).
  • /dev/null and /dev/zero have the same major number (1) and differ only in the minor number. They are virtual devices with no real hardware, but since they pass data one character at a time, they are character devices (c).
  • You can see which major number belongs to which driver in /proc/devices.
MisconceptionReality
A device file holds data insideIt is a signpost holding just two numbers. When you read or write, the kernel hands it to the driver with that number
The name (sda) decides the deviceWhat the kernel looks at is the type (b/c) and the major/minor numbers. The name is for humans
Permissions do not applyrw- permissions apply just like regular files. Only root and the disk group can read and write /dev/sda
  • The command that creates a device file by hand is mknod — mknod name b|c major minor. These days udev does it for you, so it is rarely used directly.

Block Devices and Character Devices

ItemBlock deviceCharacter device
Markerbc
Unit handledFixed-size blocksOne character at a time (stream)
BufferUsedRarely used
Random accessPossibleSequential
ExamplesHard disk, USB, CD-ROMKeyboard, mouse, printer, terminal

Remember it as disk family = block, input/output family = character. The deciding criterion is "can you read an arbitrary position directly?" A disk can be read anywhere directly, but a keyboard streams in the order keys are pressed.

Major Device Files

Device fileWhat it is
/dev/sdaThe first SATA, SCSI, or USB disk
/dev/sda1The first partition of sda
/dev/sdbThe second disk
/dev/hdaA disk of the old IDE type
/dev/nvme0n1An NVMe SSD
/dev/sr0 (/dev/cdrom)The CD/DVD drive
/dev/nullThe place things are thrown away. Reading gives nothing
/dev/zeroReading gives endless zeros
/dev/random, /dev/urandomRandom number generation
/dev/ttyThe current terminal
/dev/lp0The first printer port
/dev/fd0Floppy disk

Disk Naming Rules

   /dev/sd a 1
        │  │ │
        │  │ └── partition number (from 1)
        │  └──── which disk (a, b, c...)
        └─────── sd = SATA/SCSI/USB,  hd = IDE
ExampleMeaning
/dev/sdaThe whole first disk
/dev/sda1Partition 1 of the first disk
/dev/sdb2Partition 2 of the second disk
  • Disks get letters, partitions get numbers. This combination is asked as is.
  • These days IDE disks are also often recognized as sd.
  • The letter is the order of detection, not the type. If you plug a USB stick into a computer whose internal disk is sda, it becomes sdb, and one more becomes sdc. If the plug-in order changes, the names change too.
NameHow to read it
/dev/sdc1Partition 1 of the third sd disk
/dev/hdb2Partition 2 of the second IDE disk
/dev/nvme0n1p2Device 1 of NVMe controller 0, its partition 2 — since the name ends in a digit, a p is placed before the partition number
/dev/vda1Partition 1 of a virtual machine's virtual disk (virtio)
/dev/sr0The first optical drive. Numbering starts from 0 and there are no partition numbers

Device Files for Terminals and Printers

Device fileWhat it is
/dev/tty1 ~ /dev/tty6Virtual consoles attached directly to the monitor and keyboard
/dev/pts/0, /dev/pts/1 …Pseudo-terminals — SSH sessions or terminal windows on the graphical screen
/dev/ttyS0The first serial port
/dev/lp0A printer on the first parallel port
/dev/usb/lp0The first USB printer
$ tty
/dev/pts/0
$ echo hello > /dev/pts/0
hello
  • The tty command tells you the device file of your current terminal. If you write characters to that file, they appear on the screen — the easiest way to confirm the saying "devices are files too".

Where Virtual Devices Are Used

DeviceUseExample
/dev/nullDiscards unwanted outputcommand > /dev/null 2>&1
/dev/zeroCreates an empty file filled with zerosdd if=/dev/zero of=test.img bs=1M count=100 (a 100MB file)
/dev/urandomGets random valuesGenerating temporary passwords and keys
  • Writing to /dev/null makes data vanish, and reading from it ends immediately (EOF). Reading /dev/zero gives zeros endlessly. You tell the two apart by "what comes out when you read".

udev — Device Files, Automatically

In the past, thousands of device files that would never be used were pre-created under /dev.

   Device connected (plug in USB)
        │
        ▼
   Kernel detects ──► udev ──► creates /dev/sdb file
        │                       assigns permissions/owner
        ▼                       applies naming rules
   Device removed ──► udev ──► deletes the file
  • udev creates and deletes files under /dev in real time as devices are connected and removed.
  • Rule files go in /etc/udev/rules.d/. You can configure things such as always giving a specific USB device the same name.
  • Thanks to this, only devices that actually exist are present in /dev.
  • /dev is not a directory stored on disk but a file system built in memory. It is populated anew at every boot to match the devices present at that time.
  • udev came in with kernel 2.6, replacing the older approach (devfs). Today it runs as a daemon called systemd-udevd.
Rule file locationNature
/usr/lib/udev/rules.d/ (/lib/udev/rules.d/ depending on the distribution)The default rules shipped by the distribution — do not edit them directly
/etc/udev/rules.d/Rules added by the administrator — if the file name is the same, this one takes precedence
  • Rule files take the form number-name.rules, like 70-persistent-net.rules, and are read starting from the smallest number.
   KERNEL=="sd?1", ATTRS{idVendor}=="0781", SYMLINK+="myusb"
   └────────── condition (==) ───────────┘  └── action (+=) ──┘
  • == is a condition meaning "if the device is like this", and = and += are instructions meaning "do this". The rule above adds a link called /dev/myusb to a device whose kernel-assigned name has the form sd?1, such as sda1 or sdb1, and whose vendor number is 0781.
  • There are also links that udev creates in advance. /dev/cdrom is a symbolic link pointing to /dev/sr0, and under /dev/disk/by-uuid/ there are links named by UUID. They let you use stable names instead of names like sdb and sdc that change with order.

Hardware Query Commands

CommandWhat it shows
lsblkBlock devices as a tree
lsusbConnected USB devices
lspciPCI devices (graphics, network card)
lscpuCPU info
lsmodLoaded kernel modules
dmesgKernel messages — the record of device detection
blkidA device's UUID and file system type
lsblk                  # disk and partition structure
lsusb                  # USB list
dmesg | tail -20       # what was the device I just plugged in recognized as
  • The commands starting with ls form a group — lsblk, lsusb, lspci, lscpu, lsmod.
  • If you plug in a USB and do not see it, use dmesg to check what the kernel recognized it as — that is the standard procedure.

Output of the Three Commands When a USB Stick Is Plugged In

$ dmesg | tail -4
[ 4521.103422] usb 1-2: new high-speed USB device number 5 using xhci_hcd
[ 4521.812345] sd 6:0:0:0: [sdb] 31250000 512-byte logical blocks: (16.0 GB/14.9 GiB)
[ 4521.813990]  sdb: sdb1
[ 4521.815102] sd 6:0:0:0: [sdb] Attached SCSI removable disk
 
$ lsusb
Bus 001 Device 005: ID 0781:5567 SanDisk Corp. Cruzer Blade
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
 
$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda      8:0    0   50G  0 disk
├─sda1   8:1    0    1G  0 part /boot
├─sda2   8:2    0    4G  0 part [SWAP]
└─sda3   8:3    0   45G  0 part /
sdb      8:16   1 14.9G  0 disk
└─sdb1   8:17   1 14.9G  0 part
sr0     11:0    1 1024M  0 rom
OutputHow to read it
[sdb] in dmesgThe kernel gave this device the name sdb
31250000 512-byte logical blocks31,250,000 blocks of 512 bytes = 16,000,000,000 bytes. That is 16.0 GB (decimal) and also 14.9 GiB (binary)
sdb: sdb1There is one partition
ID 0781:5567 in lsusbVendor number:product number. The idVendor in a udev rule is the first part
MAJ:MIN in lsblkMajor number:minor number — the same values as 8, 17 in ls -l /dev/sdb1
RM in lsblk1 means a removable device. sdb and sr0 are 1
MOUNTPOINT is emptyRecognized but not yet mounted
  • Adding up the partitions of sda gives 1G + 4G + 45G = 50G, which matches the disk size. The line with TYPE disk is the whole, and the lines with part are the pieces inside it.
  • The "16GB" on the product box showing as 14.9G in lsblk is not a fault but a difference in units.
  • Messages in dmesg accumulate in a ring buffer of fixed size, so old ones get pushed out. That is why you look at the end with tail right after plugging in.
What you want to knowCommand
How disks and partitions are divided, and where they are mountedlsblk
Whether a USB device is connected at alllsusb
Built-in expansion cards such as graphics and network cardslspci
What name the kernel recognized the device as, and whether there were errorsdmesg
A partition's UUID and file system typeblkid
Whether a device driver (module) is loadedlsmod
  • You view kernel modules with lsmod and load them with modprobe module-name. modprobe also loads other modules that are required, while insmod loads only the one file you specify. To unload, use rmmod.

Handling Removable Media

lsblk                        # check that it showed up as /dev/sdb1
sudo mount /dev/sdb1 /mnt    # attach
ls /mnt                      # check contents
sudo umount /mnt             # detach — always before pulling it out
  • Modern desktop environments automatically mount it under /media/username/.
  • Always umount before pulling it out. If write cache remains, data gets corrupted.
  • Give mount the partition (/dev/sdb1), not the whole disk (/dev/sdb). The file system lives inside the partition.
  • The command name is umount, not unmount.
MediumCommon device fileFile systemExample
USB stick/dev/sdb1vfat (FAT32)mount -t vfat /dev/sdb1 /mnt
CD/DVD/dev/sr0 (/dev/cdrom)iso9660mount -t iso9660 /dev/cdrom /mnt
SymptomCauseFix
umount: /mnt: target is busySomeone is inside it — often my shell's current directory is /mntcd out of it, then try again
Not in lsblk at allThe kernel did not recognize itCheck with lsusb, dmesg
In lsblk but contents do not showNot mountedmount
  • A CD/DVD is ejected with the eject command. If it is mounted, it is detached first and then the tray opens.
  • The mount point convention is: /mnt is where the administrator manually attaches things temporarily, and /media is where removable media are automatically attached.

Tomorrow we learn how to actually work with peripherals such as printers, sound, and scanners.

📖 Glossary

  • Device file: A special file created under /dev so that hardware can be handled like a file.
  • Block device: A device with random access in fixed-size blocks. Marked b; disks fall under it.
  • Character device: A device that exchanges data sequentially, one character at a time. Marked c; keyboards and printers fall under it.
  • Major number: The number that identifies the driver responsible for a device.
  • Minor number: The number that identifies an individual device handled by the same driver.
  • udev: The manager that detects device connection and removal and automatically creates and deletes files under /dev.
  • /dev/null: A device file that discards the data sent to it.
  • dmesg: A command that prints the messages left by the kernel. Used to check device detection results.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

Which character does ls -l show for a device that exchanges data in blocks, like a hard disk?

Question 2

Which of the following is a character device?

Question 3

Which device file represents the second partition of the first SATA disk?

Question 4

Which of the following detects device connection and removal and automatically creates and deletes device files under /dev?

Question 5

Which command prints the messages left by the kernel so you can check how a newly connected device was recognized?

PreviousPackage Management (2): DEB, APT, and Source InstallationWeek 7 · Day 2Next Using Peripherals: Printers · Sound · ScannersWeek 7 · Day 4

Official references

  • Linux Master 2 official exam syllabus(opens in a new tab)

On this page

  • Key Takeaways
  • Devices Are Files Too
  • Seeing the Numbers Side by Side
  • Block Devices and Character Devices
  • Major Device Files
  • Disk Naming Rules
  • Device Files for Terminals and Printers
  • Where Virtual Devices Are Used
  • udev — Device Files, Automatically
  • Hardware Query Commands
  • Output of the Three Commands When a USB Stick Is Plugged In
  • Handling Removable Media
  • Glossary
  • Practice Questions