Home Beginner's Guide Exploring the mount command in Linux with examples

Exploring the mount command in Linux with examples

This article breaks down the Linux mount command, providing a clear understanding of how to use it to manage filesystems and attach storage devices to specific directories. With practical examples, learn how to mount various types of filesystems, including local and network storage, to enhance your system's functionality.

by Arun Kumar
mount command in linux

As an avid user of the Ubuntu terminal, I’ve always found the mount command to be one of the most powerful and essential tools in the Linux toolkit. In this blog, I’ll dive deep into the mount command, share some of my experiences, and provide sample inputs and outputs to help you get a grip on how to use it effectively.

Introduction to the mount command

The mount command in Linux is used to attach filesystems and storage devices to a specific directory in the file system hierarchy, known as a mount point. This is akin to inserting a USB drive into your computer and having it appear on your desktop or in your file explorer. The beauty of Linux, however, lies in its flexibility and control, allowing you to mount not only physical devices but also remote file systems, ISO images, and more.

Basic usage of mount command in Linux

To start with, let’s look at the most basic form of the mount command:

mount [device] [mount-point]

Here, [device] represents the storage device or file system you want to mount, and [mount-point] is the directory where you’d like it to be accessible.

Example

Imagine you have an external hard drive that you want to mount to /media/external. First, you would create the mount point (if it doesn’t already exist) and then mount the device:

sudo mkdir -p /media/external
sudo mount /dev/sdb1 /media/external

After running the mount command, your external hard drive’s contents will be accessible at /media/external.

Mount command options and flags

The mount command comes with a plethora of options and flags that allow you to specify how the file system should be mounted. Some of the options I find particularly useful include:

  • -r or --read-only: Mounts the filesystem as read-only. I often use this when I want to ensure that a disk’s contents aren’t altered during a backup process.
  • -t [type]: Specifies the file system type. This can be useful when mounting devices that don’t automatically get recognized or when mounting ISO images.
  • -o [options]: Allows you to specify various mount options such as noexec, nosuid, nodev, and many others, providing a high level of control over the mounted file system’s behavior.

Example

To mount an ISO image as read-only and ensure no executable files can be run from it, you might use:

sudo mount -o loop,noexec -t iso9660 /path/to/image.iso /mnt/iso

This command mounts the ISO image located at /path/to/image.iso to /mnt/iso with noexec option, preventing execution of any binaries on the mounted image.

Unmounting with umount

To unmount a filesystem, you use the umount command followed by either the mount point or the device name. It’s important to unmount devices before removal to prevent data loss or corruption.

Example

If you wanted to unmount the earlier mounted external hard drive, you would use:

sudo umount /media/external

Or:

sudo umount /dev/sdb1

Both commands achieve the same result, but I personally prefer to use the mount point since it’s more intuitive and less prone to errors, especially when dealing with multiple devices.

Advanced usage of the mount command in real-world scenarios

Beyond the basics, the mount command can be a powerful ally in various real-world situations, from system recovery to network file sharing. Here are some advanced use cases with sample inputs and outputs to illustrate how mount can be utilized in more complex scenarios.

Mounting network file systems

In the world of Linux, sharing resources across a network is a common practice. Network File System (NFS) and Server Message Block (SMB) are widely used protocols for sharing files across Linux and Windows systems, respectively. Mounting these network shares on your local file system can greatly simplify file access and management.

NFS mount example

Assuming you have access to an NFS share at 192.168.1.100:/shared, you can mount it to a local directory like /mnt/nfs_share:

sudo mkdir -p /mnt/nfs_share
sudo mount -t nfs 192.168.1.100:/shared /mnt/nfs_share

After executing these commands, the contents of the NFS share will be accessible in /mnt/nfs_share.

SMB/CIFS mount example

To mount a Windows share using the SMB/CIFS protocol, you might use:

sudo mkdir -p /mnt/smb_share
sudo mount -t cifs -o username=winuser,password=winpass //192.168.1.101/shared /mnt/smb_share

Replace winuser and winpass with your actual Windows username and password. This command mounts the share located at //192.168.1.101/shared to /mnt/smb_share.

Mounting a file system for repair

Sometimes, file systems may get corrupted or need manual intervention to fix issues. Mounting them in a specific mode can help in these repair processes.

Read-only mount for file system check

Before running a file system check with fsck, it’s advisable to mount the file system in read-only mode to prevent any further damage:

sudo mount -o remount,ro /dev/sda1 /mnt/repair

This command remounts the device /dev/sda1 at /mnt/repair in read-only mode, allowing for a safe fsck operation.

Using bind mounts

Bind mounts are a powerful feature that allows you to mount an existing directory to another location, effectively creating a mirror of the original directory.

Creating a bind mount

Suppose you want to make the contents of /var/www accessible in /home/user/www for convenience:

sudo mkdir -p /home/user/www
sudo mount --bind /var/www /home/user/www

After running this command, changes made in /home/user/www will reflect in /var/www and vice versa.

Mounting virtual file systems

Linux uses several virtual file systems like proc, sysfs, and tmpfs for managing system and process information. These are usually mounted automatically, but understanding how to manually mount them can be useful in certain scenarios, like chroot environments.

Mounting the proc filesystem

When setting up a chroot jail, you often need to mount the proc filesystem to provide necessary process and kernel information to the jailed environment:

sudo mount -t proc proc /mnt/chroot/proc

This command mounts the proc filesystem to /mnt/chroot/proc, making it accessible within the chroot jail.

Mount command in Linux summary

Option Description
-r or --read-only Mounts the filesystem in read-only mode, preventing any write operations to the mounted filesystem.
-t [type] Specifies the file system type, such as ext4, ntfs, or iso9660. This is useful for mounting devices or images that do not have a filesystem type that can be auto-detected.
-o [options] Allows for the specification of various mount options in a comma-separated list, such as noexec, nosuid, nodev, ro (read-only), and more, offering granular control over the behavior of the mounted filesystem.
--bind Creates a bind mount, which allows a file or directory to be mounted at another location, effectively mirroring its contents.
-o loop Used to mount image files (like ISO images) by treating them as loop devices, making the files accessible as if they were physical disk drives.

Conclusion

The mount command in Linux serves as a cornerstone for managing filesystems, offering a remarkable blend of flexibility and control. Through our exploration, we’ve seen how it caters to a wide array of needs, from mounting network shares and ISO images to setting up bind mounts and preparing environments for system repairs. The options like -r, -t, --bind, and -o loop underscore the command’s versatility, allowing users to tailor their mount operations to specific requirements.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.