- **Epistemic status:** #evergreen Automatically mounting a file system on boot in [[Linux]] can be useful for having multiple partitions to be utilized by your system. The following steps should teach you how. ## Step 1: Get the drive information Open the terminal and type the following command to get the information of the drives connected to your system. ```shell $ blkid ``` The output will look similar to the example below. ```shell > /dev/nvme0n1p1: UUID="C409-A792" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="b4344fcd-9ca4-460a-9484-1a8809562bc4" ``` From the example, the information that we need to extract is the following: 1. Name: `/dev/nvme0n1p1` 2. UUID: `C409-A792` 3. Type: `vfat` ## Step 2: Make a mount point for your drive Create a directory where you want to mount the drive. Depending on the type of drive, you will need to create it on the proper directory. - For removable drives like external hard-drives, USB drives, floppy disks you will need to create it on the `/media` directory. - For manually temporary file systems, it needs to be created in the `/mnt` directory. You can find more information here: - [Filesystem Hierarchy Standard - Chapter 3. The Root Filesystem](https://www.pathname.com/fhs/pub/fhs-2.3.html#MNTMOUNTPOINTFORATEMPORARILYMOUNT) To create the directory we will issue the command replacing the name of the directory and name of the drive with the ones that apply for your use case. ```shell $ mkdir /<name-of-directory>/<name-of-the-drive> ``` An example of the previous command: ```shell $ mkdir /media/nvme0n1p1 ``` ## Optional Step: Get your user ID > Warning - By allowing a non-root user to mount/unmount a drive it introduces a security risk because it can introduce the kernel to untrusted data. The file system could have malicious or malformed data causing a DoS or arbitrary code execution. Exercise caution before allowing unprivileged users to mount a drive. Type the following command to get the id of all the users in your machine. ```shell $ id ``` The output will look similar to the following example: ```shell > uid=1001(zero) gid=1001(zero) groups=1001(zero),27(sudo) ``` Get the `uid` of your user for the next step when we edit the `fstab` to allow users to mount/unmount the drive. ## Step 3: Edit /etc/fstab File > The `fstab` file can be used to define how disk partitions, various other block devices, or remote file systems should be mounted into the file system. ([[#^6dbc45]], 2022) We need to edit the `fstab` file to define how our file system we just created. To edit the file run the following command: ```shell $ sudo vim /etc/fstab ``` At the end of the file on one line we will add the following text to have the drive automatically mount on boot. Replace the values with the ones that correspond to your system. ```bash UUID=<uuid-of-your-drive> <mount-point> <file-system-type> <mount-option> <dump> <pass> ``` An example of the previous template that _does not allow non-root users to mount/unmount_ ```bash UUID=C409-A792 /media/nvme0n1p1 vfat defaults 0 2 ``` An example of the previous template that _does allow a specified non-root user to mount/unmount_ ```bash UUID=C409-A792 /media/nvme0n1p1 vfat uid=1001,user,defaults 0 2 ``` ### Fields - `<uuid-of-your-drive>`: Device names aren't guaranteed to be the same across a reboot. UUID are relatively unique and are hard to guess making it an ideal option for identifying your drive no matter where it is plugged in - `<mount-options>` - `defaults`: Gives users read and write access to the file system by applying the following options `rw`, `suid`, `dev`, `exec`, `auto`, `nouser`, and `async` - `uid`: The user id of the user or group you want to allow to mount/unmount the file system. It will default to the user running the current process - `user`: Allow a user to mount the drive - `<dump>`: This value is usually 0 and it means to not jump when not present. - `<pass>`: Used by the `fsck` [[Computer Program]] to determine the order of which the file systems are checked on boot. Root file systems is 1. Swap partitions are 0 because they do not need to be checked. Any other file systems in between are a 2 You can find more details about this on the following resources: - [fstab manpage](https://www.man7.org/linux/man-pages/man5/fstab.5.html) - [mount manpage](https://linux.die.net/man/8/mount) ### Step 4: Mount the drive Run the following command and if no error pops up you should have the file system mounted. ```shell sudo mount -a ``` --- ## References - bk2204. “Answer to ‘Why Do I Need the Root Password When Mounting an Internal Drive in Linux?’” _Information Security Stack Exchange_, January 9, 2022. <https://security.stackexchange.com/a/258646>. - “Filesystem Hierarchy Standard.” Accessed June 16, 2022. <https://www.pathname.com/fhs/pub/fhs-2.3.html#MNTMOUNTPOINTFORATEMPORARILYMOUNT>. - “Fstab - ArchWiki.” Accessed June 16, 2022. <https://wiki.archlinux.org/title/Fstab>. - “Fstab(5) - Linux Manual Page.” Accessed June 16, 2022. <https://www.man7.org/linux/man-pages/man5/fstab.5.html>. ^6dbc45 - Guoan, Xiao. “How To Automount File Systems on Linux.” _LinuxBabe_ (blog), February 23, 2016. <https://www.linuxbabe.com/desktop-linux/how-to-automount-file-systems-on-linux>. - Hemmelgarn, Austin. “Answer to ‘Why Use UUIDs in /Etc/Fstab Instead of Device Names?’” _Unix & Linux Stack Exchange_, February 12, 2018. <https://unix.stackexchange.com/a/423698>. - Mikel. “Answer to ‘Why Have Both /Mnt and /Media?’” _Ask Ubuntu_, January 19, 2011. <https://askubuntu.com/a/22218>. - “Mount(8): Mount Filesystem - Linux Man Page.” Accessed June 16, 2022. <https://linux.die.net/man/8/mount>. - neitsab. “Answer to ‘Why Have Both /Mnt and /Media?’” _Ask Ubuntu_, December 12, 2013. <https://askubuntu.com/a/389980>. - NewLinux. “Why Do I Need the Root Password When Mounting an Internal Drive in Linux?” Forum post. _Information Security Stack Exchange_, January 9, 2022. <https://security.stackexchange.com/q/258637>. - user10489. “Answer to ‘Why Do I Need the Root Password When Mounting an Internal Drive in Linux?’” _Information Security Stack Exchange_, January 9, 2022. <https://security.stackexchange.com/a/258641>. - warren. “Why Use UUIDs in /Etc/Fstab Instead of Device Names?” Forum post. _Unix & Linux Stack Exchange_, February 12, 2018. <https://unix.stackexchange.com/q/423693>. - Linux Handbook. “What Is UID in Linux? How to Find UID of a User?,” August 2, 2019. <https://linuxhandbook.com/uid-linux/>.