19.2. Mounting a File System
To attach a certain file system, use the
mount
command in the following form:
mount [option…] device directory
$ mount [option…] device directory
The device can be identified by:
- a full path to a block device: for example,
/dev/sda3
- a universally unique identifier (UUID): for example,
UUID=34795a28-ca6d-4fd8-a347-73671d0c19cb
- a volume label: for example,
LABEL=home
Note that while a file system is mounted, the original content of the directory is not accessible.
Important
Linux does not prevent a user from mounting a file system to a directory with a file system already attached to it. To determine whether a particular directory serves as a mount point, run the
findmnt
utility with the directory as its argument and verify the exit code:
findmnt directory; echo $?
findmnt directory; echo $?
If no file system is attached to the directory, the given command returns
1
.
When you run the
mount
command without all required information, that is without the device name, the target directory, or the file system type, the mount
reads the contents of the /etc/fstab
file to check if the given file system is listed. The /etc/fstab
file contains a list of device names and the directories in which the selected file systems are set to be mounted as well as the file system type and mount options. Therefore, when mounting a file system that is specified in /etc/fstab
, you can choose one of the following options:
mount [option…] directory mount [option…] device
mount [option…] directory
mount [option…] device
Note that permissions are required to mount the file systems unless the command is run as
root
(see Section 19.2.2, “Specifying the Mount Options”).
Note
To determine the UUID and—if the device uses it—the label of a particular device, use the
blkid
command in the following form:
blkid device
blkid device
For example, to display information about
/dev/sda3
:
blkid /dev/sda3
# blkid /dev/sda3
/dev/sda3: LABEL="home" UUID="34795a28-ca6d-4fd8-a347-73671d0c19cb" TYPE="ext3"
19.2.1. Specifying the File System Type
In most cases,
mount
detects the file system automatically. However, there are certain file systems, such as NFS
(Network File System) or CIFS
(Common Internet File System), that are not recognized, and need to be specified manually. To specify the file system type, use the mount
command in the following form:
mount -t type device directory
$ mount -t type device directory
Table 19.1, “Common File System Types” provides a list of common file system types that can be used with the
mount
command. For a complete list of all available file system types, see the section called “Manual Page Documentation”.
Type | Description |
---|---|
ext2 | The ext2 file system. |
ext3 | The ext3 file system. |
ext4 | The ext4 file system. |
btrfs | The btrfs file system. |
xfs | The xfs file system. |
iso9660 | The ISO 9660 file system. It is commonly used by optical media, typically CDs. |
nfs | The NFS file system. It is commonly used to access files over the network. |
nfs4 | The NFSv4 file system. It is commonly used to access files over the network. |
udf | The UDF file system. It is commonly used by optical media, typically DVDs. |
vfat | The FAT file system. It is commonly used on machines that are running the Windows operating system, and on certain digital media such as USB flash drives or floppy disks. |
See Example 19.2, “Mounting a USB Flash Drive” for an example usage.
Example 19.2. Mounting a USB Flash Drive
Older USB flash drives often use the FAT file system. Assuming that such drive uses the
/dev/sdc1
device and that the /media/flashdisk/
directory exists, mount it to this directory by typing the following at a shell prompt as root
:
~]# mount -t vfat /dev/sdc1 /media/flashdisk
~]# mount -t vfat /dev/sdc1 /media/flashdisk
19.2.2. Specifying the Mount Options
To specify additional mount options, use the command in the following form:
mount -o options device directory
mount -o options device directory
When supplying multiple options, do not insert a space after a comma, or
mount
interprets incorrectly the values following spaces as additional parameters.
Table 19.2, “Common Mount Options” provides a list of common mount options. For a complete list of all available options, consult the relevant manual page as referred to in the section called “Manual Page Documentation”.
Option | Description |
---|---|
async | Allows the asynchronous input/output operations on the file system. |
auto | Allows the file system to be mounted automatically using the mount -a command. |
defaults | Provides an alias for async,auto,dev,exec,nouser,rw,suid . |
exec | Allows the execution of binary files on the particular file system. |
loop | Mounts an image as a loop device. |
noauto | Default behavior disallows the automatic mount of the file system using the mount -a command. |
noexec | Disallows the execution of binary files on the particular file system. |
nouser | Disallows an ordinary user (that is, other than root ) to mount and unmount the file system. |
remount | Remounts the file system in case it is already mounted. |
ro | Mounts the file system for reading only. |
rw | Mounts the file system for both reading and writing. |
user | Allows an ordinary user (that is, other than root ) to mount and unmount the file system. |
See Example 19.3, “Mounting an ISO Image” for an example usage.
Example 19.3. Mounting an ISO Image
An ISO image (or a disk image in general) can be mounted by using the loop device. Assuming that the ISO image of the Fedora 14 installation disc is present in the current working directory and that the
/media/cdrom/
directory exists, mount the image to this directory by running the following command:
mount -o ro,loop Fedora-14-x86_64-Live-Desktop.iso /media/cdrom
# mount -o ro,loop Fedora-14-x86_64-Live-Desktop.iso /media/cdrom
Note that ISO 9660 is by design a read-only file system.
19.2.3. Sharing Mounts
Occasionally, certain system administration tasks require access to the same file system from more than one place in the directory tree (for example, when preparing a chroot environment). This is possible, and Linux allows you to mount the same file system to as many directories as necessary. Additionally, the
mount
command implements the --bind
option that provides a means for duplicating certain mounts. Its usage is as follows:
mount --bind old_directory new_directory
$ mount --bind old_directory new_directory
Although this command allows a user to access the file system from both places, it does not apply on the file systems that are mounted within the original directory. To include these mounts as well, use the following command:
mount --rbind old_directory new_directory
$ mount --rbind old_directory new_directory
Additionally, to provide as much flexibility as possible, Red Hat Enterprise Linux 7 implements the functionality known as shared subtrees. This feature allows the use of the following four mount types:
- Shared Mount
- A shared mount allows the creation of an exact replica of a given mount point. When a mount point is marked as a shared mount, any mount within the original mount point is reflected in it, and vice versa. To change the type of a mount point to a shared mount, type the following at a shell prompt:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --make-shared mount_point
$ mount --make-shared mount_point
Alternatively, to change the mount type for the selected mount point and all mount points under it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --make-rshared mount_point
$ mount --make-rshared mount_point
See Example 19.4, “Creating a Shared Mount Point” for an example usage. - Slave Mount
- A slave mount allows the creation of a limited duplicate of a given mount point. When a mount point is marked as a slave mount, any mount within the original mount point is reflected in it, but no mount within a slave mount is reflected in its original. To change the type of a mount point to a slave mount, type the following at a shell prompt:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --make-slave mount_point
mount --make-slave mount_point
Alternatively, it is possible to change the mount type for the selected mount point and all mount points under it by typing:Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --make-rslave mount_point
mount --make-rslave mount_point
See Example 19.5, “Creating a Slave Mount Point” for an example usage.Example 19.5. Creating a Slave Mount Point
This example shows how to get the content of the/media/
directory to appear in/mnt/
as well, but without any mounts in the/mnt/
directory to be reflected in/media/
. Asroot
, first mark the/media/
directory as shared:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ~]# mount --bind /media /media ~]# mount --make-shared /media
~]# mount --bind /media /media ~]# mount --make-shared /media
Then create its duplicate in/mnt/
, but mark it as "slave":Copy to Clipboard Copied! Toggle word wrap Toggle overflow ~]# mount --bind /media /mnt ~]# mount --make-slave /mnt
~]# mount --bind /media /mnt ~]# mount --make-slave /mnt
Now verify that a mount within/media/
also appears in/mnt/
. For example, if the CD-ROM drive contains non-empty media and the/media/cdrom/
directory exists, run the following commands:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ~]# mount /dev/cdrom /media/cdrom ~]# ls /media/cdrom EFI GPL isolinux LiveOS ~]# ls /mnt/cdrom EFI GPL isolinux LiveOS
~]# mount /dev/cdrom /media/cdrom ~]# ls /media/cdrom EFI GPL isolinux LiveOS ~]# ls /mnt/cdrom EFI GPL isolinux LiveOS
Also verify that file systems mounted in the/mnt/
directory are not reflected in/media/
. For instance, if a non-empty USB flash drive that uses the/dev/sdc1
device is plugged in and the/mnt/flashdisk/
directory is present, type:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ~]# mount /dev/sdc1 /mnt/flashdisk ~]# ls /media/flashdisk ~]# ls /mnt/flashdisk en-US publican.cfg
~]# mount /dev/sdc1 /mnt/flashdisk ~]# ls /media/flashdisk ~]# ls /mnt/flashdisk en-US publican.cfg
- Private Mount
- A private mount is the default type of mount, and unlike a shared or slave mount, it does not receive or forward any propagation events. To explicitly mark a mount point as a private mount, type the following at a shell prompt:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --make-private mount_point
mount --make-private mount_point
Alternatively, it is possible to change the mount type for the selected mount point and all mount points under it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --make-rprivate mount_point
mount --make-rprivate mount_point
See Example 19.6, “Creating a Private Mount Point” for an example usage.Example 19.6. Creating a Private Mount Point
Taking into account the scenario in Example 19.4, “Creating a Shared Mount Point”, assume that a shared mount point has been previously created by using the following commands asroot
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ~]# mount --bind /media /media ~]# mount --make-shared /media ~]# mount --bind /media /mnt
~]# mount --bind /media /media ~]# mount --make-shared /media ~]# mount --bind /media /mnt
To mark the/mnt/
directory as private, type:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ~]# mount --make-private /mnt
~]# mount --make-private /mnt
It is now possible to verify that none of the mounts within/media/
appears in/mnt/
. For example, if the CD-ROM drives contains non-empty media and the/media/cdrom/
directory exists, run the following commands:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ~]# mount /dev/cdrom /media/cdrom ~]# ls /media/cdrom EFI GPL isolinux LiveOS ~]# ls /mnt/cdrom ~]#
~]# mount /dev/cdrom /media/cdrom ~]# ls /media/cdrom EFI GPL isolinux LiveOS ~]# ls /mnt/cdrom ~]#
It is also possible to verify that file systems mounted in the/mnt/
directory are not reflected in/media/
. For instance, if a non-empty USB flash drive that uses the/dev/sdc1
device is plugged in and the/mnt/flashdisk/
directory is present, type:Copy to Clipboard Copied! Toggle word wrap Toggle overflow ~]# mount /dev/sdc1 /mnt/flashdisk ~]# ls /media/flashdisk ~]# ls /mnt/flashdisk en-US publican.cfg
~]# mount /dev/sdc1 /mnt/flashdisk ~]# ls /media/flashdisk ~]# ls /mnt/flashdisk en-US publican.cfg
- Unbindable Mount
- In order to prevent a given mount point from being duplicated whatsoever, an unbindable mount is used. To change the type of a mount point to an unbindable mount, type the following at a shell prompt:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --make-unbindable mount_point
mount --make-unbindable mount_point
Alternatively, it is possible to change the mount type for the selected mount point and all mount points under it:Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --make-runbindable mount_point
mount --make-runbindable mount_point
See Example 19.7, “Creating an Unbindable Mount Point” for an example usage.Example 19.7. Creating an Unbindable Mount Point
To prevent the/media/
directory from being shared, asroot
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --bind /media /media mount --make-unbindable /media
# mount --bind /media /media # mount --make-unbindable /media
This way, any subsequent attempt to make a duplicate of this mount fails with an error:Copy to Clipboard Copied! Toggle word wrap Toggle overflow mount --bind /media /mnt
# mount --bind /media /mnt mount: wrong fs type, bad option, bad superblock on /media, missing codepage or helper program, or other error In some cases useful info is found in syslog - try dmesg | tail or so
19.2.4. Moving a Mount Point
To change the directory in which a file system is mounted, use the following command:
mount --move old_directory new_directory
# mount --move old_directory new_directory
See Example 19.8, “Moving an Existing NFS Mount Point” for an example usage.
Example 19.8. Moving an Existing NFS Mount Point
An NFS storage contains user directories and is already mounted in
/mnt/userdirs/
. As root
, move this mount point to /home
by using the following command:
mount --move /mnt/userdirs /home
# mount --move /mnt/userdirs /home
To verify the mount point has been moved, list the content of both directories:
ls /mnt/userdirs ls /home
# ls /mnt/userdirs
# ls /home
jill joe
19.2.5. Setting Read-only Permissions for root
Sometimes, you need to mount the root file system with read-only permissions. Example use cases include enhancing security or ensuring data integrity after an unexpected system power-off.
19.2.5.1. Configuring root
to Mount with Read-only Permissions on Boot
- In the
/etc/sysconfig/readonly-root
file, changeREADONLY
toyes
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Set to 'yes' to mount the file systems as read-only.
# Set to 'yes' to mount the file systems as read-only. READONLY=yes [output truncated]
- Change
defaults
toro
in the root entry (/
) in the/etc/fstab
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow /dev/mapper/luks-c376919e... / ext4 ro,x-systemd.device-timeout=0 1 1
/dev/mapper/luks-c376919e... / ext4 ro,x-systemd.device-timeout=0 1 1
- Add
ro
to theGRUB_CMDLINE_LINUX
directive in the/etc/default/grub
file and ensure that it does not containrw
:Copy to Clipboard Copied! Toggle word wrap Toggle overflow GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ro"
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ro"
- Recreate the GRUB2 configuration file:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow grub2-mkconfig -o /boot/grub2/grub.cfg
# grub2-mkconfig -o /boot/grub2/grub.cfg
- If you need to add files and directories to be mounted with write permissions in the
tmpfs
file system, create a text file in the/etc/rwtab.d/
directory and put the configuration there. For example, to mount/etc/example/file
with write permissions, add this line to the/etc/rwtab.d/example
file:Copy to Clipboard Copied! Toggle word wrap Toggle overflow files /etc/example/file
files /etc/example/file
Important
Changes made to files and directories intmpfs
do not persist across boots.See Section 19.2.5.3, “Files and Directories That Retain Write Permissions” for more information on this step. - Reboot the system.
19.2.5.2. Remounting root
Instantly
If root (
/
) was mounted with read-only permissions on system boot, you can remount it with write permissions:
mount -o remount,rw /
# mount -o remount,rw /
This can be particularly useful when
/
is incorrectly mounted with read-only permissions.
To remount
/
with read-only permissions again, run:
mount -o remount,ro /
# mount -o remount,ro /
Note
This command mounts the whole
/
with read-only permissions. A better approach is to retain write permissions for certain files and directories by copying them into RAM, as described in Section 19.2.5.1, “Configuring root
to Mount with Read-only Permissions on Boot”.
19.2.5.3. Files and Directories That Retain Write Permissions
For the system to function properly, some files and directories need to retain write permissions. With root in read-only mode, they are mounted in RAM in the
tmpfs
temporary file system. The default set of such files and directories is read from the /etc/rwtab
file, which contains:
dirs /var/cache/man dirs /var/gdm [output truncated] empty /tmp empty /var/cache/foomatic [output truncated] files /etc/adjtime files /etc/ntp.conf [output truncated]
dirs /var/cache/man
dirs /var/gdm
[output truncated]
empty /tmp
empty /var/cache/foomatic
[output truncated]
files /etc/adjtime
files /etc/ntp.conf
[output truncated]
Entries in the
/etc/rwtab
file follow this format:
how the file or directory is copied to tmpfs path to the file or directory
how the file or directory is copied to tmpfs path to the file or directory
A file or directory can be copied to
tmpfs
in the following three ways:
empty path
: An empty path is copied totmpfs
. Example:empty /tmp
dirs path
: A directory tree is copied totmpfs
, empty. Example:dirs /var/run
files path
: A file or a directory tree is copied totmpfs
intact. Example:files /etc/resolv.conf
The same format applies when adding custom paths to
/etc/rwtab.d/
.