About cookies on this site Our websites require some cookies to function properly (required). In addition, other cookies may be used with your consent to analyze site usage, improve the user experience and for advertising. For more information, please review your options. By visiting our website, you agree to our processing of information as described in IBM’sprivacy statement. To provide a smooth navigation, your cookie preferences will be shared across the IBM web domains listed here.
Overview
In this tutorial, learn to:
- Mount and unmount filesystems manually
- Configure filesystem mounting on bootup
- Configure user-mountable, removable filesystems such as tape drives, external USB drives, floppies, and CDs or DVDs
This tutorial helps you prepare for Objective 104.3 in Topic 104 of the Linux Server Professional (LPIC-1) exam 101. The objective has a weight of 3.
Linux filesystems
The Linux filesystem is one big tree rooted at /, and yet we have filesystems on different devices and partitions. How do we resolve this apparent incongruity? The root (/) filesystem is mounted as part of the initialization process. Each of the other filesystems that you create is not usable by your Linux system until it is mounted at a mount point.
Prerequisites
To get the most from the tutorials in this series, you should have a basic knowledge of Linux and a working Linux system on which you can practice the commands covered in this tutorial. Unless otherwise noted, the examples in this tutorial use CentOS 6, with a 2.6.32-504 kernel. Your results on other systems may differ. Sometimes different versions of a program will format output differently, so your results may not always look exactly like the listings and figures shown here.
You should also be familiar with the material in our tutorial, "Learn Linux 101: Create partitions and filesystems."
Mounting filesystems
In the current set of mounted filesystems, a mount point is simply a directory where the filesystem on a device is grafted into the tree. Mounting is the process of making the filesystem on the device accessible. For example, you might mount filesystems on hard drive partitions as /boot, /tmp, or /home, and you might mount the filesystem on a floppy drive as /mnt/floppy and the filesystem on a CD-ROM as /media/cdrom1. As you can see, mount points may be in the root directory, or in a subdirectory farther down the tree.
Besides filesystems on partitions, floppy disks, and CDs, there are other types of filesystems. The tmpfs filesystem is a virtual memory filesystem. You can also mount filesystems from one system on another system using a networked filesystem such as NFS or AFS. You can even create a file in an existing filesystem and format that as a, possibly different, kind of filesystem and mount that too. This is often done with images of optical media, where you perhaps download an ISO CD or DVD image, then mount that file rather than burning it to real media. Swap space in a file rather than a dedicated swap partition is another example.
While the mount process actually mounts the filesystem on some device (or other resource), it is common to simply say that you "mount the device," which is understood to mean "mount the filesystem on the device."
Mounting and unmounting filesystems usually requires root authority. If you are logged in as an ordinary user, you will either use su -
to switch to root or sudo
. In our examples, when the command prompt ends with #, as in the listing below, you will need root authority.
The basic form of the mount
command takes two parameters: the device (or other resource) containing the filesystem to be mounted, and the mount point. We'll mount our small FAT32 partition /dev/sda3 at the mount point /dos. The mount point must exist before you mount anything over it. If it does not, you will get an error and need to create the mount point or use a different mount point. We illustrate these aspects of basic mounting in Listing 2.
Listing 2. Mount error
When you mount a filesystem over an existing directory, the files on the filesystem you are mounting become the files and subdirectories of the mount point. If the mount point directory already contained files or subdirectories, they are not lost, but are no longer visible until the mounted filesystem is unmounted, at which point they become visible again. It is a good idea to avoid this problem by using only empty directories as mount points.
After mounting a filesystem, any files or directories created or copied to the mount point or any directory below it will be created on the mounted filesystem. So a file such as /dos/sampdir/file.txt will be created on the FAT32 filesystem that we mounted at /dos in our example.
Usually, the mount
command will automatically detect the type of filesystem being mounted. Occasionally you may need to specify the filesystem type explicitly using the -t
option as shown in Listing 3.
Listing 3. Mounting with explicit filesystem type
To see what filesystems are mounted, use the mount
command with no parameters. Listing 4 shows our example system. Note that you do not need root authority to simply list mounted filesystems.
Listing 4. Displaying mounted filesystems
You can also view similar information by displaying /proc/mounts or /etc/mtab, both of which contain information about mounted filesystems.
Mount options
The mount
command has several options that override the default behavior. For example, you can mount a filesystem read-only by specifying -o ro
. If the filesystem is already mounted, add remount
as shown in Listing 5.
Listing 5. Remounting read-only
Notes:
- Use commas to separate multiple options, such as
remount
andro
. - When remounting an already mounted filesystem, it suffices to specify either the mount point or the device name. It is not necessary to specify both.
- You cannot mount a read-only filesystem as read-write. Media that cannot be modified, such as CD-ROM discs, will automatically be mounted read-only.
- To remount a writable device read-write, specify -o remount,rw
-oremount,rw
Remount commands will not complete successfully if any process has open files or directories in the filesystem you are remounting. See Unmounting filesystems below for additional information.
Labels, UUIDs, and links
In UNIX and early Linux systems, the /dev directory usually contained entries for all the devices that might ever be attached to a system. Any device that was used was always located in the same place in the /dev tree, so using names such as /dev/sda6 was natural. With the advent of hot-plugging of devices such as USB or Firewire (IEEE 1394) attached devices, a given device might appear in one USB port today, and that same device might be plugged into a different USB port tomorrow. In this environment, you might want to always mount your USB stick at /media/myusbstick, regardless of which USB port you plug it in to. In the tutorial for topic 102, "Learn Linux, 101: Boot managers," you learned about using labels and UUIDs (Universally Unique IDs) instead of device names to identify partitions. If the filesystem on the partition supports either, you can use these with the mount
command too. Use the blkid
command to find out the UUID and label (if present) associated with a device.
Listing 6 shows how to use blkid
to find the label and UUID for our root partition and then how to create two additional mount points and mount the root partition at these two additional points. This example is for illustration. You would not normally do this in a production environment.
Listing 6. Mount using labels or UUIDs
With the advent of udev, you will find additional symbolic links in the /dev directory for devices such as hard drives. Listing 7 shows the links to /dev/sda6 on my CentOS 6 system.
Listing 7. Symbolic links to /dev/sda6
You can also use a symbolic link as another way of specifying the device name when mounting a device.
Boot time and fstab
In the tutorial for topic 102, "Learn Linux, 101: Boot managers," you learned how to use the root=
parameter in both GRUB and LILO to tell the boot loader what filesystem should be mounted as root. For GRUB2, this is the set rootsetroot
statement. Once the root filesystem is mounted, the initialization process runs mount
with the -a
option to automatically mount a set of filesystems. The set is specified in the file /etc/fstab.
Listing 8 shows /etc/fstab for a sample CentOS 6 system. In this example, most hard drive partitions are identified by UUID. I have added examples of how to mount /dev/sda3 at /dos as we did earlier, and also how to use a label to mount a labeled partition at /mnt/fedora22.
Listing 8. Example CentOS 6 fstab
Lines starting with a # character are comments. Remaining lines contain six fields. Because the fields are positional, they must all be specified.
file system
This may be a device name such as /dev/sda1, or a label (LABEL=) or UUID (UUID=). For the root filesystem of our CentOS 6 example, it could be /dev/sda11, LABEL="CentOS 6", or UUID=2f60a3b4-ef6c-4d4c-9ef4-50d7f75124a2. Using a label or UUID makes your system more robust when devices are added or removed.
mount point
This is the mount point we discussed in Mounting filesystems above. For swap space, this should be the value 'none' or 'swap'. On older systems you will usually find the value 'none'.
type
Specifies the type of filesystem. CD/DVD drives will often support either ISO9660 or UDF filesystems, so you may specify multiple possibilities in a comma-separated list if you specify such a drive in /etc/fstab. If you want mount to automatically determine the type, specify auto. For example, you might see lines like the following on some older systems for a CD or DVD and a floppy disk.
/dev/scd0 /media/cdrom0 udf,iso9660 user,noauto,exec,utf8 0 0
/dev/fd0 /media/floppy0 auto rw,user,noauto,exec,utf8 0 0
option
Specifies the mount options. Specify defaults if you want default mount options. Some options you will want to know about are:
rw and ro specify whether the filesystem should be mounted read-write or read-only.
noauto specifies that this filesystem should not be automatically mounted at boot time or whenever mount -a is issued. In our example, this is done for the removable drives.
user specifies that a non-root user is permitted to mount and unmount the filesystem. This is especially useful for removable media. In older systems, this option is specified in /etc/fstab rather than on the mount command. With newer systems, it may be specified in udev rules that are located in rules files within /lib/udev/rules.d or /etc/udev/rules.d. The options for the DVD drive on my CentOS 6 system come from udev rules, and that is why there is no entry in /etc/fstab for an optical drive.
exec and noexec specify whether or not to allow execution of files from the mounted filesystem. User-mounted filesystems default to noexec unless exec is specified afteruser.
noatime will disable recording of access times. Not using access times may improve performance.
dump
Specifies whether the dump command should consider this ext2 or ext3 filesystem for backups. A value of 0 tells dump to ignore this filesystem.
pass
Non-zero values of pass specify the order of checking filesystems at boot time, as discussed in our tutorial "Learn Linux, 101: Maintain the integrity of filesystems."
When you mount a filesystems that is listed in /etc/fstab, you can give either the device name or the mount point when mounting the filesystem. You do not need to give both.
On some systems, for example SUSE 11.2, you may find that the fstab
generated at install time uses symbolic links to the device. So, you may see /dev/disk/by-id/ata-WDC_WD1001FALS-00J7B1_WD-WMATV3772868-part6, rather than /dev/sda6 for the file system value. Refer back to Listing 7 for additional possibilities
Consult the man pages for fstab
, mount
, and udev
for additional information, including options not covered here.
Unmounting filesystems
All mounted filesystems are usually unmounted automatically by the system when it is rebooted or shut down. When a filesystem is unmounted, any cached filesystem data in memory is flushed to the device.
You may also unmount filesystems manually. Indeed, you should do this when removing writable media such as diskettes or USB drives or memory keys.
Use the umount
command to unmount the filesystem, specifying either the device name or mount point as an argument. This listing shows how to unmount /dos, then remount it and unmount again using the device name.
Listing 10. Unmounting filesystems
After a filesystem is unmounted, any files in the directory used for the mount point are visible again.
If you attempt to unmount a filesystem while a process has open files on that filesystem, you will see an error message. Before unmounting a filesystem, you should check that there are no processes running that have open files on the filesystem. Use the lsof
or fuser
command to determine what files are open or what process has open files. You may need the -w
option on lsof
to avoid warning messages related to the Gnome Virtual File system (gvfs). Check the man pages to learn about additional mount options and lsof
. If you are checking a whole device, you can specify the device name or the mount point. You may also check whether an individual file is in use or not.
To illustrate these commands, I created a copy of /etc/fstab on /dos and a small script to read lines from stdin and print them to stdout with a 10 second pause between each line. Listing 11 shows the error message from umount
when files are in use and the use of lsof
and fuser
to check for open files on /dos, or the underlying device /dev/sda9.
Listing 11. Checking for open files
At this point you can either wait until the filesystem is no longer busy, or you can do a lazy unmount, by specifying the -l
option. A lazy unmount detaches the filesystem from the filesystem tree immediately, and cleans the references to the filesystem when it is no longer busy.
Removable filesystems
We mentioned some issues with removable devices such as USB or Firewire (IEEE 1394) attached devices. It is inconvenient to switch to root access every time you need to mount or unmount such a device. The same goes for CD, DVD, and floppy drives, where you need to unmount the device to change media. In the discussion of fstab
above, we mentioned the user
option, which allows ordinary users to mount and unmount devices.
Note that the filesystem types for the optical drive are specified as udf,iso9660
, while the filesystem type for the floppy is specified as auto
. For the optical drive, the mount process will check first for a udf filesystem (common on DVD) and then for an iso9660 filesystem (common on CD). For the floppy drive, the mount process will probe for a filesystem type. You can create or edit /etc/filesystems to change the order in which the filesystems will be probed.
Note: You should always unmount removable drives or media before disconnecting the drive or attempting to remove the media. Failure to do so may result in loss of data that has not yet been written to the device.
If you run a graphical desktop such as Nautilus, you will usually find options that allow removable devices and media to be automatically mounted. For example, if I insert a Knoppix DVD into the DVD drive of my system, I might see a mount entry such as shown in Listing 12. The presence of 'uid=1000' shows that the user with id 1000 can unmount this disc. The id
command shows the uid for user ian is 1000, so ian can unmount this disc.
Listing 12. Desktop mounting of DVD
You may also use the eject
command to eject removable media when the drive supports the operation as most CD and DVD drives do. If you have not unmounted the device first, then eject
will both unmount and eject the disc.
Swap space
You may have noticed in the discussion of fstab
above that swap space does not have a mount point. The boot process usually enables swap space defined in /etc/fstab unless the noauto
option is specified. To manually control swap space on a running system — for example, if you added a new swap partition—use the swapon
and swapoff
commands. See the man pages for details.
You can view the currently enabled swap devices with cat /proc/swaps
or with swapon -sswapon-s
as shown in Listing 13.
Listing 13. Displaying swap space
This completes your introduction to device mounting and unmounting on Linux.