Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

mount(2)

FieldValue
Numbers (x64 / i386 / arm64-generic / arm32)x64 165 / i386 21 / arm64-generic 40 / arm32 21
Since (kernel)1.0
Man pageman2/mount.2 (cached man-pages-sparse, dated 2021-08-27)
glibc wrapperYes; declared in <sys/mount.h> (installed glibc 2.41)
Aliases coverednone; unmount side: umount / umount2 pointer -> umount2.md
Ledger IDSC-mount

Purpose

mount() is the legacy, string-based filesystem mounting interface. It attaches the filesystem named by source (a device path, a directory or file, or a dummy string) to the location named by target. The call is one of five operations, chosen by testing mountflags in this order: remount (MS_REMOUNT), bind mount (MS_BIND), propagation change (MS_SHARED / MS_PRIVATE / MS_SLAVE / MS_UNBINDABLE), move (MS_MOVE), and otherwise create a new mount. The fd-based successor suite (fsopen, fsconfig, fsmount, move_mount, open_tree, fspick, mount_setattr) has its own pages in this directory; see fsopen.md.

Synopsis

#include <sys/mount.h>
int mount(const char *source, const char *target,
          const char *filesystemtype, unsigned long mountflags,
          const void *data);

Arguments and behavior

ArgumentMeaning
sourceDevice, directory/file to bind, or dummy string (ignored by remount/bind/move/propagation ops)
targetMount point; must be the original target for MS_REMOUNT
filesystemtypeValue from /proc/filesystems (e.g. ext4, tmpfs); ignored except for a new mount
mountflagsOperation selector plus MS_* modifier flags (table below)
dataFilesystem-specific option string (comma-separated); interpreted by each driver

filesystemtype and data are ignored for bind, move, and propagation operations. The magic number MS_MGC_VAL (0xc0ed0000) in the top 16 bits was required before Linux 2.4 and is ignored since.

Flags and constants

ConstantValueMeaningSince (man mount.2 / <sys/mount.h>)
MS_RDONLY1Mount read-only1.0
MS_NOSUID2Ignore suid/sgid bits and file capabilities1.0
MS_NODEV4Disallow device special files1.0
MS_NOEXEC8Disallow program execution1.0
MS_SYNCHRONOUS16Writes are synchronous (was MS_SYNC, renamed 1.1.69)1.0
MS_REMOUNT32Alter flags of an existing mount1.0
MS_MANDLOCK64Permit mandatory locking; since 4.5 needs CAP_SYS_ADMIN + CONFIG_MANDATORY_FILE_LOCKING; deprecated (no-op mand since 5.15 per fsconfig.2)1.0
MS_DIRSYNC128Directory changes synchronous2.5.19
MS_NOSYMFOLLOW256Do not follow symbolic links5.10
MS_NOATIME1024Do not update access times1.0
MS_NODIRATIME2048Do not update directory access times1.0
MS_BIND4096Create bind mount2.4
MS_MOVE8192Move subtree atomically2.4.18
MS_REC16384Recursive bind / recursive propagation change2.4.11
MS_SILENT32768Suppress kernel log warnings (supersedes MS_VERBOSE, 2.4.12)2.6.17
MS_POSIXACL1<<16VFS does not apply the umaskheader
MS_UNBINDABLE1<<17Change to unbindable2.6.15
MS_PRIVATE1<<18Change to private2.6.15
MS_SLAVE1<<19Change to slave2.6.15
MS_SHARED1<<20Change to shared2.6.15
MS_RELATIME1<<21Update atime relative to mtime/ctime2.6.20
MS_I_VERSION1<<23Update inode i_version fieldheader
MS_STRICTATIME1<<24Always perform atime updates2.6.30
MS_LAZYTIME1<<25Update on-disk [acm]times lazily4.0
MS_MGC_VAL0xc0ed0000Magic flag (obsolete; ignored since 2.4)pre-2.4

Per-mount-point flags since 2.4: MS_NODEV, MS_NOEXEC, MS_NOSUID (2.4), MS_NOATIME / MS_NODIRATIME (2.6.16), MS_RELATIME (2.6.20); MS_RDONLY per-mount since 2.6.16. Per-superblock: MS_DIRSYNC, MS_LAZYTIME, MS_MANDLOCK, MS_SILENT, MS_SYNCHRONOUS. MS_KERNMOUNT (1<<22), MS_ACTIVE (1<<30) and MS_NOUSER (1<<31) exist in the header but are kernel-internal. Remount can change only the MS_RMT_MASK set (MS_RDONLY, MS_SYNCHRONOUS, MS_MANDLOCK, MS_I_VERSION, MS_LAZYTIME plus the atime flags); MS_DIRSYNC and MS_SILENT changes are silently ignored. Since 2.6.26, MS_REMOUNT | MS_BIND changes only the per-mount flags. Since 3.17 a remount that names no atime flag preserves the current atime mode.

Mount-lock note: the kernel marks mounts locked (kernel-internal MNT_LOCK_* flags, not exposed in UAPI headers) when properties must not be altered across user namespaces; a remount that would change locked flags fails with EPERM and an unmount of a locked mount fails with EINVAL (man page pointers to mount_namespaces(7)).

Return value and errors

Returns 0 on success, -1 on error with errno set. Filesystem-independent errors include EPERM (missing privilege or locked flag), EBUSY (stacking over an existing mount of the same source/target; remount read-only with open writers), EINVAL (invalid flag combination; move of a shared-parent mount; bind of an unbindable mount; unprivileged-namespace bind without MS_REC), ENODEV (unknown filesystemtype), ENOTBLK (source not a block device), EROFS or EACCES (mounting a read-only filesystem without MS_RDONLY), ELOOP, ENAMETOOLONG, ENOENT, ENOMEM, EFAULT, ENXIO.

Security and notes

Requires CAP_SYS_ADMIN for the mount namespace (owned user namespace rules per mount_namespaces(7)); unprivileged mount namespaces (userns + CLONE_NEWNS) allow restricted mounting, but bind mounts there require MS_REC to avoid revealing submounts. Mount namespaces themselves date to 2.4.19. In containers, prefer the fd-based API: fsopen.md, fsconfig.md, fsmount.md, move_mount.md, open_tree.md, fspick.md, mount_setattr.md. Unmounting is the companion call, see umount2.md; the hub also routes plain umount from overview.md.

References

  • Cached man page: source-cache/man-pages-sparse/man2/mount.2
  • Online: https://man7.org/linux/man-pages/man2/mount.2.html
  • Constants: /usr/include/x86_64-linux-gnu/sys/mount.h; kernel UAPI include/uapi/linux/mount.h (fetched: source-cache/fetched/master-include-uapi-linux-mount.h)
  • Kernel implementation: fs/namespace.c (torvalds/linux master)