Configure Systems to Mount Filesystems at or During Boot
This guide explains how to mount filesystems manually and automatically during boot using fstab.
This guide explains how to mount filesystems both manually and automatically during boot. Previously, you learned how to create filesystems, but even after creation, a filesystem remains inaccessible until it is mounted. Mounting attaches a filesystem to a directory, allowing you to create and manage files on it. The sections below detail the step-by-step process of mounting filesystems and automating these operations using fstab.
First, consider a temporary mount directory. In this example, we will mount an XFS filesystem (created in a previous lesson) located on /dev/vdb1 at the directory /mnt.
Ensure that the directory exists and is empty before mounting the filesystem.
Run the following commands to mount the device, create a test file, and verify the mount:
Copy
Ask AI
$ ls /mnt/$ sudo mount /dev/vdb1 /mnt/$ sudo touch /mnt/testfile$ ls -l /mnt/-rw-r--r--. 1 root root 0 Apr 8 09:03 testfile
You can confirm that the new file resides on the mounted filesystem using the lsblk command:
Some filesystems mount automatically at boot time. For instance, /dev/vda2 is typically mounted to /boot based on system configuration. To automate the mounting of additional filesystems such as the XFS filesystem on /dev/vdb1, you need to add an entry to the /etc/fstab file.
After saving the file, if a reboot is not performed immediately, notify Systemd of your changes so that they are applied at the next boot. On reboot, the filesystem on /dev/vdb1 will be mounted automatically, and previously created files (for example, testfile) will become visible:
In a previous lesson, you created a swap partition at /dev/vdb3. To enable the swap partition automatically at boot, add the following line to the /etc/fstab file:
Copy
Ask AI
$ sudo vim /etc/fstab/dev/vdb3 none swap defaults 0 0
Key differences in the fstab fields for swap space include:
The second field is set to none since swap space does not require a mount point.
The third field specifies the type as swap.
Both the dump and pass fields are set to 0 because swap space is not backed up or checked during boot.
A system reboot will ensure the swap partition is enabled automatically.
Sometimes, /etc/fstab entries reference devices by their UUID (Universally Unique Identifier) rather than device names. For example, instead of using /dev/vda2, an entry might use a UUID from /dev/disk/by-uuid/. The major advantage of using UUIDs is that they remain constant even if the device names change (for instance, due to varying connection orders).To check the UUID of a block device, execute:
By following these guidelines, you can ensure that your filesystems and swap space are automatically mounted and enabled at boot, even when underlying device names change. For more detailed information on fstab and mounting options, refer to the manual page:
Copy
Ask AI
man fstab
This concludes our guide on configuring systems to mount filesystems at boot. Happy computing!