18.2. Mounting a File System

To attach a certain file system, use the mount command in the following form:
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 $?
If no file system is attached to the directory, the above 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
Note that permissions are required to mount the file systems unless the command is run as root (see Section 18.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
For example, to display information about /dev/sda3, type:
~]# blkid /dev/sda3
/dev/sda3: LABEL="home" UUID="34795a28-ca6d-4fd8-a347-73671d0c19cb" TYPE="ext3"

18.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
Table 18.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, consult the relevant manual page as referred to in the section called “Manual Page Documentation”.

Table 18.1. Common File System Types

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.
jfs The JFS file system created by IBM.
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.
ntfs The NTFS file system. It is commonly used on machines that are running the Windows operating system.
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.

Example 18.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

18.2.2. Specifying the Mount Options

To specify additional mount options, use the command in the following form:
mount -o options device directory
When supplying multiple options, do not insert a space after a comma, or mount will incorrectly interpret the values following spaces as additional parameters.
Table 18.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”.

Table 18.2. Common Mount Options

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 18.3, “Mounting an ISO Image” for an example usage.

Example 18.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 as root:
~]# 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.

18.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
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, type:
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:
mount --make-shared mount_point
Alternatively, to change the mount type for the selected mount point and all mount points under it, type:
mount --make-rshared mount_point

Example 18.4. Creating a Shared Mount Point

There are two places where other file systems are commonly mounted: the /media/ directory for removable media, and the /mnt/ directory for temporarily mounted file systems. By using a shared mount, you can make these two directories share the same content. To do so, as root, mark the /media/ directory as shared:
~]# mount --bind /media /media
~]# mount --make-shared /media
Then create its duplicate in /mnt/ by using the following command:
~]# mount --bind /media /mnt
It is now possible to 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:
~]# mount /dev/cdrom /media/cdrom
~]# ls /media/cdrom
EFI  GPL  isolinux  LiveOS
~]# ls /mnt/cdrom
EFI  GPL  isolinux  LiveOS
Similarly, it is possible to verify that any file system mounted in the /mnt/ directory is 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:
~]# mount /dev/sdc1 /mnt/flashdisk
~]# ls /media/flashdisk
en-US  publican.cfg
~]# ls /mnt/flashdisk
en-US  publican.cfg
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:
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:
mount --make-rslave mount_point

Example 18.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/. As root, first mark the /media/ directory as shared:
~]# mount --bind /media /media
~]# mount --make-shared /media
Then create its duplicate in /mnt/, but mark it as "slave":
~]# 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:
~]# 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:
~]# 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:
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:
mount --make-rprivate mount_point

Example 18.6. Creating a Private Mount Point

Taking into account the scenario in Example 18.4, “Creating a Shared Mount Point”, assume that a shared mount point has been previously created by using the following commands as root:
~]# mount --bind /media /media
~]# mount --make-shared /media
~]# mount --bind /media /mnt
To mark the /mnt/ directory as private, type:
~]# 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:
~]# 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:
~]# 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:
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:
mount --make-runbindable mount_point

Example 18.7. Creating an Unbindable Mount Point

To prevent the /media/ directory from being shared, as root, type the following at a shell prompt:
~]# mount --bind /media /media
~]# mount --make-unbindable /media
This way, any subsequent attempt to make a duplicate of this mount will fail with an error:
~]# 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

18.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

Example 18.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
To verify the mount point has been moved, list the content of both directories:
~]# ls /mnt/userdirs
~]# ls /home
jill  joe

18.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.

18.2.5.1. Configuring root to Mount with Read-only Permissions on Boot

  1. In the /etc/sysconfig/readonly-root file, change READONLY to yes:
    # Set to 'yes' to mount the system file systems read-only.
    READONLY=yes
    [output truncated]
  2. Change defaults to ro in the root entry (/) in the /etc/fstab file:
    /dev/mapper/luks-c376919e... / ext4 ro,x-systemd.device-timeout=0 1 1
  3. Add ro to the GRUB_CMDLINE_LINUX directive in the /etc/default/grub file and ensure that it does not contain rw:
    GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap rhgb quiet ro"
  4. Recreate the GRUB2 configuration file:
    ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
  5. 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:
    files /etc/example/file

    Important

    Changes made to files and directories in tmpfs do not persist across boots.
  6. Reboot the system.

18.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 /
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 /

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 18.2.5.1, “Configuring root to Mount with Read-only Permissions on Boot”.

18.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]
Entries in the /etc/rwtab file follow this format:
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 three ways, so there are three types of entries:
  • empty path: An empty path is copied to tmpfs. Example: empty /tmp
  • dirs path: A directory tree is copied to tmpfs, empty. Example: dirs /var/run
  • files path: A file or a directory tree is copied to tmpfs intact. Example: files /etc/resolv.conf
The same format applies when adding custom paths to /etc/rwtab.d/.