We know that files are the central operating units in the Linux system. Files and directories are stored in filesystems, which can be located on various devices, such as a hard disk or a USB drive. Filesystems can also be shared over the network.
2. List Mounted Filesystems
In Linux, we can mount a filesystem into any directory. As a result, the files stored in that filesystem are then accessible when we enter the directory.
We call those directories the “mount points” of a filesystem.
We can get the information of all currently mounted filesystems by using the mount command without any arguments:
$ mount proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) sys on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) dev on /dev type devtmpfs (rw,nosuid,relatime,size=8133056k,nr_inodes=2033264,mode=755) run on /run type tmpfs (rw,nosuid,nodev,relatime,mode=755) cgroup on /sys/fs/cgroup/rdma type cgroup (rw,nosuid,nodev,noexec,relatime,rdma) /media/Data/archLinux.iso on /mnt/archIso type udf (ro,relatime,utf8) /dev/sdb1 on / type ext4 (rw,noatime,commit=120) ...
Let’s pick the last output line as an example to understand the information that the mount command gives us:
/dev/sdb1 on / type ext4 (rw,noatime,commit=120) --------- --- ---- --------------------- (1) (2) (3) (4)
- The device or filesystem name we want to mount. In this case, it’s the first partition on the second hard disk.
- The mount point. This is the directory the device is mounted to — the root directory in this case.
- The type of the filesystem. In this example, it’s ext4.
- Mount options. We’ll cover some commonly used mount options in the following sections.
The command mount will, by default, report all mounted filesystems, including the virtual ones such as cgroup on /sys/fs[?].
It can be a very long list. However, if we want to check the mounting information of a particular filesystem type, we can make use of the -t type option.
Let’s see how to get the mounting information only for udf (Universal Disk Format) filesystems:
$ mount -t udf /media/Data/archLinux.iso on /mnt/archIso type udf (ro,relatime,utf8)
3. Mounting Filesystems
Mounting filesystems isn’t complicated. Usually, it takes only two steps:
- Create the mount point (create a directory using the mkdir command)
- Mount the filesystem with the command:
mount -t Type Device MountPoint
Usually, the mount command can detect the type of filesystem automatically. That is, we don’t have to pass the -t option explicitly.
There are some cases in which the mount command cannot detect the filesystem type:
- The partition is corrupt or not formatted
- The required filesystem tools are not available — for example, an attempt to mount an NTFS partition with “read & write” access without installing the ntfs-3g package
On the Linux system, mounting is typically restricted to the root user for security reasons.
The root user can set the permission of mounting point directories. As a result, all users allowed to enter the directories can access the mounted filesystems.
Next, let’s see how to mount various devices and filesystems.
3.1. USB Drive/Stick
To mount a USB drive in Linux, first of all, we have to find out the name of the USB device we want to mount.
After we plug in a USB device, the Linux system adds a new block device file into the /dev directory.
Most modern Linux distributions will populate a /dev/disk/by-label directory by udev rules. To identify the partition on the USB drive, we can go to /dev/disk/by-label to find the block device by checking the label of the partition.
Let’s see an example of how to find the device we’re about to mount.
Say we plug in a 16GB USB stick that has a single partition, which is formatted in ext4 format with label “SanDisk_16G“:
$ pwd /dev/disk/by-label $ ls -l ... lrwxrwxrwx 1 root root 10 Oct 28 23:47 Backup -> ../../sda3 lrwxrwxrwx 1 root root 10 Nov 1 18:07 SanDisk_16G -> ../../sdd1 ...
The ls -l output shows the block device file of our USB stick is /dev/sdd1.
However, not all Linux distributions will populate the /dev/disk/by-label directory. CirrOS, for instance, doesn’t populate the by-label directory by default.
In addition to searching in /dev/disk/by-label, we can also identify the block device file of our USB device by using the fdisk command with the -l option:
root# fdisk -l ... Disk /dev/sdd: 14.94 GiB, 16013942784 bytes, 31277232 sectors Disk model: Extreme 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: 0xfdc01076 Device Boot Start End Sectors Size Id Type /dev/sdd1 * 63 31277231 31277169 14.9G 83 Linux ...
Once we’ve found the correct device file, mounting the device is pretty straightforward.
First, we create the mount point:
root# mkdir /mnt/usb16G
Now let’s mount the USB stick:
root# mount /dev/sdd1 /mnt/usb16G root# mount | grep usb16G /dev/sdd1 on /mnt/usb16G type ext4 (rw,defaults,data=ordered)
And now, we can access the USB stick under the directory /mnt/usb16G.
3.2. ISO Files
Mounting isn’t restricted to physical devices. If we have a filesystem “image”, such as an optical disc ISO image, we can mount the image as a filesystem.
We mount the image through the use of a pseudo-device called the “loop device“. This makes files contained in the image accessible.
Let’s see how to mount an ISO file archLinux.iso on /mnt/archIso:
First, we create a directory as the mount point:
root# mkdir /mnt/archIso
Next, we mount the ISO image on the directory we just created:
root# mount /media/Data/archLinux.iso /mnt/archIso -o loop root# mount | grep archIso /media/Data/archLinux.iso on /mnt/archIso type udf (ro,relatime,utf8)
In the above mount command, we use the “loop” option to tell the mount command to treat the ISO image as a loop device.
3.3. Samba Share
The SMB protocol allows a Unix-like system to access shared resources in a Microsoft Windows system.
Samba is an open-source implementation of the SMB protocol.
The cifs-utils package is required to mount a Samba share.
For example, let’s say we have a Windows share “sharedOnWin” on a Windows server (192.168.0.7), and a Windows user “kent” with password “kent_PWD” has been authorized to access the shared resources.
Before we mount the Samba share, we create a directory:
root# mkdir /mnt/winShare
Next, let’s mount the Samba share:
root# mount -t smbfs //192.168.0.7/sharedOnWin /mnt/winShare -o username=kent,password=kent_PWD root# mount | grep winShare //192.168.0.7/shareOnWin on /mnt/winShare type cifs (rw,relatime...addr=192.168.0.7,username=kent...)
Now, the files in the Windows share are available in the directory /mnt/winShare.
NFS (Network File System) is a distributed filesystem protocol that allows us to share remote directories over a network.
To mount an NFS share, we must install the NFS client package first.
Let’s say we have a well-configured NFS shared directory “/export/nfs/shared” on a server 192.168.0.8.
Similar to the Samba share mount, we first create the mount point and then mount the NFS share:
root# mkdir /mnt/nfsShare
root# mount -t nfs 192.168.0.8:/export/nfs/shared /mnt/nfsShare root# mount | grep nfsShare 192.168.0.8:/export/nfs/shared/ on /mnt/nfsShare type nfs (rw,addr=192.168.0.8)
3.5. Commonly Used mount -o Options
The mount command supports many options.
Some commonly used options are:
- loop – mount as a loop device
- rw – mount the filesystem read-write (default)
- ro – mount the filesystem read-only
- iocharset=value – character to use for accessing the filesystem (default iso8859-1)
- noauto – the filesystem will not be mounted automatically during system boot
3.6. The /etc/fstab File
So far, we’ve seen several examples of the mount command to attach to various filesystems. However, the mounts won’t survive after a reboot.
For some filesystems, we may want to have them automatically mounted after system boot or reboot. The /etc/fstab file can help us to achieve this.
The /etc/fstab file contains lines describing which filesystems or devices are to be mounted on which mount points, and with which mount options.
All filesystems listed in the fstab file will be mounted automatically during system boot, except for the lines containing the “noauto” mount option.
Let’s see an /etc/fstab example:
$ cat /etc/fstab # <file system> <mount point> <type> <options> <dump> <pass> /dev/sdb1 / ext4 rw,defaults,noatime,commit=120,data=ordered 0 1 /dev/sdb2 /home ext4 rw,defaults,noatime,data=ordered 0 2 /dev/sda3 /media/Backup ntfs-3g defaults,locale=en_US.UTF-8 0 0 /dev/sda2 /media/Data ntfs-3g defaults,locale=en_US.UTF-8 0 0 ...
Thus, if we add the following line in this file, the archLinux.iso image will be automatically mounted on /mnt/archIso after system boot:
/media/Data/archLinux.iso /mnt/archIso udf ro,relatime,utf8 0 0
Once a filesystem is mentioned in /etc/fstab, we can mount it by just giving the mount point or the device.
For instance, with the above fstab configuration, we can mount the /dev/sda2 partition with either of the two short commands:
root# mount /media/Data
root# mount /dev/sda2
4. Unmounting a Filesystem
The umount command notifies the system to detach the given mounted filesystems. We just provide the filesystem name or the mount point following the umount command.
For example, if we want to unmount the previously mounted USB stick and ISO image:
root# umount /dev/sdd1 root# umount /mnt/archIso
We can also umount multiple mounted filesystems in one shot:
root# umount /dev/sdd1 /mnt/archIso
4.1. Lazy Unmount
When we want to umount a filesystem, we don’t always know if there are operations still running on it. For instance, a copy job could be running on the filesystem.
Of course, we don’t want to break the copy and get inconsistent data. The option -l will let us do a “lazy” umount.
The -l option informs the system to complete pending read or write operations on that filesystem and then safely unmount it:
root# umount -l mount_point
4.2. Force Unmount
If we pass -f option to the command umount, it’ll forcefully unmount a filesystem even if it’s still busy:
root# umount -f mount_point
We should be careful while executing umount with -f as it could lead to corrupt or inconsistent data in the unmounted filesystem.
One real-world use case for force unmounting could be unmounting a network share because of a connection problem.
In this article, we’ve seen examples of how to use the mount and umount command to attach and detach various filesystems or devices.
Later on, we talked about some commonly used mounting options and the /etc/fstab file.
Armed with these two commands, accessing network shares or other filesystems in Linux CLI won’t be a challenge for us.