Disks and filesystems
mount
Attach a filesystem (a disk partition, image, network share or tmpfs) to a directory, or list what is mounted.
mount [-t TYPE] [-o OPTIONS] SOURCE TARGET | mount [-a] | umount TARGET
Linux has one directory tree. A filesystem on a disk, USB stick, image file or network server
becomes visible only when it is mounted on a directory, its mount point. Whatever that
directory contained before is hidden until the filesystem is unmounted again with umount.
mount SOURCE TARGET does the attaching; the type is usually detected, and -o passes options
such as ro (read-only), noexec or noatime. /etc/fstab lists what to mount at boot, and
mount -a mounts everything in it that is not mounted yet. Mounting and unmounting need root,
except for fstab entries marked user.
mount with no arguments lists every mount, but the output is long and hard to read; findmnt
shows the same information as a tree, and lsblk shows which devices exist to be mounted.
The examples that mount something are not run: this site's sandbox is a container without the privileges to mount, and it has no spare disks. The ones that do run show listing, the permission error, and a dry run of an fstab file.
Examples
List mounts of one type
mount -t devpts,mqueue
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666) mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
-t with no source and target filters the listing by type. Each line reads
SOURCE on TARGET type TYPE (OPTIONS).
Mounting as a normal user
mkdir -p /tmp/ram
mount -t tmpfs tmpfs /tmp/ram
mount: /tmp/ram: must be superuser to use mount.
dmesg(1) may have more information after failed mount system call. Mounting needs root. mount exits with 32, its code for "mount failure".
Unmounting as a normal user
umount /dev/shm
umount: /dev/shm: must be superuser to unmount.
Unmounting needs root too. On a real system, as root, the error you will meet most is
"target is busy": some process has a file open or its working directory inside the mount.
lsof +f -- /mnt/data or fuser -vm /mnt/data finds it.
Dry-run an fstab with --fake
/dev/sdb1 /data ext4 defaults,noatime 0 2
tmpfs /tmp tmpfs size=2G,mode=1777 0 0
server:/export/home /mnt/home nfs defaults,_netdev 0 0
/dev/sdc1 /backup ext4 noauto,nofail 0 2
mount --fstab fstab -a --fake -v
/data : successfully mounted /tmp : successfully mounted /mnt/home : successfully mounted /backup : ignored
-a processes every fstab entry, --fake (-f) goes through the motions without calling
the kernel, and -v reports each one. --fstab FILE reads an alternative file instead of
/etc/fstab. /backup is ignored because of noauto: it is mounted only on request.
Mount a disk partition
sudo mkdir -p /mnt/data
sudo mount /dev/sdb1 /mnt/data
Not run in the sandbox: needs root and a real disk; the sandbox has neither. No output is shown because none was captured.
The filesystem type is detected automatically. Find the partition name with lsblk
first. sudo umount /mnt/data detaches it again (note the spelling: umount, no n).
Mount read-only and without executables
sudo mount -o ro,noexec,nosuid /dev/sdb1 /mnt/data
Not run in the sandbox: needs root and a real disk. No output is shown because none was captured.
ro prevents writes (good for inspecting a disk from another machine or recovering data),
noexec refuses to run programs from it, nosuid ignores setuid bits. Options are
comma-separated without spaces. To change options on something already mounted, add
remount: mount -o remount,rw / is the classic fix for a root filesystem that came up
read-only.
Mount a disk image or ISO file
sudo mount -o loop,ro debian.iso /mnt/iso
Not run in the sandbox: needs root and loop devices, which the sandbox does not provide. No output is shown because none was captured.
-o loop attaches the file to a loop device first, so a file containing a filesystem can be
mounted like a disk. Recent mount versions add loop automatically for regular files.
A filesystem in memory
sudo mount -t tmpfs -o size=512M,mode=1777 tmpfs /mnt/scratch
Not run in the sandbox: needs root (CAP_SYS_ADMIN), which the sandbox does not grant. No output is shown because none was captured.
tmpfs keeps files in RAM (and swap), so it is fast and empties on unmount or reboot. size
caps it. The source name tmpfs is arbitrary but conventional.
Make a directory appear in a second place
sudo mount --bind /srv/site/uploads /var/www/uploads
Not run in the sandbox: needs root (CAP_SYS_ADMIN). No output is shown because none was captured.
A bind mount shows the same directory at another path, without symlinks. It is how containers and chroots get selected host directories.
Common pitfalls
Mounting over a non-empty directory hides its contents until you unmount; the files are not lost, just invisible. This confuses people who mount over
/homeor/var.Device names like
/dev/sdb1can change between boots. In/etc/fstabuseUUID=...(fromlsblk -f) and addnofailfor disks that may be absent, or the boot can stop at an emergency shell.After editing
/etc/fstab, test withsudo findmnt --verifyandsudo mount -abefore rebooting.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
BSD/macOS
mountuses-tand-otoo, but filesystem type names and options differ, and the fstab format has small differences. On macOS, removable disks are usually mounted withdiskutil mount.
Options worth knowing
A selection, described in our own words. For the full list, see mount(8) or mount --help.
| Option | What it does |
|---|---|
-t, --types TYPE | Filesystem type, or a list of types to filter the listing. |
-o, --options OPTIONS | Comma-separated mount options (ro, noexec, loop, remount, ...). |
-a, --all | Mount everything in fstab that is not mounted yet. |
-r, --read-only | Mount read-only, like -o ro. |
-w, --rw | Mount read-write (the default). |
-f, --fake | Do everything except the actual mount (dry run). |
-v, --verbose | Describe each action. |
-l, --show-labels | When listing, include filesystem labels. |
-L, --label LABEL | Mount the filesystem with this label. |
-U, --uuid UUID | Mount the filesystem with this UUID. |
-T, --fstab PATH | Use an alternative fstab file. |
-B, --bind | Bind-mount a directory at a second location. |
-R, --rbind | Bind-mount a directory and everything mounted below it. |
-M, --move | Move a mount to another mount point. |
-n, --no-mtab | Do not write to /etc/mtab. |
Related
findmntShow mounted filesystems as a tree, or look up which filesystem a path is on and how it is mounted.lsblkList block devices (disks, partitions, LVM volumes, loop devices) as a tree, with sizes and mount points.dfReport how much space is used and free on mounted filesystems.duShow how much disk space files and directories use, totalled per directory.lsofList open files and the processes holding them, including network sockets, deleted files and working directories.