How to mount and umount a file system in Linux

A file system residing on a SATA/PATA or SCSI device needs to be mounted manually to access it. The mount command allows the root user to manually mount a file system. The first argument of the mount command specifies the file system to mount. The second argument specifies the target directory where the file system is made available after mounting it. The target directory is referred to as a mount point.

The general syntax for mounting a file system with mount is:

# mount [-t file_system_type] [-o mount_options] device mount_point_directory

By using mount, you can override the default settings in /etc/fstab. For example, entering the following mounts the partition /dev/sdd1 to the directory /data:

# mount /dev/sdd1 /data

You do not usually specify the file system type because it is recognized automatically (using magic numbers in the superblock, or simply by trying different file system types; see man mount for details).

The following are some of the options you can use when mounting a file system with the command mount or by entering them in /etc/fstab.

  • remount. This option causes file systems that are already mounted to be mounted again. When you make a change to the options in /etc/fstab, you can use remount to incorporate the changes.
  • rw, ro. These options indicate whether a file system should be writable (rw) or only readable (ro).
  • sync, async. These options set synchronous (sync) or asynchronous (async) input and output in a file system. The default setting is async.
  • atime, noatime. These options set whether the access time of a file is updated in the inode (atime) or not (noatime). The option noatime should improve the performance.
  • nodev, dev. The nodev option prevents device files from being interpreted as such in the file system.
  • noexec, exec. You can prohibit the execution of programs on a file system with the option noexec.
  • nosuid, suid. The nosuid option ensures that the suid and sgid bits in the file system are ignored.

Some options only make sense in the file /etc/fstab. These options include the following:

  • auto, noauto. File systems set with the option noauto in the file /etc/fstab are not mounted automatically when the system is booted.
  • user, nouser. This option lets users mount the file system. Normally, this is a privilege of the user root.
  • defaults. This option causes the default options rw, suid, dev, exec, auto, nouser, and async to be used.

The options noauto and user are usually combined for removable media such as floppy disk or CD-ROM drives.

Unmount a File System

Once a file system is mounted, you can use the umount command (without an “n”) to unmount the file system. You can unmount the file system by using umount with the device or the mount point.
For example to unmount a file system (dev/sdd1) mounted at /data, you could enter one of the following:

# umount /data

or

umount /dev/sdd1

In order to unmount the file system, no application or user may use the file system. If it is being used, Linux sees the file system as being “busy” and will refuse to unmount the file system and will produce the below error.

# umount /data
umount: /data: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

Unmounting is not possible if the mount point is accessed by a process. For umount to be successful, the process needs to stop accessing the mount point.

The lsof command lists all open file and processes accessing them in the provided directory. It is useful to identify which processes currently prevent the file system from successful umounting.

# lsof /data
COMMAND    PID    USER   FD   TYPE DEVICE  SIZE/OFF     NODE NAME
bash      1160    root  cwd    DIR  253,1       180  4194369 /root
rsyslogd   566    root  cwd    DIR  253,1       224       64 /
...

You can also use the fuser command to get the process IDs of the processes currently running on the mpunt point you want to umount.

# fuser -cu /data

You can also kill all the processes on the mount point using the fuser command.

# fuser -ck /data

Once the processes are identified, an action can be taken, such as waiting for the process to complete or sending a SIGTERM or SIGKILL signal to the process. In this case, it is sufficient to umount the mount point.

# umount /data
Note: A common cause for the file system on the mount point to be busy is if the current working directory of a shell prompt is below the active mount point. The process accessing the mount point is bash. Changing to a directory outside the mount point allows the device to be unmounted.

Force Umount the file systems

There might be times when the system (kernel) sees the file system as busy, no matter what you try to do. In these cases, you can enter umount -f to force the file system to unmount. However, we recommend using this only as a last resort, as there is probably a reason why the kernel thinks the file system is still mounted.

# umount -f /mount_point

View Currently Mounted File Systems

You can view the file systems currently mounted by entering the command mount. Information similar to the following appears:

# mount
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
devtmpfs on /dev type devtmpfs (rw,nosuid,seclabel,size=487424k,nr_inodes=121856,mode=755)
/dev/vda1 on / type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
...

You can also view this information in the file /proc/mounts.

Related Post