Mount command in Linux is used to mount partitions, usb storage, cd-drives. These devices are mounted on to a mount point in order to use of Linux system. Once used the mounted device you need to unmount it, in order to prevent its further usage. Although mount automatically identifies file system type but in some cases you have to file system type.
In this guide we will cover how to use mount and unmount command to attaching or detaching various file systems / drives.
Use Mount and Umount command in Linux with Examples
Listing all mounted file systems using mount command
Running mount command with no other arguments displays all available mount points.
The information shown contains
device on directory type type [options]
This command shows list of all type of file system, but in case if you want to list only specific file system type, you can filter same using mount command with -t switch
Syntax
mount -t file_system_type
Example
mount -t ext4
BELOW IS LIST OF TYPICALLY USED MOUNT OPTION
-a: Same as auto marks file system for automatic mounting.
async: allows asynchronous I/O operations on the file system being used.
auto: marks the file system as enabled to be mounted automatically. This command can also be executed by using option mount -a
defaults: this option is an alias for async,auto,dev,exec,nouser,rw,suid.
exec: allows execution of binary files present in file system.
user: Allows an user (other than root user) to mount and unmount the file system.
loop: Mounts an image (an .iso file, for example) as a loop device.
-l: lists all file system mounted.
noexec: prevents the execution of executable files on the particular filesystem.
nouser: prevents any users (other than root) to mount and unmount the filesystem.
remount: mounts the filesystem again in case it is already mounted.
ro: mounts the filesystem as read only.
rw: mounts the file system with read and write capabilities.
-t: This option is usually followed by file system to be used by mount command.
-r: Similar to ro mounts file system as read-only.
Mounting a File System using mount command in Linux
For mounting a file system on to a mount point using the mount command use below command
Syntax
mount [OPTION…] Device_to_mount mount_location
The mount point will be attached and becomes the root directory of the mounted file system.
Example
To mount the /dev/sda1 file system to the /mnt/media directory you would use:
sudo mount /dev/sda1 /mnt/disk_1
During mounting of a device with file system such as ext4 or xfs the mount command will auto-detect the file system type, however in other cases you will have to specify.
Mounting using -t with mount command
Use switch -t to specify the file system type:
Syntax
mount -t file_system_type Device_to_mount mount_location
To specify additional mount options , use the -o option:
Syntax
mount -o file_system_type Device_to_mount mount_location
Multiple options can be provided as a comma-separated list with any spaces
Example
mount -o ro,loop test.iso /media/cdrom
Mounting a File System using /etc/fstab
fstab file control which file system are to be mounted at time of booting of system and location where these files are to be mounted. To mount a file system at time of boot you must make entry for specified file system in fstab file.
A typical entry in fstab has following fields
Device – mention for the device to which is to be mounted. Here you have to provide the device file or Label in this field.
Mount point – This contains information about location of directory where this file system is to be mounted.
Filesystem – Here details for file system type (ext4, iso9660 etc) is given.
Mount options – Refer mount options above
Dump value – This can be either 0 or 1. Dump value refers to whether backup should be done for the file system or not. This method of backing up is not preferred method in modern systems, it can be left as 0.
Filesystem check order – This value determines the order in which filesystems are checked by “fsck” program during the boot process. If the value is “0”, fsck won’t check the filesystem. For older file systems ext2, NTFS or FAT16 system it should be 0.
A sample entry
/dev/sda5 /media/mydata ext4 defaults 0 0
This command will mount sda5 device at startup on location /media/mydata, with mount default option. There will be no Dump or backup taken f, also no File system check order is provided since no check is required.
Mounting CD ROM using mount command in Linux
Below command mount device CDROM available at /dev/cdrom at directory mnt/cdrom. Ensure directory cdrom exists at location mnt before executing command.
Syntax
mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom
OR
mount -o ro /dev/cdrom /mnt/cdrom
Mount ISO image file
Similar to mounting of USB firstly identify mount destination for ISO image file. For mounting an disk image file you need to use loop device.
Syntax
mount /path/to/image.iso /destination/of_mount -o loop
Example
mount /test/home/image.iso /mnt/iso -o loop
Mounting USB using mount command in Linux
To mount USB drive first identify mount destination for USB drive.
Syntax
mount /dev/sdxx /mount_destination
Example
Assuming that USB is device location /dev/sdc1, use below command for mounting
mount /dev/sdc1 /mnt/usb
Unmount filesystem using umount command
To unmount file system you can simply specify path of mount, noteL umount command does not have ‘n’ between u and m i.e. Its umount not unmount.
Syntax
umount /mount_destination
Example
umount /mnt/usb
You may get error ‘target is busy‘ in case file system is being used. In this case you will either have to forcefully unmount or wait for accessing application to stop.
Unmount file system forcefully using umount command
Syntax
umount -f /mount_destination
Example
umount -f /mnt/usb
In case, some data is being written onto file system you may lose your data.
Lazy Unmount file system using umount command
As already mentioned umount file system forcefully may result in loss of data, in such case you may use lazy method of unmounting. In this case unmounting will start once file system is no longer busy.
Syntax
umount -l /mount_destination
Example
umount -l /mnt/usb