Site icon DesignLinux

How to Mount and Unmount File Systems in Linux

How to Mount and Unmount File Systems in Linux

The mount command mounts (attach) file systems or a storage device and makes it accessible in the existing directory tree. While the umount command detaches the mounted file system from the directory structure. In this tutorial, you will learn how to attach and detach file systems using the mount and umount commands.

List Mounted File Systems

To list the all currently attached file systems, run mount command without any argument:

mount

This will show information about the device name, directory path where the device is mounted, filesystem type and mount option.

Mounting a File System

Use the mount command as following to mount a file system for a specific location:

mount [OPTION...] DEVICE_NAME DIRECTORY

Once the file system is attached, the mount point becomes the root directory of the mounted file system.

For instance, to mount the /dev/sdb2 file system to the /mnt/tns directory you would use:

sudo mount /dev/sdb2 /mnt/tns

Generally, when mounting a device it will auto-detect the file system type. However, some file systems are not recognized and need to be explicitly specified.

You can use -t option to specify the file system type:

mount -t TYPE DEVICE_NAME DIRECTORY

To specify additional mount options, use the -o option:

mount -o OPTIONS DEVICE_NAME DIRECTORY

Get a list of all mount options by typing man mount in your terminal.

Mounting USB Drive

Most latest Linux distro like Ubuntu, it will mounts USB drives automatically when you insert it, but sometimes you may need to manually mount the drive.

Perform the following steps, to manually mount a USB device:

At first, create the mount point by typing:

sudo mkdir -p /media/usb

For example, your USB drive uses /dev/sdd2 device, run following command to mount it to /media/usb directory:

sudo mount /dev/sdd2 /media/usb

Use the below command to find the device and filesystem type:

fdisk -l

Mounting ISO Files

Perform the following steps to mount an ISO file.

Make a mounting point and location would be as per your choice:

sudo mkdir /media/iso

Now mount the ISO file to the mount point by typing:

sudo mount /home/filename.iso /media/iso -o loop

You should replace /home/filename.iso with your ISO file path.

Mounting NFS

It’s required to have NFS client package installed on your system, before going to mount an NFS share.

Install NFS client on Ubuntu and Debian:

sudo apt install nfs-common

Install NFS client on CentOS and Fedora:

sudo yum install nfs-utils

Now follow the given steps to mount a remote NFS directory on your system:

Create a directory for mount point for the remote filesystem:

sudo mkdir /media/nfs

To mount the remote NFS share automatically at boot, edit the /etc/fstab file with your text editor:

sudo nano /etc/fstab

Add the following lines to the file and you should replace remote.server:/dir with the NFS server IP address and the exported directory:

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

Run following command to mount the NFS share:

sudo mount /media/nfs

Unmounting a File System

You can use the umount command to detach a mounted a file system. Use the umount command in following form:

umount DIRECTORY
umount DEVICE_NAME

Sometimes, the umount command will fail to detach the file system. So use the fuser command to find out which processes are accessing the file system:

fuser -m DIRECTORY

After determining the processes you can stop them and unmount the file system.

To unmount a busy file system use the -l (--lazy) option as following:

umount -l DIRECTORY

You can unmount using -f (--force) option to force an unmount. This option is usually used to unmount an unreachable NFS system.

umount -f DIRECTORY

Force mount may corrupt the data on the file system so it’s not recommended.

Conclusion

Hopefully, you successfully learned about how to use mount and umount command to attach and detach various file systems to your directory structure.

If you have any question or feedback, leave a comment below.

Exit mobile version