Learn Mount command in Linux and Umount command with examples a Guide

how-to-linux-mount-and-umount-command

I have two partitions on my hard disk, and Windows 10 operating system has been installed in the first partition.

One more partition is left. When I open this PC on windows operating system I found two drives:-

  1. Local Disk (C:)
  2. Local Disk (E:)

In the above description Local Disk (C:) and Local Disk (E:) are mounting points. When you click on one of these point data appears from the respective partition.

When you will connect another device (Pendrive) the new local disk will appear with name Local Disk (F:).

But In Linux, When you will connect an external device, It will not mount automatically. You can mount external storage device on the desired directory in a Linux distribution.

You can change mounting point in Linux as per your choice.

You can use the mount command to attach the file system and external storage media such as USB flash drive, pen drive, external hard disk on particular mounting point or directory.

You can use this directory (Mounting point) to access content for an external device.

This article will cover Linux mount commands and options with example, Mount different file system, mount USB drive, ISO image, NFS (network file system) and Linux umount command. I will describe multiple options used to mount the Linux file system.

When you connect external storage media such as Pen drive, Flash drive, External Hardisk, it will automount in some Linux distro.

Content Article

Check Linux Mounted Drives

You must check the mounted drives and its mounting points before go to mount new device or partition.

If the drive / Partition or mounting point (Directory ) is busy, I mean already in use then you can not use the same drive to mount another mounting point.

In another hand, you can not mount two drives on the same mounting point. or Singe mounting point can not be used for two different devices

So first you need to check Linux mounted drives.

You can use mount command without any arguments to display the list of mounted devices.

$mount

You will see the output which includes all file system including virtual ones for the example cgroup, sysfs, and others.

Each line display the information includes device name, mounting point, file system type and mounting options.

Check the following the format of every line

Device_Name on mounting_point type filesystem_type (Mount_options)

If you want to display only a specific file system then use the -t option.

For the example, I am going to print partitions consist ext4 filesystem

[email protected]:~$mount -t ext4
/dev/sda1 on / type ext4 (rw,relatime,errors=remount-ro)
[email protected]:~$

Linux Mount ntfs and Other Filesystem

Let me share the basic syntax for the mount command. It is the following:

mount [options] [device_name] [path to mounting point / Directory]

You can check basic Linux mount command syntax and other available options by mount –help command.

Look the example below:

[email protected]:~$mount --help

Usage:
 mount [-lhV]
 mount -a [options]
 mount [options] [--source] source | [--target] directory
 mount [options] source directory
 mount operation mountpoint [target]

In the above syntax, two important things are required to mount device

  • Device Name: When you will connect a new external device, It will get a name. This name is assigned by the Linux operating system.
    If you don’t know the device name you can’t mount this device.
    To know the device name use the following command.
[email protected]:~$sudo fdisk -l
Disk /dev/sda: 16 GiB, 17179869184 bytes, 33554432 sectors
Disk model: VBOX HARDDISK   
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x0547e33d

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *     2048 33552383 33550336  16G 83 Linux


Disk /dev/sdb: 298.1 GiB, 320072932864 bytes, 625142447 sectors
Disk model: External        
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x31cc365b

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sdb1  *         2048    206847    204800   100M  7 HPFS/NTFS/exFAT
/dev/sdb2          206848 113776639 113569792  54.2G  7 HPFS/NTFS/exFAT
/dev/sdb3       113776640 341331967 227555328 108.5G  7 HPFS/NTFS/exFAT
/dev/sdb4       341331968 625139711 283807744 135.3G  7 HPFS/NTFS/exFAT 
[email protected]:~$

As per the above output I want to mount device /dev/sdb1 on /mnt folder. Then I will use the following command to mount it.

[email protected]:~$sudo mount /dev/sdb1 /mnt/ 
[email protected]:~$

Linux mount ntfs filesystem, I know the connected device has ntfs format you can see in the above example as well.

I want to attach ntfs filesystem, then I will run the following command

[email protected]:~$sudo mount -t ntfs /dev/sdb1 /mnt/ 
[email protected]:~$

Mount partition in Linux at boot

When you mount the storage device by using the mount command, it is temporary. it will unmount after reboot the system.

if you want to mount permanently, then you will have to make the entry in filename /etc/fstab

when you run mount command, it will fetch the information from /etc/fstab like device name, mounting point, file system, etc.

The /etc/fstab file contains a list of entries in the following form:

[Device] [Mount Point] [File System Type] [Options] [Dump] [fsck]

[email protected]:~$cat /etc/fstab 
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#                
# / was on /dev/sda1 during installation
UUID=9d988063-e2ec-44de-ad48-e72824eb6ade /               ext4    errors=remount-ro 0       1
/swapfile                                 none            swap    sw              0       0
[email protected]:~$

Linux Mount USB drive

When you connect USB pen drive/flash drive, some version of Linux detects and mount automatically.

Some versions of Linux don’t mount external drive by itself. In this case, you need to mount manually.

The second condition, If the USB device will be mounted automatically at the default mounting point.

You can remount on the desired mounting point. To see the example below for clarification

Create a new directory by running the following command.

mkdir -P /mnt/usb

[email protected]:~$sudo mkdir /media/usb
[sudo] password for vijay:
[email protected]:~$

See the device name by using the command (fdisk -l)

Than in Linux mount USB drive by running following command

mount /dev/sdb1 /mnt/usb

[email protected]:~$ls /media/usb/
[email protected]:~$sudo mount /dev/sdb1 /media/usb/
[email protected]:~$ls /media/usb/
 bckup                        'System Volume Information'
 kali-linux-2019-2-amd64-iso   ubuntu-19.04-desktop-amd64.iso
 Photoshop                     VirtualBox-6.0.8-130520-Win.exe
[email protected]:~$

Mount USB drive with read-write permission.

[email protected]:~$sudo mount -o rw /dev/sdb1 /media/usb/
[email protected]:~$

Linux Mount ISO file

Have heard about ISO files before? If Yes, Then you know how to access the content of ISO files.

You can’t see and access the content from ISO files. In Linux mount ISO file and access the data without any problem.

Basically, you can mount ISO file on loop device, it is a pseudo optical device used to make access of data as an optical device.

Let’s create a mounting point to mount an ISO file, I am going to create directory name iso inside media folder

mkdir /media/iso

[email protected]:~$sudo mkdir /media/iso
[email protected]:~$

In Linux mount ISO file by using the following command:

$sudo mount /Path_to/ISO file /media/iso -o loop
$sudo mount /media/usb/ubuntu-19.04-desktop-amd64.iso /media/iso/ -o loop

In Linux Mount ISO file see example below:

[email protected]:~$sudo mount /media/usb/ubuntu-19.04-desktop-amd64.iso /media/iso/ -o loop
mount: /media/iso: WARNING: device write-protected, mounted read-only.
[email protected]:~$

Linux Mount NFS

You can not mount system without having NFS client package installed on the linux system. You must install the first NFS client by using the following command

Use the following command to install NFS client on Ubuntu and Debian

[email protected]:~$sudo apt-get install nfs-common
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  keyutils libnfsidmap2 libtirpc-common libtirpc3 rpcbind
Suggested packages:
  open-iscsi watchdog
The following NEW packages will be installed:
  keyutils libnfsidmap2 libtirpc-common libtirpc3 nfs-common rpcbind
0 upgraded, 6 newly installed, 0 to remove and 234 not upgraded.
Need to get 418 kB of archives.
After this operation, 1,516 kB of additional disk space will be used.
Do you want to continue? [Y/n] 

Use the following command to install NFS client on RHEL, Fedora, and CentOS

$sudo yum install nfs-utils

Create a directory to make a mounting point for NFS

$mkdir /media/nfs

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

$sudo mount -F nfs [-o mount-options] server:/directory /mount-point

Unmount Linux command

Once you have mounted a device you can’t remove it safely without unmounting.

You can unmount device by using command unmount command followed by device name or mounting directory path.

unmount Linux command followed by mounting point

[email protected]:~$sudo umount /media/iso 
[sudo] password for vijay:
[email protected]:~$

Umount Linux command followed by the device name.

[email protected]:~$sudo umount /dev/sdb1
[email protected]:~$

Linux Unmount when a device is busy

unmount command will fail to detach the file system If it is in use. In those situations, first, find out the process which is using the mounted file system.

[email protected]:~$sudo umount /media/usb 
[sudo] password for vijay: 
umount: /media/usb: target is busy.
[email protected]:~$

You can use the fuser command to find out which professes are accessing the file system.

Once you find the processes and stop them and unmount the file system.

[email protected]:~$fuser -m /media/usb/
/media/usb:           4617
[email protected]:~$

You can use lazy -l (–lazy) option to unmount a busy file system. It will be unmounted as it is not busy anymore

[email protected]:~$sudo umount -l  /media/usb
[email protected]:~$

Conclusion

By now, I think you should have a good understanding of using the mount and umount command for attaching various file system on the desired directory and detaching the mounts.

You can visit man pages to learn more about the Linux mount and umount command and its options.

Man page for mounting system https://linux.die.net/man/8/mount

If you like our content, please consider buying us a coffee.
Thank you for your support!