Mounting File Systems
To access a file system's contents, you need to attach its block device to a mount point in the directory hierarchy. Any directory can be used to function as a mount point.
Typically, you create a directory for a mount point. If you use an existing directory, the contents remain hidden until you unmount the overlying file system.
About the mount Command
You use the mount command to attach the device containing the file system to the mount point as follows:
sudo mount [options] device mount_point
The device can be mounted by referencing its name, UUID, or
label. For example, to mount the file system that was created in
the previous section to /var/projects
, any of
the following commands can be used after you create the
directory by running the following commands:
sudo mkdir /var/projects
sudo mount /dev/sdb1 /var/projects
sudo mount UUID="ad8113d7-b279-4da8-b6e4-cfba045f66ff" /var/projects
sudo mount LABEL="Projects" /var/projects
Issuing the mount command by itself displays all the mounted file systems. In the following example, an extract of the command's output indicates the following:
-
/dev/sdb1
with anext4
file system is mounted on/var/projects
for both reading and writing -
/dev/mapper/vg_host01-lv_root
, an LVM logical volume also with anext4
file system, is mounted on/
for both reading and writing:
sudo mount
The output of the previous command would be similar to the following:
/dev/sdb1 on /var/projects type ext4 (rw) /dev/mapper/vg_host01-lv_root on / type ext4 (rw) ...
Or, you can use the cat /proc/mounts command to display information about mounted file systems.
The df -h command displays information about file systems and their use of disk space:
Filesystem Size Used Avail Use% Mounted on ... /dev/sda3 46G 18G 29G 39% / /dev/sda2 795M 452M 344M 57% /boot /dev/sda1 100M 5.7M 95M 6% /boot/efi ...
To attach or bind a block device at several mount points, use the mount -B command.
You can also remount part of a directory hierarchy, which need
not be a complete file system, somewhere else. For example, you
would use the following command to mount
/var/projects/project1
on
/mnt
, for example:
sudo mount -B /var/projects/project1 /mnt
Each directory hierarchy acts as a mirror of the other. The same files are accessible in either location. However, any submounts aren't replicated. These mirrors don't provide data redundancy.
To mount a file over another file, you would use the following command:
sudo touch /mnt/foo
sudo mount -B /etc/hosts /mnt/foo
In the previous example, the /etc/hosts
and /mnt/foo
mount
points represent the same file. The existing file that acts as a mount point isn't accessible
until you unmount the overlying file.
To include submounts in the mirror, use the -R option to create a recursive bind.
When you use the -B or -R option, the file system mount options remain the same as those for the original mount point. To change, the mount options, use a separate remount command, for example:
sudo mount -o remount,ro /mnt/foo
You can mark the submounts in a mount point as being shared, private, or secondary. You can specify the following options:
- mount --make-shared mount_point
-
Any mounts or unmounts under the specified mount point propagate to any mirrors that you create, and this mount hierarchy reflects mounts or unmount changes that you make to other mirrors.
- mount --make-private mount_point
-
Any mounts or unmounts under the specified mount point don't propagate to other mirrors, nor does this mount hierarchy reflect mounts or unmount changes that you make to other mirrors.
- mount --make-slave mount_point
-
Any mounts or unmounts under the specified mount point don't propagate to other mirrors, but this mount hierarchy does reflect mounts or unmount changes that you make to other mirrors.
To prevent a mount from being mirrored by using the -B or -R options, mark its mount point as being unbindable:
sudo mount --make-unbindable mount_point
To move a mounted file system, directory hierarchy, or file between mount points, use the -M option, for example:
sudo touch /mnt/foo
sudo mount -M /mnt/foo /mnt/bar
To unmount a file system, use the umount command:
sudo umount /var/projects
Or, you can specify the block device if it's mounted on only one mount point.
For more information, see the mount(8)
and
umount(8)
manual pages.
Using More Options of the mount Command
You can identify the mount command behavior by using the -o option to specify options in a comma-separated list. Some of these options are as follows:
Note:
These options can also be entered in the
/etc/fstab
file.
-
auto
-
Causes the file system to be mounted automatically by using the mount -a command.
-
exec
-
Causes the execution of any binary files in the file system.
-
loop
-
Uses a loop device (
/dev/loop*
) to mount a file that contains a file system image. See Mounting a File That Contains a File System Image, Creating a File System on a File Within Another File System, and thelosetup(8)
manual page.Note:
The default number of available loop devices is 8. You can use the kernel boot parameter
max_loop=N
to configure up to 255 devices. Or, add the following entry to/etc/modprobe.conf
:options loop max_loop=N
In the previous example, N is the number of loop devices that you require (from 0 to 255), and then reboot the system.
-
noauto
-
Prevents the file system from being mounted automatically when mount -a is issued.
-
noexec
-
Prevents the execution of any binary files in the file system.
-
nouser
-
Prevents any user other than the
root
user from mounting or unmounting the file system. -
remount
-
Remounts the file system if it's already mounted. You would typically combine this option with another option such as
ro
orrw
to change the behavior of a mounted file system. -
ro
-
Mounts a file system as read-only.
-
rw
-
Mounts a file system for reading and writing.
-
user
-
Allows any user to mount or unmount the file system.
The following examples show different ways to use the mount -o command syntax.
-
Mount the
/dev/sdd1
file system as/test
with read-only access and grant only theroot
user to mount or unmount the file system:sudo mount -o nouser,ro /dev/sdd1 /test
-
Mount an ISO image file on
/mount/cdrom
with read-only access by using the loop device:sudo mount -o ro,loop ./Linux-Server-dvd.iso /media/cdrom
-
Remount the
/test
file system with both read and write access, and prohibit the execution of any binary files that are in the file system:sudo mount -o remount,rw,noexec /test
Mounting a File That Contains a File System Image
A loop device lets you access a file as a block device. For example, you can mount a file
that contains a DVD ISO image on the directory mount point /ISO
as follows:
sudo mount -t iso9660 -o ro,loop /var/ISO_files/V33411-01.iso /ISO
If required, create a permanent entry for the file system in the
/etc/fstab
file, for example:
/var/ISO_files/V33411-01.iso /ISO iso9660 ro,loop 0 0
About the File System Mount Table
The /etc/fstab
file contains the file system mount table, which provides
all the information that the mount command requires to mount block
devices or implement binding of mounts. If you add a file system, you must create the
appropriate entry in the /etc/fstab
file to ensure that the file system is
mounted at boot time. The following are typical entries from this file:
/dev/sda1 /boot ext4 defaults 1 2 /dev/sda2 / ext4 defaults 1 1 /dev/sda3 swap swap defaults 0 0
The descriptions of each field in the previous output are as follows:
-
The first field indicates the device to mount, which is specified by the device name, UUID, or device label, or the specification of a remote file system. A UUID or device label is preferable to a device name if the device name could change, for example:
LABEL=Projects /var/projects ext4 defaults 1 2
Note that the first field specifies the path of the file system, directory hierarchy, or file that's to be mounted on the mount point specified by the second field. The third and fourth fields are specified as
none
andbind
. -
The second field is either the mount point for a file system or
swap
to indicate a swap partition. The mount point must be a path to either a file or a directory. -
The third field is the file system type, such as
ext4
orswap
. -
The fourth field specifies any mount options.
-
The fifth column specifies whether the dump command dumps the file system (
1
) or not (0
). -
The sixth column identifies the order by which the fsck command performs a file system check at boot time. The root file system has the value
1
, while other file systems have2
. A value of0
skips checking, as is appropriate for swap, for file systems that aren't mounted at boot time, and for binding of existing mounts.
For bind mounts, only the first four fields are specified, for example:
path mount_point none bind
For more information, see the fstab(5)
manual
page.