>_ Operating systems & Linux

Disks and partitions on Linux: lsblk, mount and /etc/fstab

How to identify disks and partitions on Linux, create a partition with parted, format it, mount it by hand and make it permanent in /etc/fstab using UUIDs.

Adding a second disk to a Linux computer, or connecting an external disk that must always be available, is an operation that frightens people more than it deserves. The steps are always the same: identify the disk, partition it, format it, mount it and, if needed, make it permanent. The only real risk is picking the wrong disk, and working methodically is enough to avoid it.

Identifying disks

Linux represents every disk as a file in /dev. The names follow a precise logic:

  • /dev/sda, /dev/sdb: SATA, USB and SCSI disks, in order of detection; partitions are sda1, sda2;
  • /dev/nvme0n1: the first NVMe SSD; partitions are nvme0n1p1, nvme0n1p2;
  • /dev/mmcblk0: SD cards and eMMC storage, with partitions mmcblk0p1.

The clearest command for getting your bearings is:

lsblk -f

It shows the tree of disks and partitions with size, filesystem, label, UUID and mount point. Before any destructive operation it is worth running it, connecting the new disk and running it again: the line that appears is the right one. Running sudo dmesg | tail straight after connecting also confirms the name assigned.

Beware: the letters sda, sdb are not stable. They can change from one boot to the next if disks are added or removed. That is why permanent configurations use UUIDs.

Creating a partition table

A new disk first needs a partition table. Today's standard is GPT, which overcomes the old MBR's limits on disk size and number of partitions. With parted, assuming the new disk is /dev/sdb:

sudo parted /dev/sdb mklabel gpt
sudo parted -a optimal /dev/sdb mkpart data ext4 0% 100%

The first command wipes any structure present on the disk: that is why the lsblk check comes first. The second creates a partition using all the space, correctly aligned. If you prefer an interactive interface you can use fdisk or cfdisk, or GParted in a graphical environment.

Formatting

The new partition is still empty: it needs a filesystem. For a data disk used only by Linux, ext4 is a safe choice:

sudo mkfs.ext4 -L data /dev/sdb1

The -L option assigns a readable label. For a disk that will also be connected to Windows and macOS, use mkfs.exfat instead.

Mounting by hand

On Linux a disk has no drive letter: its contents appear inside any folder you choose, the mount point.

sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data
df -h /mnt/data

To unmount it, use sudo umount /mnt/data. If the system replies «target is busy», some program still has a file open or a terminal is sitting inside that folder; lsof +f -- /mnt/data shows who is using it.

A detail that often confuses people: if you mount a disk over a folder that already contains files, those files don't disappear but stay hidden until the disk is unmounted.

Making the mount permanent

A manual mount lasts until the next reboot. To make it automatic you add a line to /etc/fstab. First get the UUID:

sudo blkid /dev/sdb1

Then add a line like this:

UUID=3f2a1c9e-...  /mnt/data  ext4  defaults,nofail  0  2

The six fields give: the device, the mount point, the filesystem type, the options, a historical field for the dump program that is left at 0 today, and the order of the boot-time check (1 for the root, 2 for other disks, 0 for no check).

Option Meaning
defaults standard read-write settings
nofail boot continues even if the disk is missing
noatime does not update the last-access time, fewer writes
ro read-only
x-systemd.automount mounts on first access instead of at boot

Checking before rebooting

A mistake in /etc/fstab can halt the boot and leave you in an emergency shell. Before rebooting, always check:

sudo systemctl daemon-reload
sudo mount -a
sudo findmnt --verify

mount -a tries to mount everything listed and not yet mounted: if it reports an error, fix it straight away. findmnt --verify checks the file's syntax. In my work I add nofail to every disk that isn't essential for booting: an unplugged external disk must never stop the computer from starting.

Permissions on the new disk

A freshly created ext4 filesystem belongs to root. To make it writable by your user:

sudo chown -R $USER:$USER /mnt/data

With exFAT and FAT32, on the other hand, Linux permissions don't exist: owner and permissions are set at mount time with the uid=, gid= and umask= options.

In short

  • lsblk -f before and after connecting identifies the right disk with certainty.
  • The steps are always: GPT table, partition, filesystem, mount.
  • Names like sdb can change; /etc/fstab uses UUIDs.
  • nofail keeps a missing disk from blocking the boot.
  • After every change to /etc/fstab, run mount -a and findmnt --verify before rebooting.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. With degrees in computer science and psychology, he combines technical analysis with an understanding of the human factor.

Read also