Menu

Control mounting and unmounting of filesystems

“Attach filesystems by hand with mount, at boot with /etc/fstab or systemd units, by label or UUID, and let users mount USB sticks.”

View .md

On Windows every disk gets its own letter. Linux has one directory tree that starts at /, and every filesystem is attached somewhere inside it. Attaching is called mounting, and the directory you attach it to is the mount point.

Your root filesystem is mounted on /, the EFI partition on /boot/efi, a data disk maybe on /srv/data. To a program it’s all one tree. Knowing how mounting works lets you add disks, use USB sticks, and fix a machine that won’t boot because of one bad line in /etc/fstab.

Mounting by hand#

mount DEVICE MOUNTPOINT attaches a filesystem. The mount point must be an existing directory:

console
$ sudo mkdir -p /mnt/data
$ sudo mount /dev/sdb1 /mnt/data
$ ls /mnt/data
lost+found  notes.txt

mount usually detects the type. Name it yourself with -t, and pass options with -o:

bash
sudo mount -t ext4 -o ro,noexec /dev/sdb1 /mnt/data
sudo mount -o remount,rw /mnt/data
sudo mount -o loop,ro debian-12.iso /mnt/iso
sudo mount --bind /srv/www /var/www

The second line changes options on an already mounted filesystem. The third mounts an ISO image through a loop device. The last one, a bind mount, makes the same directory appear in a second place.

Note

Anything that was already inside the mount point is hidden while something is mounted on it, not deleted. Unmount and it’s back.

What is mounted right now?#

mount with no arguments lists every mount. That list is long, so filter it, or use findmnt, which is easier to read:

console
$ mount | grep sdb1
/dev/sdb1 on /mnt/data type ext4 (rw,relatime)
$ findmnt /mnt/data
TARGET    SOURCE    FSTYPE OPTIONS
/mnt/data /dev/sdb1 ext4   rw,relatime

The kernel’s own list is /proc/mounts. The old /etc/mtab file is now just a symlink to it.

Unmounting#

umount (no “n” after the “u”) detaches a filesystem. Give it the mount point or the device:

bash
sudo umount /mnt/data
sudo umount /dev/sdb1

Unmount before you pull out a USB stick: Linux keeps writes in memory for a while, and umount makes sure they reach the disk.

If a process has a file open or a shell sits inside the directory, you get this:

console
$ sudo umount /mnt/data
umount: /mnt/data: target is busy.
$ sudo fuser -vm /mnt/data
                     USER        PID ACCESS COMMAND
/mnt/data:           root     kernel mount /mnt/data
                     alex       2314 ..c.. bash

The c means bash has its current directory there. lsof /mnt/data shows the same from another angle. cd out or stop the process, then try again. Last resorts: umount -l (lazy: detach now, clean up when no longer busy) and umount -f (force, mainly for an unreachable NFS server).

Labels and UUIDs#

Device names like /dev/sdb are handed out in the order the kernel finds disks. Add a disk or move a cable, and yesterday’s /dev/sdb is today’s /dev/sdc. So for anything permanent, identify a filesystem by something stored on it:

  • UUID: a unique ID written by mkfs. Always there, never changes unless you change it.
  • LABEL: a name you choose, like data or USBSTICK. Easy to read, but you must keep labels unique yourself.
  • PARTUUID and PARTLABEL: the same idea for a GPT partition, even before it has a filesystem.

blkid reads them from the devices:

console
$ sudo blkid
/dev/sda1: UUID="1A2B-3C4D" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="6b1f0a52-6c1d-4f7e-a2a1-0c9d7e3b5f10"
/dev/sda2: UUID="b7e4a1c2-3d4e-4f50-8a6b-7c8d9e0f1a2b" TYPE="swap" PARTUUID="0d2c4e6f-8a1b-4c3d-9e5f-7a6b8c9d0e1f"
/dev/sda3: UUID="3c1d6f0e-8a7b-4e2a-9d61-0f6b2a9c4e11" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="a4e1c2b3-5d6f-4a7b-8c9d-0e1f2a3b4c5d"
/dev/sdb1: LABEL="data" UUID="7d0c9b8a-1e2f-4a3b-8c4d-5e6f7a8b9c0d" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="data" PARTUUID="e5f6a7b8-9c0d-4e1f-a2b3-c4d5e6f7a8b9"

blkid /dev/sdb1 asks about one device. blkid -L data and blkid -U 7d0c... go the other way and print the device that has that label or UUID.

lsblk -f shows the same information as a tree, plus where each one is mounted:

console
$ lsblk -f
NAME   FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sda
├─sda1 vfat   FAT32       1A2B-3C4D                              1015M     1% /boot/efi
├─sda2 swap   1           b7e4a1c2-3d4e-4f50-8a6b-7c8d9e0f1a2b                [SWAP]
└─sda3 ext4   1.0         3c1d6f0e-8a7b-4e2a-9d61-0f6b2a9c4e11     26.9G    22% /
sdb
└─sdb1 ext4   1.0   data  7d0c9b8a-1e2f-4a3b-8c4d-5e6f7a8b9c0d    512.4M     1% /mnt/data

Mount with them directly:

bash
sudo mount UUID=7d0c9b8a-1e2f-4a3b-8c4d-5e6f7a8b9c0d /mnt/data
sudo mount LABEL=data /mnt/data

Behind the scenes, udev keeps symlinks for every disk in /dev/disk/by-uuid/, /dev/disk/by-label/, /dev/disk/by-partuuid/ and /dev/disk/by-id/. You can use those paths anywhere a device name is expected.

To set or change a label, each filesystem has its own tool: e2label or tune2fs -L for ext, xfs_admin -L for XFS, fatlabel for FAT, swaplabel -L for swap, btrfs filesystem label for Btrfs.

Mounting at boot: /etc/fstab#

/etc/fstab (filesystem table) lists what to mount at boot. One filesystem per line, six fields separated by spaces:

/etc/fstabtext
# <device>                                 <mount point> <type> <options>          <dump> <pass>
UUID=3c1d6f0e-8a7b-4e2a-9d61-0f6b2a9c4e11  /             ext4   errors=remount-ro  0      1
UUID=1A2B-3C4D                             /boot/efi     vfat   umask=0077         0      1
UUID=b7e4a1c2-3d4e-4f50-8a6b-7c8d9e0f1a2b  none          swap   sw                 0      0
LABEL=data                                 /srv/data     ext4   defaults,nofail    0      2
//nas.local/share                          /mnt/nas      cifs   credentials=/root/.nas,_netdev  0  0
FieldMeaning
1. deviceUUID=..., LABEL=..., PARTUUID=..., a /dev/... path, or a network share
2. mount pointthe directory; none (or swap) for swap
3. typeext4, xfs, btrfs, vfat, exfat, swap, nfs, cifs, tmpfs, or auto
4. optionscomma separated, no spaces; defaults if you have nothing special
5. dumpused by the old dump backup tool; almost always 0
6. passfsck order at boot: 1 root, 2 others, 0 never

XFS and Btrfs lines usually get 0 in the pass field, because they don’t use fsck at boot.

Common mount options#

OptionMeans
defaultsrw,suid,dev,exec,auto,nouser,async
ro / rwread-only / read-write
auto / noautomount (or not) at boot and with mount -a
useran ordinary user may mount it; only that user may unmount. Implies noexec,nosuid,nodev
usersany user may mount and unmount it. Same implied options
ownerthe user who owns the device file may mount it
noexecprograms on it can’t be run
nosuidignore SUID and SGID bits on it
nodevignore device files on it
noatimedon’t update access times on every read (faster)
nofaildon’t fail the boot if the device is missing
_netdevneeds the network, so wait for it
uid=, gid=, umask=owner and permissions for FAT and exFAT, which don’t store any

Test before you reboot#

A typo in fstab can drop you into an emergency shell at the next boot. So test right away:

console
$ sudo findmnt --verify
Success, no errors or warnings detected
$ sudo mount -a
mount: (hint) your fstab has been modified, but systemd still uses
       the old version; use 'systemctl daemon-reload' to reload.
$ sudo systemctl daemon-reload

mount -a mounts everything in fstab that isn’t mounted yet and isn’t noauto. No output means it worked. On systemd systems, also run systemctl daemon-reload after editing fstab, as the hint says.

You can also mount any fstab entry by naming only its mount point or device: sudo mount /srv/data. mount looks up the rest in fstab.

Letting users mount removable media#

Normally only root can mount. For a USB stick that a normal user should mount and unmount, add an fstab line with user (or users) and noauto:

/etc/fstabtext
LABEL=USBSTICK  /media/usb  vfat  noauto,user,uid=1000,gid=1000  0  0
console
$ sudo mkdir -p /media/usb
$ mount /media/usb
$ ls -ld /media/usb
drwxr-xr-x 4 alex alex 16384 Jan  1  1970 /media/usb
$ umount /media/usb

No sudo needed for the user. uid= and gid= make the files belong to user 1000, because FAT has no Unix owners of its own.

The FHS says /media/ is the place for removable media, and /mnt/ for temporary mounts by the admin. On a desktop you rarely write fstab lines for USB sticks: udisks2 mounts them on demand when you click them in the file manager.

DistributionDesktop automount location
Debian, Ubuntu/media/<user>/<label>
RHEL, Fedora/run/media/<user>/<label>

From a terminal, udisksctl mount -b /dev/sdc1 does the same without root, and udisksctl unmount -b /dev/sdc1 undoes it.

systemd mount units#

On a systemd system, fstab isn’t read directly at boot. systemd-fstab-generator turns each line into a mount unit, and systemd mounts those. You can see them:

console
$ systemctl list-units --type=mount
  UNIT              LOAD   ACTIVE SUB     DESCRIPTION
  -.mount           loaded active mounted Root Mount
  boot-efi.mount    loaded active mounted /boot/efi
  srv-data.mount    loaded active mounted /srv/data
...

The unit name is the mount point with / replaced by -: /srv/data becomes srv-data.mount, and / becomes -.mount. systemd-escape -p --suffix=mount /srv/data prints it for tricky paths.

You can also skip fstab and write a unit yourself. The file name must match the mount point:

/etc/systemd/system/srv-data.mountini
 1[Unit]
 2Description=Data disk
 3
 4[Mount]
 5What=/dev/disk/by-uuid/7d0c9b8a-1e2f-4a3b-8c4d-5e6f7a8b9c0d
 6Where=/srv/data
 7Type=ext4
 8Options=defaults,noatime
 9
10[Install]
11WantedBy=multi-user.target
bash
sudo systemctl daemon-reload
sudo systemctl enable --now srv-data.mount

A matching .automount unit (or the fstab option x-systemd.automount) mounts the filesystem only when something first accesses the directory. That’s handy for network shares and slow disks.

fstab is still the usual way and what most admins expect to find. Mount units are useful when a mount needs to wait for a specific service, because you can add After= and Requires= lines like in any other unit.

Exam checklist#

  • mount DEVICE DIR, -t TYPE, -o OPTIONS. mount alone or findmnt lists mounts. mount -a mounts all of fstab except noauto.
  • umount DIR or umount DEVICE. “target is busy”: find the process with fuser -vm or lsof.
  • Use UUID= or LABEL= instead of /dev/sdX. blkid and lsblk -f show them, and blkid -L/-U find the device.
  • fstab fields: device, mount point, type, options, dump, pass. Pass 1 root, 2 others, 0 never.
  • defaults = rw,suid,dev,exec,auto,nouser,async.
  • user lets one user mount and unmount, users lets anyone unmount, owner needs the device owner. All imply nosuid,nodev (and user/users also noexec).
  • /media/ is for removable media, /mnt/ for temporary mounts. Desktops automount to /media/<user>/ (Debian/Ubuntu) or /run/media/<user>/ (RHEL/Fedora).
  • systemd turns fstab lines into .mount units named after the path (/srv/data = srv-data.mount). Run systemctl daemon-reload after changing fstab.

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. Which /etc/fstab line mounts the filesystem with label backup on /backup at boot, and doesn’t stop the boot if the disk is missing?
  2. umount /mnt/data answers “target is busy”. What’s the best next step?
  3. In /etc/fstab, what does the user option do?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy