How to mount and unmount a Linux file system with examples

How to mount and unmount Linux file system by example

Examples of mounting and unmounting file systems on Linux

On Linux, you can connect a file system or a removable drive using: mount You can detach the file system using a command unmount command. The mount command connects a file system to an existing file system. The unmount command completes all ongoing read and write operations before safely unmounting the file system. In this tutorial, you will learn how to mount and unmount a file system on Linux with examples.

Basic syntax of the mount command

The basic syntax of the Linux mount command is as follows:

mount [OPTION/S] DEVICE_NAME DIRECTORY_NAME

Where: options: options to use with the mount commands listed here. DEVICE_NAME: Name of the file system to connect. DIRECTORY_NAME: Related directory to which the file system connects.

For some file systems, the mount command automatically detects the file system, but some file systems are not supported. To specify a file system with the mount command, you can use the following command: -t Options such as:

In the following file system device example: /mnt/media When ext4 File system is mounted on /mnt/media

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

Must be used to specify file system independent options -o Options using the mount command, as shown in the following example:

mount LABEL=newdisk -o noatime,nodev,nosuid

You can see all available options for the mount command by running: mount --help

List all mounted file systems

You can list all mounted file systems, including virtual file systems. To do this, you can run the following command in Terminal:

mount

The above command lists all file systems, including sysfs, proc, devtmpfs, and cgroup. Virtual file system such as cgroup2.

Print only sysfs Executable file system mount With command -t Options such as:

mount -t sysfs

The output looks like this:

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

Installing a USB drive

To mount a USB drive on a Linux system, you need to create a directory for the mount point and mount the USB drive there.

Create directory /media/usb using mkdir command:

sudo mkdir -p /media/usb

Can be used lsblk Or fdisk-l Command to check the device name of USB drive:

lsblk

The output looks like this:

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 you can see in the output above, the USB drive name is sdb1. To mount a USB drive, you can use the following command:

sudo mount /dev/sdb1 /media/usb

Mount the file system specified by / etc / fstab

of /etc/fstab file contains a list of file systems to be mounted during the boot process. To check the file system specified in /etc/fstabAnd run the following command:

cat /etc/fstab

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

mount a-

And to unmount all file systems specified in /etc/fstab You can execute the following command:

unmount -a

Mount ISO file

To mount an ISO file, you must use a loop device that makes the file accessible as a block device. You can mount an ISO file using the following types of commands:

sudo mount ISO_IMAGE_PATH DIRECTORY_NAME -o loop

Therefore, first create the mount point directory using the following command:

sudo mkdir -p /mnt/iso

Mount imagename.iso You can run the following command but you need to replace /path/to/imagename.iso Actual ISO file path:

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

Mounting an NFS file system

The NFS client package allows you to mount NFS file systems. The NFS client package is installed by default on most Linux systems. If not, run the folloiwng command to install it.

For Ubuntu or Debian based Linux distributions:

sudo apt install nfs-common

For CentOS system:

sudo yum install nfs-utils

Install the NFS client package on the Fedora system.

sudo dnf install nfs-utils

Next, create the mount point directory using the following command:

sudo mkdir -p /media/nfs

Open /etc/fstab To automatically mount an NFS file system after boot:

sudo nano /etc/fstab

Paste the following code replacement remote.server:/dir NFS server host name or IP address:

#                    
remote.server:/dir /media/nfs  nfs      defaults    0       0

Execute the following command to mount the NFS drive.

sudo mount /mnt/nfs

Unmount file system

The following is the basic syntax: unmount Linux commands. You can use the unmount command to detach a file system.

unmount OPTIONS DEVICE_NAME_OR_DIRECTORY_NAME

To unmount a USB drive /dev/sdb1 You can execute the following command in Terminal:

unmount /mnt/usb

The above commands will not work if the device is busy. You need to run the following command to see what processes are running on the device:

fuser -m /mnt/usb

Once you get the processes, kill them and run unmount Command again.

You can also safely unmount USB drive -l (-Lazy) option and unmount command. Wait for the device to be busy before unmounting the device. Also called Lazy Unmount.

unmount -l /dev/sdb1

Conclusion

You have learned how to mount and unmount file systems on Linux using examples. If you have any questions about this, don’t forget to comment below.

Sidebar