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.”
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:
$ 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:
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/wwwThe 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:
$ 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:
sudo umount /mnt/data
sudo umount /dev/sdb1Unmount 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:
$ 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
dataorUSBSTICK. 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:
$ 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:
$ 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:
sudo mount UUID=7d0c9b8a-1e2f-4a3b-8c4d-5e6f7a8b9c0d /mnt/data
sudo mount LABEL=data /mnt/dataBehind 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:
# <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:
$ 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:
LABEL=USBSTICK /media/usb vfat noauto,user,uid=1000,gid=1000 0 0$ 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:
$ 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:
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.targetsudo systemctl daemon-reload
sudo systemctl enable --now srv-data.mountA 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.
After= and Requires= lines like in any other unit.Exam checklist#
mount DEVICE DIR,-t TYPE,-o OPTIONS.mountalone orfindmntlists mounts.mount -amounts all of fstab exceptnoauto.umount DIRorumount DEVICE. “target is busy”: find the process withfuser -vmorlsof.- Use
UUID=orLABEL=instead of/dev/sdX.blkidandlsblk -fshow them, andblkid -L/-Ufind the device. - fstab fields: device, mount point, type, options, dump, pass. Pass
1root,2others,0never. defaults=rw,suid,dev,exec,auto,nouser,async.userlets one user mount and unmount,userslets anyone unmount,ownerneeds the device owner. All implynosuid,nodev(anduser/usersalsonoexec)./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
.mountunits named after the path (/srv/data=srv-data.mount). Runsystemctl daemon-reloadafter changing fstab.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(lpic-104): Control mounting and unmounting of filesystems"
[main 2232c54] learn(lpic-104): Control mounting and unmounting of filesystems
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: