Configure systems to mount file systems at or during boot
Learn to configure Linux systems for automatic file system mounting during boot using manual methods and the /etc/fstab file.
In this article, you will learn how to configure Linux systems to mount file systems automatically during boot. Mounting a file system means attaching it to a directory within your existing directory hierarchy, allowing you to access and create files on it. Previously, file systems may have been created but not mounted, which prevents access to their directories and files.Below is a step-by-step guide that demonstrates how to mount a file system manually and configure permanent mounts in the /etc/fstab file.─────────────────────────────────────────────
First, inspect the directory used for temporary mounts. The /mnt directory is conventionally used for this purpose:
Copy
Ask AI
$ ls /mnt/
This directory should be empty initially.Assume you want to mount an XFS file system that has already been created on the partition /dev/vdb1. To mount it, use the following command:
Copy
Ask AI
$ sudo mount /dev/vdb1 /mnt/
After mounting, you can create files on the file system. For example, create a test file and list the directory contents:
Copy
Ask AI
$ ls /mnt/$ sudo touch /mnt/testfile$ ls -l /mnt/-rw-rw-r--. 1 aaron aaron 30 Jan 31 14:30 testfile
To confirm the file system is mounted, use the lsblk command:
Once you are finished with work on the file system, unmount it using the umount command (note that the command is spelled without an “n”):
Copy
Ask AI
$ sudo umount /mnt/
You can verify that the /mnt directory is empty again:
Copy
Ask AI
$ ls /mnt/
When Linux boots, certain file systems (like /dev/vda1 mounted on /boot) are automatically mounted according to the instructions provided in configuration files.
For example, the following output from lsblk shows various mount points:
When booting, a Linux system mounts file systems according to the configurations specified in the /etc/fstab file. To automatically mount our XFS file system on /dev/vdb1, follow these steps:
Create a directory that will serve as the mount point. In this example, we create /mybackups:
Copy
Ask AI
$ sudo mkdir /mybackups
Edit the /etc/fstab file using your preferred text editor. In this example, we use Vim:
Copy
Ask AI
$ sudo vim /etc/fstab
The /etc/fstab file contains six fields per line:
Field 1: The block device file (e.g., /dev/vdb1), indicating the partition or storage resource.
Field 2: The mount point (e.g., /mybackups), where the file system will be attached.
Field 3: The file system type (e.g., xfs). If using another file system type like ext4, adjust this accordingly.
Field 4: Mount options (commonly set to defaults).
Field 5: Dump utility flag (commonly 0 since dump is rarely used).
Field 6: File system check order at boot (use 0 to disable, 1 for the root partition, and 2 for other partitions).
A sample line to mount the XFS file system on /dev/vdb1 to /mybackups would be:
Copy
Ask AI
/dev/vdb1 /mybackups xfs defaults 0 2
For context, a typical /etc/fstab file might look like this:
Copy
Ask AI
/dev/mapper/cs-root / xfs defaults 0 0/dev/vdb1 /mybackups xfs defaults 0 2# If you had an ext4 partition, it might look like:#/dev/vdb2 /mybackups ext4 defaults 0 2# After editing this file, run 'systemctl daemon-reload' to update systemd configuration.
After saving your changes, update systemd units generated from /etc/fstab without needing to reboot immediately:
Copy
Ask AI
$ sudo systemctl daemon-reload
Verify that the /mybackups directory is empty before reboot:
Copy
Ask AI
$ ls /mybackups/
After rebooting the system, you should find the test file in /mybackups, confirming that the file system was automatically mounted during boot. Check with:
Copy
Ask AI
$ ls -l /mybackups/-rw-rw-r-- 1 aaron aaron 30 Jan 31 14:30 testfile
3. Configuring a Swap Partition to Mount Automatically
If you have created a swap partition (e.g., on /dev/vdb3), you can enable it to activate automatically at boot. To do this, edit the /etc/fstab file:
Copy
Ask AI
$ sudo vim /etc/fstab
Locate a line similar to the following, which mounts swap space for another device:
Copy
Ask AI
/dev/mapper/cs-swap none swap defaults 0 0
Then add or modify the line for the swap partition on /dev/vdb3:
Copy
Ask AI
/dev/vdb3 none swap defaults 0 0
Note that for swap, the second field is set to “none” because it does not attach to any directory. The file system type is “swap,” and the last two fields are set to 0 since swap space is not backed up or checked during boot.After saving the file, reboot your system and verify that the swap partition is active:
Copy
Ask AI
$ swapon --show
Swap partitions do not mount to a directory. Instead, they enable virtual memory to improve system performance.
Instead of specifying a block device file like /dev/vda1, you can use UUIDs (Universally Unique Identifiers). UUIDs are especially useful because device names may change if storage devices are connected in a different order.A typical /etc/fstab line using a UUID looks like this:
This guide has shown you how to mount file systems both manually and automatically using /etc/fstab, as well as how to configure a swap partition and use UUIDs for improved device management. Now it’s time to put this knowledge into practice by mounting file systems and configuring the /etc/fstab file on your Linux system. Happy configuring!