How to Mount and Unmount Filesystems in Linux with Examples

How Mount and Unmount Filesystems in Linux with Examples
How Mount and Unmount Filesystems in Linux with Examples

Mount and Unmount Filesystems in Linux with Examples

In Linux, you can attach a file system or removable drive using mount command while you can detach a filesystem using unmount command. The mount command attaches the filesystem to the existing filesystem. The unmount command first completes any read, write operation running then safely unmounts the filesystem. In this tutorial, you are going to learn how to Mount and Unmount Filesystems in Linux with Examples.

Basic Syntax of mount Command

Following is the basic syntax for the mount command in Linux.

mount [OPTION/S] DEVICE_NAME DIRECTORY_NAME

Where,
OPTION: Options that should be used with mount command listed here.
DEVICE_NAME: Name of the filesystem to attach.
DIRECTORY_NAME: The related directory to which filesystem will attach.

The mount command auto detects filesystems in case of some filesystems but some filesystems are not supported. To specify file system with mount command you can use -t option like given below:

In the following example filesystem device /mnt/media with ext4 filesystem will mount to /mnt/media

mount -t ext4 /dev/sdb1 /mnt/media

To specify filesystem independent options you should use -o options with mount command as given in the following example:

mount LABEL=newdisk -o noatime,nodev,nosuid

You can check all options available with mount command by running mount --help

List all Mounted Filesystems

You can list all the mounted filesystems including virtual filesystem. you can run the following command in the terminal to do so:

mount

The above command will list all the filesystem including sysfs, proc, devtmpfs, cgroup. cgroup2 etc. virtual filesystems.

To print only sysfs filesystems you can run mount command with -t option like given below:

mount -t sysfs

The output should look like:

sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime,seclabel)

Mounting USB Drive

To mount USB drive in the Linux systems you should create a directory for mount point then mount the USB drive to it.

Create directory /media/usb by using mkdir command:

sudo mkdir -p /media/usb

You can use lsblk or fdisk-l command to check the device name of the USB drive:

lsblk

The output should be:

NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda               8:0    0 931.5G  0 disk 
├─sda1            8:1    0   500M  0 part 
├─sda2            8:2    0 100.1G  0 part
├─sda3            8:2    0 100.1G  0 part 
├─sda4            8:4    0     1K  0 part 
├─sda5            8:5    0   220G  0 part 
├─sda6            8:6    0   220G  0 part 
└─sda7            8:7    0   220G  0 part /run/media/linux4one/Local Disk G
sdb               8:16   1  29.3G  0 disk 
└─sdb1            8:17   1  29.3G  0 part 
sr0              11:0    1  1024M  0 rom  

As given in the above output the USB drive name is sdb1. Now to mount USB drive you can use the following command:

sudo mount /dev/sdb1 /media/usb

Mount Filesystems Specified in /etc/fstab

The /etc/fstab file contains the list of filesystems to be mounted during the booting process. To check the filesystems specified in /etc/fstab, run below command:

cat /etc/fstab

In some situations, you can unmount some partitions specified in /etc/fstab. So if you want to mount all the partitions specified in /etc/fstab you can run the following command:

mount a-

And for unmounting all the file systems specified in /etc/fstab you can run following command:

unmount -a

Mount ISO Files

To mount ISO file you should use a loop device which makes the file accessible as a block device. You can mount ISO file by using the following type of command:

sudo mount ISO_IMAGE_PATH DIRECTORY_NAME -o loop

So First create a mount point directory by using the following command:

sudo mkdir -p /mnt/iso

Now to mount imagename.iso file you can run the following command, while you should replace /path/to/imagename.iso with actual ISO file path:

sudo mount /path/to/imagename.iso /mnt/iso

Mount NFS Filesystem

NFS client package enables it to mount NFS filesystem. NFS client package is by default installed on most of the Linux systems. If it’s not on your system install it running the folloiwng command:

For Ubuntu or Debian Based Linux Distros:

sudo apt install nfs-common

For CentOS system:

sudo yum install nfs-utils

Install NFS client package on Fedora system:

sudo dnf install nfs-utils

Now create a mount point directory using the following command:

sudo mkdir -p /media/nfs

Open /etc/fstab to automatically mount NFS filesystem after booting:

sudo nano /etc/fstab

Paste following code replacing remote.server:/dir with your NFS server Hostname or IP address:

# <file system>    <dir>       <type>   <options>   <dump>  <pass>
remote.server:/dir /media/nfs  nfs      defaults    0       0

Run the following command to mount NFS drive:

sudo mount /mnt/nfs

Unmount Filesystems

Following is the basic syntax for unmount command in Linux. By using unmount command you can detach the filesystem.

unmount OPTIONS DEVICE_NAME_OR_DIRECTORY_NAME

To unmount USB drive /dev/sdb1 you can run following command in terminal:

unmount /mnt/usb

If the device is busy then above command will not work. You should run the following command to check which processes are working on the device:

fuser -m /mnt/usb

Once you get processes, just kill them and run unmount command again.

You can also unmount USB drive safely using -l (–lazy) option with unmount command. It will wait until the device is in a busy state, then it will unmount the device. It is also known as Lazy Unmount.

unmount -l /dev/sdb1

Conclusion

Here you have successfully learned How to Mount and Unmount Filesystems in Linux with Examples. If you have any queries regarding this then please don’t forget to comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here