How to Use mount Command in Linux


Mounting a file system is a useful task on Linux that helps you organize or neatly arrange different partitions, each occupying a different file system. The mounting will help you quickly find the right file system when needed. To perform the mounting operation, the mount command is used. It is a powerful command in the Linux system that allows you to attach your file systems like hard disk partitions, and removable devices, such as USB, and Floppy disk to your system.

Read this guide to find in detail how to use the mount command in Linux, here, you will learn:

Syntax for mount Command in Linux

Before moving towards using the mount command in Linux, let’s first understand its syntax, which is given below:

mount -t type device dir

Here, in the above syntax,

  • -t type flag is optional and specifies the file system type, such as EXT3, EXT4, XFS,w3eq, and more.
  • device refers to a device or partition containing the file system you want to mount.

dir represents a directory or location where you want to attach your file system.

Besides that, there are some other options you can use with the mount command in Linux to further specify file system types. You can find these commands with their descriptions by opening the mount help section on the Linux terminal using the below-given command :

mount -h

Moreover, the Linux system includes different kinds of file systems, and understanding those file systems is important for you. To get the list of supported file systems on Linux, you can use the below-given command:

cat /proc/filesystems

To find out in detail about these file systems, you can open the manual on Linux using the following command:

man filesystem

How to Use mount Command in Linux

You can use the mount command in Linux to:

How to Find a List of Currently Mounted File Systems in Linux

If you want to find out the list of the currently mounted file systems in Linux, you can use the below-given mount command without using any argument:

mount

To get the result of mounted file systems in structure format, you can use the following command:

findmnt

You can also use the lsblk command to find out the mounted block devices on Linux:

lsblk

How to Find a List of Specific File Systems on Linux

To find the list of a specific file system on Linux, you can use the -t argument with the mount command followed by the file system to be displaced. Here, as an example, I am using the following command to get the mounted file system information on Linux:

mount -t ext4

How to Mount a File System on Linux

For mounting a file system on Linux, you must specify the directory or mount point where the file system will be attached. For example, to mount a /dev/sdc1 file system to the /mnt/USB location, you can use the following command:

sudo mount /dev/sdc1 /mnt/USB

Note: You must ensure the mount point “/mnt/USB” directory is created on your Linux system. If it’s not, you can create it using the mkdir command.

How to Mount a USB Drive on Linux

Most modern Linux distributions automatically mount your USB drive after the insertion, however, this may not be true for some Linux distributions. If you feel the need to mount your USB drive, you can use the mount command to successfully complete the operation. The steps to mount a USB drive on Linux using the mount command are provided below:

Step 1: First, insert your USB drive into your Linux system and wait till the system detects the drive.

Step 2: Then run the fdisk command with sudo privileges to check for the device name:

sudo fdisk -l

You will find your USB device at the end of the output generated by the fdisk command:

Step 3: Create a mount directory using the mkdir command according to your choice. Here, we have created a directory named /mnt/USB on Linux using the following command:

sudo mkdir /mnt/USB

Step 4: Now, use the mount command followed by the USB device and the location where you want to mount your USB drive. Here, in our case, /dev/sdc1 is the location of the USB drive that is mounted to /mnt/USB location using the following command:

sudo mount /dev/sdc1 /mnt/USB

Step 5: To ensure your USB drive is mounted on Linux, you can run the below-given command:

sudo mount | grep /dev/sdc

Note: If your device information is /dev/sdbX, then use sdb instead of sdc in the above command to get the information of your mounted device.

How to Mount an ISO File on Linux

You can also mount your ISO file in Linux by using the mount command. However, for mounting the ISO file, first create a directory that serves as the mount location using the mkdir command. Then use the mount command to attach or mount the ISO file to the specified mount point:

sudo mount iso_file /media/isofile -o loop

Here, replace iso_file in the above command with the location of your ISO file on Linux. /media/isofile is the mount point location. While -o loop maps a loop device to the specified ISO file and mounts it on the specified mount point.

How to Mount a CDROM in Linux

By default, the Linux system automatically mounts removable devices like CDROM. However, if for some reason mounting fails, you can mount the CDROM again on Linux using the below-given mount command:

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

Note: Ensure /mnt point exists for the above command to work. If it is not created, use the mkdir command to create the /mnt point on Linux.

How to Mount an NFS on Linux

Network File System, commonly referred to as NFS is a technology that allows you to share files and directories over a network. By mounting the NFS, you will be able to remotely access the files on Linux that are stored locally. For mounting an NFS on Linux, follow the below-given steps:

Step 1: First, create a mount point for NFS on a Linux system by using the following command:

sudo mkdir /media/directory_name

Step 2: Then mount the NFS directory at a specified mount point on Linux through the following command:

sudo mount IP_address:/directory mount_point

Ensure replacing the IP address, directory location, and mount_point according to yourself.

Note: For creating an NFS share on Linux, follow the guide.

Step 3: To automatically mount the NFS share during system startup, open the fstab file on Linux using the following command:

sudo nano /etc/fstab

Then add the following line inside the file by replacing the values according to your NFS share:

IP_address:/directory mount_point nfs defaults 0 0

Once done, save the file using CTRL+X, add Y and press Enter to automatically mount the NFS share on Linux.

How to Unmount or Detach a File System on Linux

Unmounting or detaching a file system on Linux is as simple as mounting it. You can either mount the file system through the mount point or the device name; in both cases, you will be able to unmount your file system on Linux.

To unmount a file system on Linux with the device name, you can use the following syntax:

sudo umount /dev/sdX

Here, replace X with the appropriate letter corresponding to your specific device, for example, sda1, sda2, etc.

To unmount a file system on Linux with a mount point, you can follow the below-given syntax:

sudo umount /mnt/USB

Note: To verify your mounted file system, you can simply use the following command:

df -h

Bonus Tips

During the mount process on a Linux system, you may encounter the following types of errors:

The following are the solutions to encounter such types of errors during the mount process.

Fix Cannot create directory: Read-only file system

While mounting your file system on Linux, you will require creating a directory at the /mnt location using the mkdir command. However, sometimes you may encounter an error that the specific directory cannot be created on a specific location because of the Read-only file system, as shown below:

You can fix the error by using the fsck command given below:

sudo fsck -n -f

The command will check for the file system consistency and repair the file system accordingly. -n and -f flags are used to display potential issues without making any changes and force-checking even if the file system appears clean, respectively.

After the above command execution, reboot your system and start creating your directory at the /mnt location.

Fix Specific file system that is already mounted or the mount point is busy

There is another error that you will face during the mount process which shows that the specific file system is already mounted or mount point busy. You can fix this error by first unmounting the device using the below-given command:

sudo umount mount_point

If successful, try mounting the device again, if the problem is still there, then check for the process using the mount point.

ps aux | grep mount_point

If you see the process using the specific mount point, terminate the process or wait till it is completed, then try mounting the device again.

If still you encounter the same issue, you can force unmount the device using the following command:

sudo umount -f /dev/sdX

Then mount the device again.

Conclusion

The mount command in Linux is a versatile command used for attaching a file system to a specific directory within the hierarchy of the file system. You can use the mount command in Linux to find a list of currently mounted or specific file systems, mounting a file system, USB drive, ISO file, and CDROM. Further, you can also use the mount command to mount an NFS share on a Linux system. In this article, we have presented in detail how the mount command can be used to perform the aforementioned tasks. It will help you learn the use of command and start mounting your device at a specific mount location according to your desire.

Print Friendly, PDF & Email