This tutorial explains Linux “mount” command, options and its usage with examples.
All files accessible in Unix, or a Unix-style system such as Linux, are arranged in one big tree: the file hierarchy, rooted at /. These files can be spread out over several devices. The mount command attaches a filesystem, located on some device or other, to the file tree. Conversely, the umount command will detach it again.
The standard form of the mount command is:
mount -t type device dir
This tells the kernel to attach the filesystem found on device (which is of type type) at the directory dir. The previous contents (if any), owner, and mode of dir become invisible, and as long as this filesystem remains mounted, the pathname dir refers to the root of the filesystem on device.
If only directory or device is given, for example:
then mount looks for a corresponding mountpoint (and then, if not found, for a corresponding device) entry in the /etc/fstab file, and attempts to mount it.
mount -a [-fFnrsvw] [-t vfstype] [-O optlist]
mount [-fnrsvw] [-o options [,…]] device | dir
mount [-fnrsvw] [-t vfstype] [-o options] device dir
Print a help message.
Mount all filesystems (of the given types) mentioned in fstab.
(Used in conjunction with -a.) Fork off a new incarnation of mount for each device. This will do the mounts on different devices or different NFS servers in parallel. This has the advantage that it is faster; also NFS timeouts go in parallel. A disadvantage is that the mounts are done in undefined order. Thus, you cannot use this option if you want to mount both /usr and /usr/spool.
Causes everything to be done except for the actual system call; if it’s not obvious, this “fakes” mounting the file system. This option is useful in conjunction with the -v flag to determine what the mount command is trying to do. It can also be used to add entries for devices that were mounted earlier with the -n option.
Add the ext2, ext3 and XFS labels in the mount output. Mount must have permission to read the disk device (e.g. be suid root) for this to work. One can set such a label for ext2 or ext3 using the e2label utility, or for XFS using xfs_admin.
Mount without writing in /etc/mtab. This is necessary for example when /etc is on a read-only file system.
Tolerate sloppy mount options rather than failing. This will ignore mount options not supported by a filesystem type. Not all filesystems support this option. This option exists for support of the Linux autofs-based automounter.
Mount the file system read-only. A synonym is -o ro.
Mount the file system read/write. This is the default. A synonym is -o rw.
Mount the partition that has the specified label.
Mount the partition that has the specified uuid. These two options require the file /proc/partitions (present since Linux 2.1.116) to exist.
The argument following the -t is used to indicate the file system type. The file system types which are currently supported are: adfs, affs, autofs, coda, coherent, cramfs, devpts, efs, ext, ext2, ext3, hfs, hpfs, iso9660, jfs, minix, msdos, ncpfs, nfs, ntfs, proc, qnx4, ramfs, reiserfs, romfs, smbfs, sysv, tmpfs, udf, ufs, umsdos, vfat, xenix, xfs, xiafs. Note that coherent, sysv and xenix are equivalent and that xenix and coherent will be removed at some point in the future — use sysv instead. Since kernel version 2.1.21 the types ext and xiafs do not exist anymore.
For most types all the mount program has to do is issue a simple mount(2) system call, and no detailed knowledge of the filesystem type is required. For a few types however (like nfs, smbfs, ncpfs) ad hoc code is necessary. The nfs ad hoc code is built in, but smbfs and ncpfs have a separate mount program. In order to make it possible to treat all types in a uniform way, mount will execute the program /sbin/mount.TYPE (if that exists) when called with type TYPE. Since various versions of the smbmount program have different calling conventions, /sbin/mount.smb may have to be a shell script that sets up the desired call.
The type iso9660 is the default. If no -t option is given, or if the auto type is specified, the superblock is probed for the filesystem type (adfs, bfs, cramfs, ext, ext2, ext3, hfs, hpfs, iso9660, jfs, minix, ntfs, qnx4, reiserfs, romfs, udf, ufs, vxfs, xfs, xiafs are supported). If this probe fails, mount will try to read the file /etc/filesystems, or, if that does not exist, /proc/filesystems. All of the filesystem types listed there will be tried, except for those that are labeled “nodev” (e.g., devpts, proc and nfs). If /etc/filesystems ends in a line with a single * only, mount will read /proc/filesystems afterwards.
The auto type may be useful for user-mounted floppies. Creating a file /etc/filesystems can be useful to change the probe order (e.g., to try vfat before msdos) or if you use a kernel module autoloader. Warning: the probing uses a heuristic (the presence of appropriate `magic’), and could recognize the wrong filesystem type, possibly with catastrophic consequences. If your data is valuable, don’t ask mount to guess.
More than one type may be specified in a comma separated list. The list of file system types can be prefixed with no to specify the file system types on which no action should be taken. (This can be meaningful with the -a option.)
For example, the command:
mount -a -t nomsdos,ext
mounts all file systems except those of type msdos and ext.
Used in conjunction with -a, to limit the set of filesystems to which the -a is applied. Like -t in this regard except that it is useless except in the context of -a. For example, the command:
mount -a -O no_netdev
mounts all file systems except those which have the option _netdev specified in the options field in the /etc/fstab file.
It is different from -t in that each option is matched exactly; a leading no at the beginning of one option does not negate the rest.
The -t and -O options are cumulative in effect; that is, the command
mount -a -t ext2 -O _netdev
mounts all ext2 filesystems with the _netdev option, not all filesystems that are either ext2 or have the _netdev option specified.
Options are specified with a -o flag followed by a comma separated string of options. Some of these options are only useful when they appear in the /etc/fstab file. The following options apply to any file system that is being mounted (but not every file system actually honors them – e.g., the sync option today has effect only for ext2, ext3 and ufs):
All I/O to the file system should be done asynchronously.
Update inode access time for each access. This is the default.
Can be mounted with the -a option.
Use default options: rw, suid, dev, exec, auto, nouser, and async.
Interpret character or block special devices on the file system.
Permit execution of binaries.
The filesystem resides on a device that requires network access (used to prevent the system from attempting to mount these filesystems until the network has been enabled on the system).
Do not update inode access times on this file system (e.g, for faster access on the news spool to speed up news servers).
Can only be mounted explicitly (i.e., the -a option will not cause the file system to be mounted).
Do not interpret character or block special devices on the file system.
Do not allow execution of any binaries on the mounted file system. This option might be useful for a server that has file systems containing binaries for architectures other than its own.
Do not allow set-user-identifier or set-group-identifier bits to take effect. (This seems safe, but is in fact rather unsafe if you have suidperl(1) installed.)
Forbid an ordinary (i.e., non-root) user to mount the file system. This is the default.
Attempt to remount an already-mounted file system. This is commonly used to change the mount flags for a file system, especially to make a readonly file system writeable. It does not change device or mount point.
Mount the file system read-only.
Mount the file system read-write.
Allow set-user-identifier or set-group-identifier bits to take effect.
All I/O to the file system should be done synchronously.
All directory updates within the file system should be done synchronously. This affects the following system calls: creat, link, unlink, symlink, mkdir, rmdir, mknod and rename.
Allow an ordinary user to mount the file system. The name of the mounting user is written to mtab so that he can unmount the file system again. This option implies the options noexec, nosuid, and nodev (unless overridden by subsequent options, as in the option line user,exec,dev,suid).
Allow every user to mount and unmount the file system. This option implies the options noexec, nosuid, and nodev (unless overridden by subsequent options, as in the option line users,exec,dev,suid).
Remount a subtree somewhere else (so that its contents are available in both places).
Move a subtree to some other place.
The Files /etc/fstab, /etc/mtab And /proc/mounts
The file /etc/fstab may contain lines describing what devices are usually mounted where, using which options.
mount -a [-t type] [-O optlist]
causes all filesystems mentioned in fstab (of the proper type and/or having or not having the proper options) to be mounted as indicated, except for those whose line contains the noauto keyword. This command would typically be included in a boot script. Adding the -F option will make mount fork, so that the filesystems are mounted simultaneously.
When mounting a filesystem mentioned in fstab or mtab, it suffices to give only the device, or only the mount point. The programs mount and umount maintain a list of currently mounted filesystems in the file /etc/mtab. If no arguments are given to mount, this list is printed.
The mount program does not read the /etc/fstab file if both device (or LABEL/UUID) and dir are specified. For example:
mount /dev/foo /dir
If you want to override mount options from /etc/fstab, you have to use:
mount device|dir -o
and then the mount options from command line will be appended to the list of options from /etc/fstab. If there are duplicate options, the usual behavior is that the one occuring last in the command will be used.
When the proc filesystem is mounted (at /proc, for instance), the files /etc/mtab and /proc/mounts have very similar contents. The former has somewhat more information, such as the mount options used, but is not necessarily up-to-date (compare with the -n option below). It is possible to replace /etc/mtab by a symbolic link to /proc/mounts. When you have very large numbers of mounts things will be much faster with the symlink, but some information is lost that way; in particular, the “user” option will fail.
1. Mount a CD-ROM
The device file for CD would exist under /dev directory.
# mount -t iso9660 -o ro /dev/cdrom /mnt
In the above example, the option “-o ro” indicates that the cdrom should be mounted with read-only access.
Note: Make sure that the destination directory (in the above example, /mnt) exist before you execute the mount command.
2. View All Mounts
After you execute mount a partition or filesystem, execute the mount command without any arguments to view all the mounts.
# mount /dev/sda5 on / type ext4 (rw,errors=remount-ro) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) none on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620) tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755) none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880) none on /run/shm type tmpfs (rw,nosuid,nodev) /dev/sda6 on /mydata type ext2 (rw) /dev/sda7 on /backup type vfat (rw) gvfs-fuse-daemon on /home/mk/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev,user=bala)
3. Mount all the filesystem mentioned in /etc/fstab
The filesystems listed in /etc/fstab gets mounted during booting process.
Example /etc/fstab file entries:
# cat /etc/fstab # proc /proc proc nodev,noexec,nosuid 0 0 # / was on /dev/sda5 during installation /dev/sda5 / ext4 errors=remount-ro 0 1 # /mydata was on /dev/sda6 during installation /dev/sda6 /mydata ext2 defaults 0 2 # /backup was on /dev/sda7 during installation /dev/sda7 /backup vfat defaults 0 3
Execute mount command with -a option to mount all the /etc/fstab entries.
# mount -a # mount /dev/sda5 on / type ext4 (rw,errors=remount-ro) proc on /proc type proc (rw,noexec,nosuid,nodev) sysfs on /sys type sysfs (rw,noexec,nosuid,nodev) none on /sys/fs/fuse/connections type fusectl (rw) none on /sys/kernel/debug type debugfs (rw) none on /sys/kernel/security type securityfs (rw) udev on /dev type devtmpfs (rw,mode=0755) devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620) tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755) none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242
4. Mount only a specific filesystem from /etc/fstab
When you pass only the directory name to mount, it looks for mount point entries, if not found, then search continuous for a device in /etc/fstab and gets mounted.
# mount | grep /mydata # cat /etc/fstab | grep mydata ##########/mydata was on /dev/sda6 during installation########## /dev/sda6 /mydata ext2 defaults 0 2
As seen above, /mydata directory is not a mountpoint, but it is present in /etc/fstab.
# mount /mydata # mount | grep /mydata /dev/sda6 on /mydata type ext2 (rw)
If you execute the same again, you would get the error message as follows:
# mount /mydata mount: /dev/sda6 already mounted or /mydata busy mount: according to mtab, /dev/sda6 is already mounted on /mydata
Here you may also pass the device name instead of directory name (to be picked up from /etc/fstab file).
# mount /dev/sda6
Note that the files /etc/mtab and /proc/mounts contents would be similar.
5. View all mounted partitions of specific type
It is possible to list only the specific type of filesystem mounted using the option -l with -t as shown below:
# mount -l -t ext2 /dev/sda6 on /mydata type ext2 (rw) # mount -l -t ext4 /dev/sda5 on / type ext4 (rw,errors=remount-ro)
6. Bind mount points to a new directory
The mountpoint can be binded to a new directory. So that you would be able to access the contents of a filesystem via more than one mountpoints at the same time.
# mount -B /mydata /mnt
Now the bind is done and you might verify it as follows,
# mount | grep /mydata /dev/sda6 on /mydata type ext2 (rw) /mydata on /mnt type none (rw,bind)
As seen above the bind is done properly. So when you do modification in filesystem in one place, you can see those reflection of it in other mount point too.
7. Access contents from new mount point
Mount allows you to access the contents of a mount point from a new mount point. Its nothing but move a mounted tree to another place.
# mount -M /myfol /mnt/
Once its done, you cant use the old mount point as its moved to a new mount point and this can be verified as shown below:
# mount | grep /myfol # mount | grep /mnt /dev/sda5 on /mnt type ext2 (rw)
8. Mount without writing entry into /etc/mtab
During read only mount of /etc/, the /etc/mtab file entries cannot be modified by mount command. To mount without writing into /etc/mtab, use the option -n as shown
# mount -n /dev/sda6 /mydata
You cannot see any entry for this /mydata in mount command output and as well from /etc/mtab file as follows:
# mount | grep /mydata # cat /etc/mtab | grep /mydata
9. Mount filesystem with read or read/write access
To mount partition as read only, use -r option which is synonym to -o ro.
# mount /dev/sda5 /mydata -r # mount | grep /mydata /dev/sda5 on /mydata type ext4 (ro)
ext3 and ext4 filesystem allows you to do write operation when the filesystem is dirty. So, you may have to use “ro,noload” to prevent these kind of write operation.
# mount /dev/sda5 /mydata -t ext4 -o ro -o noload # mount | grep /mydata /dev/sda5 on /mydata type ext4 (ro,noload)
Similiarly to mount a partition with read/write access, use -w option which is same as “-o rw” (i.e : default).
10. Remount the mounted filesystem
In order to mount the already mounted filesystem, use remount option.
The /myfol mount point is going to be remounted with read/write access from read access as shown below:
# mount | grep /myfol /dev/sda5 on /myfol type ext4 (ro,noload) # mount -o remount,rw /myfol # mount | grep /myfol /dev/sda5 on /myfol type ext4 (rw)
11. Mount an iso image into a directory
The iso image can be mounted as shown below:
# mount -t iso9660 -o loop folders.iso /mnt # cd /mnt # ls dir1/ dir2/ dir3/
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.