Linux Mount Command

29/12/2020
In the Linux ecosystem, mounting is one of the major operations that the system relies on. In fact, the filesystem of Linux is solely dependent on the “mount” mechanism.

Mounting mechanism

Before jumping deeper into the “mount” command, let’s have a look what it actually is. In Linux, mounting is the process of attaching an additional filesystem on top of the currently accessible filesystem on the computer.

A quick note on the filesystem: it’s a hierarchy of directories that systems use to organize file(s) and folder(s) on a storage media. Filesystem exists in every single storage solution: USB flash drive, CD-ROM, HDD, SSD, and even floppy disks! In the case of UNIX/Linux and similar systems, the filesystem starts with the root directory (noted as “/”). Under “root”, all the other child filesystems exist.

Using mount

Because of the mounting mechanism at the core of the system, “mount” plays an important role in shaping the Linux ecosystem. This command offers a ton of utility for numerous situations. Whenever you’re running “mount” to mount a filesystem, it contacts with the kernel to complete the operation.

In this article, I will try to cover important and interesting ones as much as possible. However, it’s completely up to you and your creativity to use “mount” to your advantage.

“mount” location

Run this command.

which mount

It’s located in the “/usr/bin” directory. This directory is generally the home of almost all the commands you’ll ever run in the history of your Linux.

Basics

Here’s how the very basic structure of running “mount” commands looks like.

mount –source <source> –target <target>

While using “–source” and “–target” flags aren’t so common, I strongly recommend people doing so for having a clear distinction on the purpose of the command.

In order to mount a certain filesystem, “mount” needs the source and the target. For example, I grabbed the Ubuntu installation ISO and I wish to mount this file. Run the following commands.

sudo mkdir /mnt/ubuntu_iso

sudo mount -o loop -t iso9660 –source ~/Desktop/ubuntu-19.04-desktop-amd64.iso
 –target /mnt/ubuntu_iso

In this series of commands, we created a directory where the ISO file will be mounted. Then, we told “mount” to use that directory for mounting the ISO in that directory.

If you end up with an error like “failed to setup loop device”, then check if the command is correctly typed. In other cases, make sure the loop module of the kernel is running.

lsmod | grep loop

No output in this step means the module isn’t running. Start it using “modprobe”.

sudo modprobe loop

The command should run just fine now.

Listing all the mounts

When running “mount’ just by itself without any additional parameters, it will return the list of all the currently mounted filesystems in the computer’s storage.

mount

Or,

mount -l

Mount version

mount -V

This will display the software version of “mount”.

Filesystem info

If you have a certain type of filesystem in mind, you can use “mount” to check what mounts use this filesystem.

mount -t <filesystem_type>

For example, this command will list all the filesystems that use “ext4”.

mount -l -t ext4

Using /etc/fstab

This is a special system configuration file that contains the filesystems for the OS. While it’s now easier to mount devices and filesystems, at the early ages, fstab was the only option to tell the computer to check for any specific filesystem and mount it automatically.

It’s located on “/etc/fstab”.

bat /etc/fstab

Bat is a superior alternative for cat with additional features like syntax highlighting and formatting.

As mentioned in the file, fstab follows the following structure for the table.

<filesystem> <mount_point> <type> <options>
<dump> <pass>

In the list, there’s an entry by default (the system’s HDD). Let’s explain it part by part.

This part is the filesystem that will be mounted. It’s set by the UUID.

Next is the mount point. In this case, it’ll be mounted as the root.

Now, the filesystem type. It uses the ext4 journaling file system.

These are the options that mount will follow when mounting the filesystem.

The next values are for “dump” and “pass” respectively.

In this article, we’re not going deeper into the fstab craziness. If you’re interested, feel free to search the internet for suitable guides and explanations with additional tips and tricks.

Mounting a device

In almost all the modern Linux distros these days will automatically mount any storage device you connect to it automatically, for example, USB flash drives. However, if it didn’t mount or auto-mount is disabled, you have to mount it manually.

At first, check if the device is being recognized by the system.

fdisk -l

Create a suitable directory for mounting the device.

sudo mkdir /run/media/usb

Time to finally mount the drive into the folder.

sudo mount –source /dev/sdb1 –target /run/media/usb

Note: If you’re willing to mount storage with some unorthodox filesystems like exFAT, you need the appropriate drivers. In the case of Ubuntu, it’s “exfat-utils” and “exfat-fuse”. In the case of Arch and other Arch-based distros, it’s “exfat-utils”. If you’re using anything other than that, make sure to check for suitable exFAT solution for your system.

Unmounting any mount

Once you’ve mounted, the filesystem will be present there forever. In the case of core filesystems, it’s important that they remain mounted. However, removal devices must be unmounted first and then removed. Otherwise, it might cause issues like data loss, data corruption, and other damages. I accidentally destroyed a USB flash drive of mine.

To unmount any filesystem, use the “unmount” command.

sudo umount -v <target>

Final thoughts

It’s always great to consult the man and info pages for in-depth guides on any tool. “mount” is no different.

man mount

info mount

Enjoy!

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Hướng dẫn sử dụng plugin [inputs.procstat] của TIG stack để giám sát process

Tiếp tục chuỗi bài viết về TIG stack, hôm nay Onet sẽ hướng dẫn các bạn sử dụng plugin [inputs.procstat]...
30/12/2020

Tại sao Proxy Socks lại được ưa chuộng hơn Proxy HTTP?

Khi set up proxy thì các bác sẽ phải chọn loại giao thức HTTP hoặc Socks. Vậy 2 loại proxy Socks và...
04/01/2024

5 Best Linux Distributions for Gaming

After writing about Steam on Linux in my last article, I’m here with another gaming article for fellow Linux gamers out...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch vụ thuê mua proxy Grass siêu rẻ, nhanh chóng và an toàn
10/05/2024

Thuê mua proxy Pixel giá tốt, tốc độ CỰC ĐỈNH
10/05/2024

Dịch vụ thuê mua proxy v4 an toàn, tốc độ, giá siêu rẻ
10/05/2024

Thuê mua proxy Iphone uy tín, chất lượng cao
10/05/2024

Thuê mua proxy android uy tín, giá tốt nhất hiện nay
10/05/2024