Block vs Character Devices

In Linux, everything is a file — including hardware. Hard drives, keyboards, random number generators, terminals — all appear as files in /dev/. But not all device files are the same: block devices and character devices behave very differently.

Block Devices — Random Access Storage

Block devices store data in fixed-size blocks and support random access — you can read or write any block directly:

  • Hard drives: /dev/sda, /dev/sdb (SATA)
  • NVMe SSDs: /dev/nvme0n1, /dev/nvme1n1
  • Partitions: /dev/sda1, /dev/nvme0n1p1
  • Virtual disks: /dev/vda (in VMs)
  • Loop devices: /dev/loop0 (mount ISO files)
$ ls -la /dev/sd* /dev/nvme* brw-rw---- 1 root disk 8, 0 Jan 15 /dev/sda ← 'b' = block device brw-rw---- 1 root disk 8, 1 Jan 15 /dev/sda1 ← major=8, minor=1 # Direct disk access (careful — raw disk!) dd if=/dev/sda bs=512 count=1 | hexdump -C # read first sector

Character Devices — Sequential Stream

Character devices provide a stream of bytes, accessed sequentially (no seeking back):

  • /dev/null: Discards anything written. Reads return EOF.
  • /dev/zero: Reads return infinite null bytes.
  • /dev/random, /dev/urandom: Random bytes from kernel entropy pool.
  • /dev/tty: Your controlling terminal.
  • /dev/pts/0: Pseudo-terminal (SSH sessions, terminal emulators).
  • /dev/input/event0: Keyboard or mouse input events.
$ ls -la /dev/null /dev/random /dev/tty crw-rw-rw- 1 root root 1, 3 Jan 15 /dev/null ← 'c' = character device crw-rw-rw- 1 root root 1, 8 Jan 15 /dev/random crw-rw-rw- 1 root tty 5, 0 Jan 15 /dev/tty

Major and Minor Numbers

What are the numbers in device file listings? The two numbers after permissions (like "8, 1") are the major and minor numbers. The major number identifies the driver (8 = SCSI/SATA disk driver). The minor number identifies which specific device that driver manages (0 = whole disk, 1 = first partition).
# Common major numbers # 1 = mem (null, zero, random) # 4 = tty # 5 = console # 8 = SCSI/SATA disk (sda=0, sdb=16, sdc=32...) # 259 = NVMe (nvme0n1=0, nvme0n1p1=1) # Create a device node manually mknod /dev/mydevice c 89 0 # character device, major=89, minor=0

Practical Uses

# Wipe a disk (overwrite with zeros) dd if=/dev/zero of=/dev/sdb bs=4M status=progress # Generate random password head -c 32 /dev/urandom | base64 # Redirect errors to /dev/null command 2>/dev/null # Test write speed to disk dd if=/dev/zero of=/dev/sdb bs=1G count=1 oflag=direct

Frequently Asked Questions

What will I learn here?

This page covers the core concepts and techniques you need to understand the topic and progress confidently to the next lesson.

How should I use this page?

Start with the overview, then follow the section links to deepen your understanding. Use the table of contents on the right to jump to specific sections.

What should I read next?

Use the navigation below to continue to the next lesson or explore related topics.