Introduction
Linux file system mounting is a fundamental skill for system administrators and technical users. This process allows the operating system to access and interact with storage devices and their file systems by attaching them to specific locations in the directory structure.
In this lab, you will learn how to create a simulated storage device, format it with a file system, and mount it to access its contents. You will also explore different mounting options that control how the file system can be used, such as restricting execution permissions or making it read-only.
By the end of this lab, you will understand:
- How to create and format a storage device in Linux
- The process of mounting and unmounting file systems
- How to use mount options to control file system access
- How to verify the status of mounted file systems
These skills are essential for managing storage devices in Linux environments, from local hard drives to removable media and network storage.
Creating and Mounting a Storage Device
In this step, you will create a simulated storage device, format it with a file system, and mount it to make it accessible within the Linux directory structure.
Navigate to the Project Directory
First, let's navigate to the project directory where you will work throughout this lab:
cd ~/project
Create a Simulated Storage Device
Now, create a simulated storage device using the dd command. This command will create a file of a specific size that will act as your storage device:
dd if=/dev/zero of=storage_device.img bs=1M count=100
This command creates a 100MB file named storage_device.img. Let's understand each part of this command:
if=/dev/zero: The input file is/dev/zero, which is a special file that produces an endless stream of zerosof=storage_device.img: The output file is namedstorage_device.imgbs=1M: The block size is set to 1 megabytecount=100: Copy 100 blocks, resulting in a 100MB file
You should see output similar to this:
100+0 records in
100+0 records out
104857600 bytes (105 MB, 100 MiB) copied, 0.0331261 s, 3.2 GB/s
Format the Storage Device with a File System
Before you can use the storage device, you need to format it with a file system. The mkfs.ext4 command formats the file with the ext4 file system, which is commonly used in Linux:
mkfs.ext4 storage_device.img
You'll see several lines of output as the command creates the file system, similar to:
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done
Creating filesystem with 25600 4k blocks and 25600 inodes
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
Create a Mount Point
Before mounting the device, create a directory that will serve as the mount point. This is the location where the contents of your file system will be accessible:
mkdir -p ~/project/mount_point
Mount the Storage Device
Now, mount the storage device to the mount point using the mount command:
sudo mount -t ext4 storage_device.img ~/project/mount_point
This command tells Linux to:
- Mount a device of type (
-t) ext4 - The device to mount is
storage_device.img - The mount location is
~/project/mount_point
Verify the Mount
To verify that the device is mounted correctly, use the df command (disk free) with options to show the file system type and human-readable sizes:
df -hT | grep mount_point
You should see output similar to:
/dev/loop0 ext4 90M 24K 83M 1% /home/labex/project/mount_point
Test the Mounted File System
To test that the file system is working correctly, create a test file in the mounted directory:
echo "This is a test file" | sudo tee ~/project/mount_point/test.txt > /dev/null
Read the file to verify it was created successfully:
cat ~/project/mount_point/test.txt
You should see the text "This is a test file" displayed, confirming that your file system is properly mounted and functioning.
Understand What Happened
You have successfully:
- Created a file that simulates a storage device
- Formatted it with the ext4 file system
- Created a mount point directory
- Mounted the file system to that directory
- Confirmed the mount was successful
- Created and read a file on the mounted file system
This process is similar to connecting and using an external hard drive or USB stick, but using a file to simulate the physical device.
Mount Options for Security and Access Control
In this step, you will learn how to use mount options to control how the file system can be accessed. Mount options provide additional control over file system behavior, such as restricting execution permissions or making a file system read-only.
Unmount the Current File System
Before applying new mount options, you need to unmount the current file system:
sudo umount ~/project/mount_point
If you get an error message saying the device is busy, make sure you're not currently in the mount point directory:
cd ~/project
Then try unmounting again.
Mount with the noexec Option
The noexec mount option prevents the execution of any binaries on the mounted file system. This is a security feature that can prevent malicious scripts or programs from running.
Mount the file system with the noexec option:
sudo mount -t ext4 -o noexec storage_device.img ~/project/mount_point
The -o noexec part tells mount to use the noexec option.
Test the noexec Option
Let's create a simple shell script on the mounted file system and try to execute it:
echo '#!/bin/bash' | sudo tee ~/project/mount_point/test_script.sh > /dev/null
echo 'echo "This script is running"' | sudo tee -a ~/project/mount_point/test_script.sh > /dev/null
Make the script executable:
chmod +x ~/project/mount_point/test_script.sh
Now try to execute the script:
~/project/mount_point/test_script.sh
You should see an error message like:
bash: /home/labex/project/mount_point/test_script.sh: Permission denied
This confirms that the noexec option is preventing script execution.
Unmount and Remount with the Read-Only Option
Now, let's try mounting the file system as read-only. First, unmount it:
sudo umount ~/project/mount_point
Then, mount it with the read-only option:
sudo mount -t ext4 -o ro storage_device.img ~/project/mount_point
The -o ro part tells mount to use the read-only option.
Test the Read-Only Option
Try to create a new file on the mounted file system:
touch ~/project/mount_point/new_file.txt
You should see an error message like:
touch: cannot touch '/home/labex/project/mount_point/new_file.txt': Read-only file system
This confirms that the file system is mounted as read-only.
Check the Mount Options
You can verify the current mount options by using the mount command:
mount | grep mount_point
You should see output including ro, indicating the read-only option is active:
/home/labex/project/storage_device.img on /home/labex/project/mount_point type ext4 (ro,relatime)
Combining Mount Options
You can combine multiple mount options by separating them with commas. Let's unmount the file system and remount it with both the noexec and ro options:
sudo umount ~/project/mount_point
sudo mount -t ext4 -o ro,noexec storage_device.img ~/project/mount_point
Verify the mount options:
mount | grep mount_point
You should see both options in the output:
/home/labex/project/storage_device.img on /home/labex/project/mount_point type ext4 (ro,noexec,relatime)
Understanding Mount Options
The mount options you've explored are important for security and controlling access:
noexecprevents execution of binary files, which can protect against malicious codero(read-only) prevents changes to the file system, which can protect data integrity
These options are commonly used in production environments to implement the principle of least privilege, giving systems and users only the access they need.
Automating Mounts with /etc/fstab
In this step, you will learn how to make file system mounts persistent across system reboots by configuring the /etc/fstab file. This is essential for automatically mounting storage devices when the system starts.
Understanding /etc/fstab
The /etc/fstab (file system table) file contains information about file systems that should be automatically mounted when the system boots. Each line in the file represents a separate file system and includes fields like the device, mount point, file system type, and mount options.
Let's first look at the current content of the file:
cat /etc/fstab
You'll see several lines with system mounts already configured.
Unmount the Current File System
Before configuring persistent mounting, unmount the current file system:
sudo umount ~/project/mount_point
Create a Backup of /etc/fstab
It's always a good practice to create a backup before modifying system configuration files:
sudo cp /etc/fstab /etc/fstab.backup
Add an Entry to /etc/fstab
Now, add an entry for your storage device to /etc/fstab. You'll need to use the absolute path to your device file:
echo "$(readlink -f ~/project/storage_device.img) $(readlink -f ~/project/mount_point) ext4 defaults 0 0" | sudo tee -a /etc/fstab
This command:
- Uses
readlink -fto get the absolute paths to your files - Redirects the formatted line to
/etc/fstabusingtee -awith sudo privileges
Let's break down the fields in the fstab entry:
- Device - The full path to your storage device file
- Mount point - The full path to your mount directory
- File system type - ext4
- Mount options - defaults (equivalent to rw,suid,dev,exec,auto,nouser,async)
- Dump - 0 (do not back up with dump)
- Pass - 0 (do not check file system at boot time)
Test the fstab Entry
Test if your entry works correctly by using the mount -a command, which mounts all file systems listed in /etc/fstab that are not already mounted:
sudo mount -a
If there are no error messages, it means your entry is correct.
Verify the Mount
Verify that the file system is mounted as expected:
df -hT | grep mount_point
You should see your file system mounted with the ext4 type.
Modify the fstab Entry with More Options
Now, let's modify the entry to use the mount options you learned in the previous step. Edit the /etc/fstab file with the nano text editor:
sudo nano /etc/fstab
Find the line you added, which should look something like:
/home/labex/project/storage_device.img /home/labex/project/mount_point ext4 defaults 0 0
Change the mount options (fourth field) from defaults to ro,noexec:
/home/labex/project/storage_device.img /home/labex/project/mount_point ext4 ro,noexec 0 0
Press Ctrl+O to write the changes, then Enter to confirm, and Ctrl+X to exit nano.
Remount with the New Options
Unmount the file system and remount all entries in /etc/fstab:
sudo umount ~/project/mount_point
sudo mount -a
Verify the New Mount Options
Verify that the file system is mounted with the new options:
mount | grep mount_point
You should see the ro and noexec options in the output.
Understanding Persistent Mounts
The /etc/fstab file provides a way to:
- Automatically mount file systems at boot time
- Configure mount options in a persistent way
- Control the order of file system checks during boot
In a production environment, proper configuration of /etc/fstab ensures that all necessary file systems are available when the system starts without requiring manual intervention.
Summary
Congratulations on completing the Linux File System Mounting lab. You have gained valuable skills that are essential for any Linux system administrator or technical user.
In this lab, you have learned:
Creating and Formatting Storage Devices
- How to create a simulated storage device using the
ddcommand - How to format the device with the ext4 file system using
mkfs.ext4
- How to create a simulated storage device using the
Basic Mounting Operations
- How to create a mount point directory
- How to mount a file system using the
mountcommand - How to verify the mount using
dfand other commands
Mount Options for Security and Control
- How to use the
noexecoption to prevent execution of binaries - How to use the
rooption to create a read-only file system - How to combine multiple mount options for enhanced security
- How to use the
Persistent Mounting with /etc/fstab
- How to configure automatic mounting of file systems at boot time
- How to set persistent mount options
- How to test and verify fstab entries
These skills will allow you to:
- Manage various types of storage devices in Linux
- Implement security controls through mount options
- Configure automatic mounting for system startup
- Troubleshoot file system mounting issues
As you continue your Linux journey, you'll find these skills valuable when working with physical storage devices, network shares, and virtual file systems. The principles you've learned apply to many different Linux distributions and storage technologies.



