On Linux, “everything is a file”, including hardware. Disks, terminals, random number generators and the infamous /dev/null all appear as device files in /dev. Understanding them helps you identify disks on a VPS, write scripts that discard output, and avoid the most dangerous commands in system administration.

Block vs character devices

ls -l /dev/vda /dev/null /dev/tty
brw-rw---- 1 root disk 252, 0 Nov  5 09:00 /dev/vda
crw-rw-rw- 1 root root   1, 3 Nov  5 09:00 /dev/null
crw-rw-rw- 1 root tty    5, 0 Nov  5 09:00 /dev/tty
Type First letter Accessed Examples
Block b In blocks, with buffering (random access) Disks, partitions, LVM volumes
Character c As a stream of bytes Terminals, /dev/null, /dev/urandom, serial ports

The two numbers (252, 0) are the major number (which driver) and the minor number (which device).

Disks and partitions on a VPS

Name Typical source
/dev/vda, /dev/vdb virtio disks (common on KVM VPS)
/dev/sda, /dev/sdb SCSI/SATA disks (or virtio-scsi)
/dev/nvme0n1 NVMe disks (on hardware or VMs that present NVMe)
/dev/vda1, /dev/sda2 Partitions
/dev/mapper/… LVM and encrypted volumes

List them safely:

lsblk -f            # disks, partitions, filesystems, mount points
sudo blkid          # UUIDs and filesystem types
df -h               # mounted filesystems and usage

Use UUIDs (from blkid) in /etc/fstab, not names like /dev/vdb1, because device names can change between boots. See Linux directory structure and what is KVM virtualization.

Special devices you’ll use

Device Behaviour Typical use
/dev/null Discards everything written; reads return nothing cmd > /dev/null 2>&1
/dev/zero Endless zero bytes Create files: dd if=/dev/zero of=file bs=1M count=100
/dev/urandom Endless random bytes Keys, passwords: head -c 32 /dev/urandom | base64
/dev/random Random bytes (same as urandom on modern kernels) —
/dev/tty Your current terminal Prompt the user even when stdout is redirected
/dev/stdin, /dev/stdout, /dev/stderr The standard streams Pass streams as file names
/dev/shm Shared memory (a tmpfs in RAM) Fast temporary files

See pipes and redirection.

udev: who creates device files?

Modern Linux creates /dev entries dynamically with udev when the kernel detects hardware. udev also creates stable symlinks:

ls -l /dev/disk/by-uuid/
ls -l /dev/disk/by-id/
udevadm info --query=all --name=/dev/vda | head

Dangerous commands: double-check device names

Commands that write directly to a block device destroy whatever is on it:

dd if=image.img of=/dev/vdb       # overwrites /dev/vdb completely
mkfs.ext4 /dev/vdb1               # formats the partition
  • Run lsblk immediately before, and make sure you have the right device. /dev/vda is usually your system disk on a VPS.
  • Never write to a mounted filesystem’s device.
  • Have backups. See backup strategy.

Permissions on devices

Access is controlled like files: the disk group can read raw disks, the tty group terminals, and so on. Don’t add users to disk: that’s effectively root access.

Quick reference

Task Command
List disks and partitions lsblk -f
UUIDs sudo blkid
Discard output > /dev/null 2>&1
Random secret head -c 32 /dev/urandom | base64
Device info udevadm info --name=/dev/vda
Stable names /dev/disk/by-uuid/

Related: disk full troubleshooting and Linux kernel modules.

  • Linux
  • Storage
  • Filesystem