mount
Attaching file systems — mount, umount, -o options, /etc/fstab, findmnt, loop devices and bind
Linux has a single directory tree, and every file system (a disk partition, an image, tmpfs, a network share) is attached to some directory — a mount point. mount attaches, umount detaches, findmnt shows what is mounted where. What to mount at boot is described in /etc/fstab.
Only root can mount (it needs the CAP_SYS_ADMIN capability). This privilege is disabled in the IzzyLab sandbox: mount and umount answer "permission denied" — even for tmpfs and bind. What does work: findmnt, findmnt --verify for checking fstab, blkid and creating images (see filesystems).
Syntax
mount # list (findmnt is better)
mount [-t type] [-o options] source dir
mount dir # take the rest from /etc/fstab
mount -a # mount everything from fstab
umount dir|device
findmnt [options] [device|dir]
mount options
| Option | What it does |
|---|---|
-t type | FS type: ext4, xfs, vfat, tmpfs, nfs… Usually detected automatically |
-o list | comma-separated mount options (table below) |
-r / -w | read-only (-o ro) / read-write (the default) |
-a | mount everything from fstab except noauto lines and what is already mounted |
-T file | another file instead of /etc/fstab |
-f | "fake": everything except the actual system call |
-v | verbose |
-L label / -U uuid | source by label or UUID (same as LABEL=…, UUID=…) |
-m | create the mount point directory if it doesn't exist |
--bind / -B | make a directory also available at another place |
--rbind / -R | the same, including all nested mount points |
--move / -M | move a mount point |
-o options
| Option | Meaning |
|---|---|
defaults | rw,suid,dev,exec,auto,nouser,async — the standard set for fstab |
ro / rw | read-only / read-write |
remount | change options of an already mounted FS without unmounting: -o remount,ro /data |
noexec | forbid running programs from this FS |
nosuid | ignore setuid/setgid bits |
nodev | ignore device files |
noatime / relatime | don't update access time / update it rarely (relatime is the default) |
loop | mount an image file via a loop device |
bind | same as --bind |
noauto | don't mount on mount -a and at boot |
nofail | don't stop the boot if the device is missing |
user / users | allow a regular user to mount (from fstab only) |
uid=, gid=, umask= | owner and permissions for file systems without Unix permissions (vfat, ntfs) |
size=100M, mode=1777 | size and permissions for tmpfs |
_netdev | network FS: mount after the network is up |
umount options
| Option | What it does |
|---|---|
-l | "lazy" unmount: remove from the tree now, release when files are closed |
-f | force (for an unreachable NFS) |
-R | unmount together with everything nested |
-d | for loop: also free the loop device |
-a | unmount everything that is mounted |
-q | don't complain if not mounted |
/etc/fstab
One line — one file system, six whitespace-separated fields:
# source mountpoint type options dump pass
UUID=247f8868-2629-4efe-94cd-dd6ac84027cc /data ext4 defaults,noatime 0 2
LABEL=backup /backup ext4 defaults,nofail 0 2
/srv/disk.img /mnt/img ext4 loop,ro 0 0
tmpfs /mnt/tmp tmpfs size=100M,mode=1777 0 0
| # | Field | Meaning |
|---|---|---|
| 1 | source | /dev/sdb1, UUID=…, LABEL=…, path to an image, tmpfs, server:/export |
| 2 | mount point | the directory must exist; for swap — none |
| 3 | type | ext4, xfs, tmpfs, nfs, swap, auto |
| 4 | options | as in -o; at least defaults |
| 5 | dump | 0 almost always |
| 6 | pass | fsck check order at boot: 1 — root, 2 — the rest, 0 — don't check |
blkid shows the UUID and label. Names like /dev/sdb1 can change after a reboot, the UUID doesn't.
findmnt
| Option | What it does |
|---|---|
| (no options) | tree of all mount points: TARGET, SOURCE, FSTYPE, OPTIONS |
dir / device | only this mount point; exit code 1 if there is none |
-T path | which FS this file or directory is on |
-t type[,type] | only the given types |
-O option | only those with the given mount option (-O ro) |
-l | flat list instead of a tree |
-n | no header |
-o columns | choose columns: TARGET,SOURCE,FSTYPE,OPTIONS,SIZE,USED,AVAIL,UUID,LABEL |
-D | df-style output |
-J / -P / -r | JSON / KEY="value" pairs / raw output — for scripts |
-s | read /etc/fstab instead of current mounts |
-F file | with -s: another file instead of fstab |
-x (--verify) | check fstab; --verbose — detailed; --tab-file file — another file |
Examples
# what is mounted where; which FS a directory is on
findmnt
findmnt -T /var/log
findmnt -n -o FSTYPE,OPTIONS /
# all real disks, df style
findmnt -D -t ext4,xfs
# is something mounted on a directory (for scripts)
mountpoint -q /data && echo mounted
# mount a partition and an image (needs root on a real machine)
mkdir -p /data /mnt/img
mount /dev/sdb1 /data
mount -o loop,ro /srv/disk.img /mnt/img
# an in-memory tmpfs and a bind mount of a directory elsewhere
mount -t tmpfs -o size=100M tmpfs /mnt/tmp
mount --bind /srv/www /var/www
# make an FS read-only on the fly and back
mount -o remount,ro /data
mount -o remount,rw /data
# check fstab BEFORE rebooting (works even without the right to mount)
findmnt --verify
findmnt --verify --verbose --tab-file ./fstab.new
# mount everything from fstab and unmount
mount -a
umount /data
# a loop device by hand
losetup -f --show /srv/disk.img # prints, e.g., /dev/loop0
losetup -l
losetup -d /dev/loop0
Pitfalls
- "target is busy" on
umount. Something is working in the directory — including your own shell if you are standing in it. Leave it (cd /) and find the processes:fuser -vm /data(thepsmiscpackage, not in the sandbox) or by hand:ls -l /proc/*/cwd /proc/*/fd 2>/dev/null | grep /data. Last resort —umount -l. - An error in fstab breaks the boot. A line with a nonexistent UUID and no
nofaildrops the system into emergency mode. After editing, always runfindmnt --verifyandmount -a. mount -fchecks nothing: it reports "successfully mounted" even for a nonexistent device and a wrong type. To check fstab, usefindmnt --verify.- Mounting hides the directory's contents. Files that were in
/databeforemountare not gone, they are just invisible while the FS is mounted. That's how gigabytes "go missing":dudoesn't see them,dfcounts them. - The mount point must exist —
mountdoesn't create it (except with-m). Create the directory from fstab in advance. - Changes don't persist.
mountfrom the command line lasts until reboot; for an FS to be mounted always, you need a line in/etc/fstab. /etc/mtabandmountwith no arguments show the same as/proc/self/mounts;findmntpresents it more clearly and can filter.- In a container (Docker, the IzzyLab sandbox) mounting is forbidden without
--privilegedorCAP_SYS_ADMIN, and there is no/dev/loop*at all: "cannot find an unused loop device". You can inspect an image's contents without mounting viadebugfs(see filesystems).
See also
filesystems · df-du · proc · dd · truncate · capabilities