brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: /dev/disk/by-id gives a disk a name that never changes

Disk
~/tips/tip-disk-by-id-stable-names

ls -l /dev/disk/by-id/

Uses a name based on model and serial number instead of sda or sdb, which can swap after a reboot.

/dev/sda isn’t a fixed name. Add a USB drive or a controller, and after the next boot the disk that was sdb can be sdc. For anything that matters, I’d use a name that sticks.

ls -l /dev/disk/by-id/

udev creates a symlink for every disk with a name built from the bus, model and serial number, like ata-WDC_WD40EFRX-68N32N0_WD-WCC7K0123456. Partitions get -part1, -part2 added. The link points at whatever sdX name the disk got this time, so the by-id name always reaches the same physical disk.

That’s the name I’d use when creating a ZFS pool, in scripts that run smartctl or for dd to a specific disk. It’s harder to mix up two disks when the serial is in the name, and that serial is also printed on the label, so you can match it to the disk in your hand.

Next to by-id there are other folders: by-uuid and by-label name filesystems, by-path names the port a disk is connected to.

Two cautions. Some USB enclosures report the same fake serial for every disk, so two of those get confusing names. And a disk often has several by-id links, for example an ata- and a wwn- one; both are stable, just pick one style and stick with it.

Further reading: Arch Wiki: Persistent block device naming, udev(7) manual page

More Disk tips sudo e2freefrag /dev/sdb1 sudo smartctl -a --json /dev/sda | jq '.smart_status.passed' lsscsi -s udisksctl power-off -b /dev/sdX du -sh — .[!.]* * 2>/dev/null | sort -h All Disk tips (44) All topics

Link