Linux mount Command with Examples

June 21, 2022

Introduction

The Linux file system hierarchy is arranged in a tree, with the file system starting from the root directory (/). All other child file systems branch out from the root directory.

The mount command allows users to mount, i.e., attach additional child file systems to a particular mount point on the currently accessible file system. The command passes the mount instructions to the kernel, which completes the operation.

This tutorial will teach you the basics of attaching file systems in Linux using the mount command.

Linux mount command with examples.

Prerequisites

  • A system running Linux.
  • Access to the terminal (Ctrl + Alt + T).
  • A user account with administrator privileges.

Linux mount Command Syntax

The standard mount command syntax is:

mount -t [type] [device] [dir]

The command instructs the kernel to attach the file system found on [device] at the [dir] directory. The -t [type] option is optional, and it describes the file system type (EXT3, EXT4, BTRFS, XFS, HPFS, VFAT, etc.).

If the destination directory is omitted, it mounts the file systems listed in the /etc/fstab file.

While the file system is mounted, the previous contents, owner, and mode of the [dir] directory are invisible, and the [dir] pathname refers to the file system root.

Exit Status

The mount command returns one of the following values that indicate the process completion status:

  • 0. Success.
  • 1. Incorrect command invocation or insufficient permissions.
  • 2. System error.
  • 4. Internal mount bug.
  • 8. Operation interrupted by user.
  • 16. Issues with writing or locking the /etc/mtab file.
  • 32. Mount failure.
  • 64. At least one mount operation succeeded, but not all.

mount Command Options

The mount command options further specify file system types, mount location, and type. The following table shows the most common mount options:

OptionDescription
-aMounts all file systems listed in /etc/fstab.
-FForks a new incarnation of mount for each device. Must be used in combination with the -a option.
-hDisplays the help file with all command options.
-lLists all the file systems mounted and adds labels to each device.
-L [label]Mounts the partition with the specified [label].
-MMoves a subtree to another location.
-O [opts]Used in combination with -a, it limits the file system set that -a applies to. The [opts] refers to options specified in the options field of the /etc/fstab file. The command accepts multiple options specified in a comma-separated list (without spaces).
-rMounts the file system in read-only mode.
-RRemounts a subtree in a different location, making its contents available in both places.
-t [type]Indicates the file system type.
-TUsed to specify an alternative /etc/fstab file.
-vMounts verbosely, describing each operation.
-VDisplays the program version information.

Run the man mount command for a complete list of options, syntax forms, and filesystem-specific mount options.

Linux mount Command Examples

Outlined below are the most common use cases of the mount command.

List Mounted File Systems

Run the mount command without any options to display all currently mounted file systems. The output also displays the mount points and mount options.

For example:

Listing mounted file systems using the mount command.

List Specific File Systems

The -t option allows users to specify which file systems to display when running the mount command. For example, to show only ext4 file systems, run the following command:

mount -t ext4
Listing only specific file systems using mount.

Mount a File System

Mounting a file system requires the user to specify the directory or mount point to which the file system will be attached. For example, to mount the /dev/sdb1 file system to the /mnt/media directory, run:

sudo mount /dev/sdb1 /mnt/media

To specify additional file system-specific mount options, pass the -o flag followed by the options before the device name. Use the following syntax:

mount -o [options] [device] [dir]

See the man page or help file for a complete list of available options.

Mount File System with /etc/fstab

The /etc/fstab file contains lines describing the mount location of system devices and the options they are using. Generally, fstab is used for internal devices, such as CD/DVD devices, and network shares (samba/nfs/sshfs). Removable devices are typically mounted by the gnome-volume-manager.

Providing only one parameter (either [dir] or [device]) causes mount to read the contents of the /etc/fstab configuration file to check if the specified file system is listed in it. If the given file system is listed, mount uses the value for the missing parameter and the mount options specified in the /etc/fstab file.

The defined structure in /etc/fstab is:

<file system> <mount point> <type> <options> <dump> <pass>

The following screenshot shows the contents of the /etc/fstab file:

Showing the contents of the /etc/fstab configuration file.

To mount a file system specified in the /etc/fstab file, use one of the following syntaxes:

mount [options] [dir]
mount [options] [device]
  • For [dir], specify the mount point.
  • For [device], specify the device identifier.

See the mount command man page or run man mount for a comprehensive list of file system-specific and file system-independent options.

Mount USB Drive

Modern Linux distributions automatically mount removable drives after insertion. However, if the automatic mount fails, follow the steps below to mount the USB drive manually:

1. Create a mount point using the mkdir command:

mkdir /media/usb-drive

2. Find the USB device and file system type. Run:

fdisk -l
Listing partitions using fdisk in Linux.

3. Using the device identifier from fdisk output, mount the USB drive using the following syntax:

sudo mount [identifier] /media/usb-drive

For example, if the device is listed as /dev/sdb1, run:

sudo mount /dev/sdb1 /media/usb-drive

Mount a CD-ROM

Being a removable device, Linux automatically mounts CD-ROMs as well. However, if mounting fails, mount a CD-ROM manually by running:

mount -t iso9660 -o ro /dev/cdrom /mnt

Make sure that the /mnt mount point exists for the command to work. If it doesn't, create one using the mkdir command.

iso9660 is the standard file system for CD-ROMs, while the -o ro options cause mount to treat it as a read-only file system.

Mount ISO Files

Mounting an ISO file requires mapping its data to a loop device. Attach an ISO file to a mount point using a loop device by passing the -o loop option:

sudo mount /image.iso /media/iso-file -o loop

Mount an NFS

A Network File System (NFS) is a distributed file system protocol for sharing remote directories over a network. Mounting an NFS allows you to work with remote files as if they were stored locally.

Follow the steps below to mount a remote NFS directory on your system:

Important: Mounting an NFS requires having the NFS client package installed. See how to install the NFS server on Ubuntu.

1. Create a mount point using the mkdir command:

sudo mkdir /media/nfs

2. Mount the NFS share by running:

sudo mount /media/nfs

3. To automatically mount the remote NFS share at boot, edit the /etc/fstab file using a text editor of your choice:

sudo vi /etc/fstab

Add the following line to the file and replace remote.server:/dir with the NFS server IP address or hostname and the exported directory:

remote.server:/dir /media/nfs  nfs      defaults    0       0

Non-Superuser Mounting

Although only a superuser can mount file systems, file systems in the /etc/fstab file containing the user option can be mounted by any system user.

Edit the /etc/fstab file using a text editor and under the <options> field specify the user option. For example:

/dev/cdrom /cd iso9660 ro,user,noauto,unhide

Adding the line above to /etc/fstab allows any system user to mount the iso9660 file system from a CD-ROM device.

Specifying the users option instead of user allows any user to unmount the file system, not only the user that mounted it.

Move a Mount

If you decide to move a mounted file system to another mount point, use the -M option. The syntax is:

mount --move [olddir] [newdir]

For [olddir], specify the current mount point. For [newdir], specify the mount point to which you want to move the file system.

Moving the mounted file system to another mount point causes its contents to appear in the [newdir] directory but doesn't change the physical location of the files.

How to Unmount a File System

To unmount, i.e., detach an attached file system from the system tree, use the umount command. Detach the file system by passing either its mount point or the device name.

The syntax is:

umount [dir]

or

umount [device]

For example, to detach a USB device listed as /dev/sdb1, run:

umount /dev/sdb1

While busy with open files or ongoing processes, a file system cannot be detached, and the process fails. If you aren't sure what's using the file system, run the fuser command to find out:

fuser -m [dir]

For [dir], specify the file system mount point. For example:

fuser -m /media/usb-drive
Listing processes currently using a device.

The output lists the PIDs of processes currently accessing the device. Stop the processes and unmount the file system.

Lazy Unmount

If you don't want to stop the processes manually, use the lazy unmount, which instructs the unmount command to detach the file system as soon as its activities stop. The syntax is:

umount --lazy [device]

Forced Unmount

The -f (--force) option allows users to force an unmount. However, be cautious when force unmounting a file system as the process may corrupt the data on it.

The syntax is:

umount -f [dir]

Conclusion

This tutorial showed how to use the mount command to attach various file systems to the directory tree and provided other practical examples. The tutorial also showed how to use the umount command to detach a file system.

Next, we recommend you to read about the fsck command for checking and repairing file systems, or see how to check disk space in Linux.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
SSHFS: How to Mount Remote File Systems Over SSH
July 26, 2021

Need to transfer files between two machines securely? Try the SSHFS client, which mounts a remote file system directly to your local machine and uses the SSH file transfer protocol.
Read more
How to Mount NTFS Partition in Linux
October 8, 2020

In Linux, an NTFS partition is accessed in dual-boot setups where file exchange between drives is required. This article provides a detailed explanation of the NTFS partition mounting process.
Read more
How to Format Disk Partitions in Linux
December 2, 2020

Formatting and mounting disk partitions in Linux is common because partitions cannot be used without this process. In this tutorial you will learn how to format and mount disk partitions with ext4, FAT32, or NTFS file systems.
Read more
Introduction to the Linux File System
October 21, 2020

This article will help you understand the development process of Linux file systems and the main features of the ext4 system, as well as and alternate Linux file system options.
Read more
  • © 2022 Copyright phoenixNAP | Global IT Services. All Rights Reserved.