# 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.

Source: https://aminbytes.dev/bytes/devices-and-filesystems/mounting/


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:

```text {title="/etc/fstab"}
# <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
```

| Field | Meaning |
| --- | --- |
| 1. device | `UUID=...`, `LABEL=...`, `PARTUUID=...`, a `/dev/...` path, or a network share |
| 2. mount point | the directory; `none` (or `swap`) for swap |
| 3. type | `ext4`, `xfs`, `btrfs`, `vfat`, `exfat`, `swap`, `nfs`, `cifs`, `tmpfs`, or `auto` |
| 4. options | comma separated, no spaces; `defaults` if you have nothing special |
| 5. dump | used by the old `dump` backup tool; almost always `0` |
| 6. pass | fsck 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

| Option | Means |
| --- | --- |
| `defaults` | `rw,suid,dev,exec,auto,nouser,async` |
| `ro` / `rw` | read-only / read-write |
| `auto` / `noauto` | mount (or not) at boot and with `mount -a` |
| `user` | an ordinary user may mount it; only that user may unmount. Implies `noexec,nosuid,nodev` |
| `users` | any user may mount and unmount it. Same implied options |
| `owner` | the user who owns the device file may mount it |
| `noexec` | programs on it can't be run |
| `nosuid` | ignore SUID and SGID bits on it |
| `nodev` | ignore device files on it |
| `noatime` | don't update access times on every read (faster) |
| `nofail` | don't fail the boot if the device is missing |
| `_netdev` | needs 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`:

```text {title="/etc/fstab"}
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.

| Distribution | Desktop 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:

```ini {title="/etc/systemd/system/srv-data.mount"}
[Unit]
Description=Data disk

[Mount]
What=/dev/disk/by-uuid/7d0c9b8a-1e2f-4a3b-8c4d-5e6f7a8b9c0d
Where=/srv/data
Type=ext4
Options=defaults,noatime

[Install]
WantedBy=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.

 (Side note: 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

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?
   - **`LABEL=backup /backup ext4 defaults,nofail 0 2`** (correct)
   - `/backup LABEL=backup ext4 defaults,nofail 0 2`
   - `LABEL=backup /backup ext4 noauto 0 2`
   The order is device, mount point, type, options, dump, pass. `nofail` lets the boot carry on without the disk. `noauto` would skip it at boot entirely.

2. `umount /mnt/data` answers "target is busy". What's the best next step?
   - Run `mount -a`
   - **Find the process with `fuser -vm /mnt/data` or `lsof /mnt/data`, then stop it or `cd` out** (correct)
   - Run `fsck` on the device
   Some process has a file open or its working directory inside the mount. `fuser` and `lsof` show which one.

3. In `/etc/fstab`, what does the `user` option do?
   - Mounts the filesystem in the user's home directory
   - Gives every user write access to all files
   - **Lets an ordinary user mount it, and only that user can unmount it again** (correct)
   `user` allows a normal user to mount the entry (it also implies `noexec,nosuid,nodev`). Use `users` if any user may unmount it.


