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. ─────────────────────────────────────────────Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
1. Mounting a File System Manually
First, inspect the directory used for temporary mounts. The/mnt directory is conventionally used for this purpose:
/dev/vdb1. To mount it, use the following command:
lsblk command:
umount command (note that the command is spelled without an “n”):
/mnt directory is empty again:
When Linux boots, certain file systems (like
/dev/vda1 mounted on /boot) are automatically mounted according to the instructions provided in configuration files.lsblk shows various mount points:
2. Configuring Automatic Mounting with /etc/fstab
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: -
Edit the
/etc/fstabfile using your preferred text editor. In this example, we use Vim:
/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 likeext4, adjust this accordingly. - Field 4: Mount options (commonly set to
defaults). - Field 5: Dump utility flag (commonly
0since dump is rarely used). - Field 6: File system check order at boot (use
0to disable,1for the root partition, and2for other partitions).
/dev/vdb1 to /mybackups would be:
/etc/fstab file might look like this:
/etc/fstab without needing to reboot immediately:
/mybackups directory is empty before reboot:
/mybackups, confirming that the file system was automatically mounted during boot. Check with:
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:
/dev/vdb3:
Swap partitions do not mount to a directory. Instead, they enable virtual memory to improve system performance.
4. Using UUIDs Instead of Device Names
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:
blkid command. For example, to view the UUID for /dev/vdb1:
Conclusion
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!