How To Auto Mount An External Hard Disk Or A Pen Drive In The Right Way In Linux
Mounting an external storage device with the right file permission is critical to effectively use the mounted device. In this article, let us see how to automatically mount an external hard disk or a pen drive/ sd card in the right way in Linux Operating System.
Selecting The Right File System
Having the right file system allows to access the storage media across operating systems. If you are planning to use your external hard disk or pen drive in all three major operating systems ( Windows, macOS, Linux ), then you have to format your disk in the right file format. Disks formatted with FAT32 ( File Allocation Table ) and exFAT ( Extensible File Allocation Table ) file systems work with all Operating Systems without any additional libraries. But, they may not be the best fit in certain scenarios like supporting large file size (max size allowed for a single file is 4GB in FAT32) and enabling symlinks (not supported in both exFAT & FAT32).
NTFS ( New Technology File System) is Microsoft’s proprietary file system. But, it also works with Linux and macOS with the help of libraries like FUSE and NTFS-3G. In most of the recent Linux distros, fuse and ntfs-3g are pre-installed and it is not required to install separately.
Mounting The Disk
To automount a disk, an entry with a reference to the device path or UUID of the device has to be added in fstab
( File System Table ) file located inside the etc
folder. To view the list of devices connected to a Linux system, execute the command sudo blkid
in the terminal. This would return an output similar to the one below.
Identify the attached device and note its UUID.
Mount Options
As a next step, edit the fstab
file by executing the command sudo nano /etc/fstab
in the terminal. Add an entry similar to the one below after changing the UUID.
UUID=OC1.... /mnt/ext/data ntfs umask=027,uid=1000,gid=1000,defaults,x-systemd.device-timeout=30 0 2
Although fstab provides multiple options to mount a drive, the above options would be sufficient for regular users. Understanding these options will help us to control the behavior of the mounted drive. Let us see some of those options in detail.
In the above image, the fstab
entry contains 6 segments separated by space. Let us have a look one by one.
First Segment refers to UUID of the device. The other option is to use the device path assigned by the system like /dev/sda or sdb etc. It would work but it may also change if multiple other devices are attached. The safe option is to go ahead with UUID.
Second Segment refers to the destination path to which the device is mounted and accessible.
Third Segment refers to the type of file system of the drive.
Fourth Segment refers to mount options. In this case, it is mounted with the default option. With the default option, it automounts the disk and also provides full permission to everyone. Giving full permission to everyone may not be a good idea, so the owner and file permission has to be set with umask
, uid
and gid
. The owner (uid) and group (gid) for the mounted drive are set as 1000. In most cases, it would be the id of the logged-in user if the system is used by a single user. To get the user id and group id, execute the command “id
” in the terminal which returns the id details of the current user. If the id for the current user is different than 1000, change the fstab
entry accordingly.
File Permission
umask
refers to the file and directory permission for the users. If you require fine-grained permission for the files and directories, it can also be independently set with fmask
and dmask
. With the above settings, the logged-in user will have full permission (0) , logged-in user’s group will have read & execute permission (2) and others won’t have any permission (7). To know more about the file mask and its details, refer to this link.
x-systemd.device-timeout
, refers to the wait time for mounting the disk. In this case, it would wait for a max of 30 seconds for the device to mount, if it is not able to mount within 30 seconds, it would skip the operation and would continue with the other boot-related activities.
Fifth Segment refers to auto backup and it’s set to 0, so no action is taken.
Sixth Segment refers to the order by which the device should be mounted and validated for errors. If it is set as 0, no check is performed. If it is set as 1, it would be validated during boot. Assign 2 to perform it after the boot. In our case, we have set it as 2 to reduce the boot time.
Save the file and execute the command “sudo mount -a” to mount all the drives specified in fstab
file without restarting the machine.
Since the mount options are persisted in fstab
file, you can re-start your machine and confirm that the drive is auto-mounted in the said path with the intended permission.
Leave a comment