How to Auto Mount Partitions and Secondary Hard Drives in Linux with fstab file

If you are familiar with Linux based system and you have multiple partitions or Hard drives then you know that the Linux OS will only mount the root partition where the File System has been installed. And in order to mount the other partition or hard drives you need to click on their Tabs on File manager or just mount with mount utility in command line. But we can automatically mount these partitions and Drives at startup, just by editing the fstab file on the file system.



The fstab (/etc/fstab) file is a system configuration file on Debian systems. The fstab file typically lists all available disks and disk partitions, and indicates how they are to be initialized or otherwise integrated into the overall system's file system. Each file system is described in a separate line. For Demonstration i am using Ubuntu mate OS, but you can do this on any Linux OS. Now Lets first start the terminal and list all the available drives and partitions on the system by lsblk command :
 $ lsblk


at the above output we can see that the sda5 and sda6 partition will mounted, where sda5 is the root partition and sda6 is SWAP partition. Now lets see the fstab file configuration by cat /etc/fastab command :
 $ cat /etc/fstab
 #  <file system>                <mount point>     <type>    <options>            <dump>   <pass>
 # / was on /dev/sda5 during installation
 UUID=ea8075fc-0703-4a5b-9b73-644f1cdca5f4    /             ext4     errors=remount-ro      0         1
 # swap was on /dev/sda6 during installation
 UUID=d533e07f-01d2-4ca2-b11b-9e3b07e51997      none            swap     sw                     0         0

Now lets see how the fstab file will be structured : 
 <file system>  <mount point>  <type>  <options>  <dump>  <pass>

<file system> :  The file system defines the storage device, for example /dev/sda5, and the UUID (Universally Unique InDentifier) of /dev/sda5 is UUID=ea8075fc-0703-4a5b-9b73-644f1cdca5f4, yoiu can also put /dev/sda5 instead UUID of device, but it is good to use UUID of devices because it is more robust way to name devices, which works even if old HDDs are removed and new HDD's are added. Because if the old HDD's removed and the new ones are added then the system will give the new ones same name like /dev/sda1, sda2, sda3 ... but the UUID of the devices are not the same.

<mount point> :  mount point tells the system where it should mount the . At my case the sda5 will mount to '/' root and sda6 which is swap is none. 

<type> :type defines the file system type of the device or partition to be mounted. Many different file systems are supported. Some examples are: ext2, ext3, reiserfs, xfs, jfs, smbfs, iso9660, vfat, ntfs, swap, and auto. The 'auto' type lets the mount command to attempt to guess what type of file system is used, this is useful for removable devices such as CDs and DVDs. At my case it is ex4 and swap.

<options> : options define particular options for filesystems. Some options relate only to the file system itself. Some of the more common options are:

  • auto - file system will mount automatically at boot, or when the command 'mount -a' is issued. 
  • noauto - the filesystem is mounted only when you tell it to.
  • exec - allow the execution binaries that are on that partition (default).
  • noexec - do not allow binaries to be executed on the filesystem.
  • ro - mount the filesystem read only.
  • rw - mount the filesystem read-write. 
  • defaults - default mount settings (equivalent to rw,suid,dev,exec,auto,nouser,async). 
  • noatime - do not update inode access times on the filesystem. Can help performance. 

<dump> : dump is checked by the dump(8) utility. This field is usually set to 0, which disables the check.

<pass> : fsck reads the pass number and determines in which order the file systems should be checked. Possible entries are 0, 1, and 2. The root file system should have the highest priority, 1, all other file systems you want to have checked should get a 2. File systems with a pass value 0 will not be checked by the fsck utility. 

Now that's the enough details to start editing fstab file. But first make a backup of fstab file, where if anything goes wrong then you can restore it, Since it’s an important file, it would be best that make back up this file before editing it. and you also need to root privilege for editing fstab file. To create backup file :
 $ sudo mkdir /etc/backup
 $ sudo cp /etc/fstab /etc/backup/fstab
the above command will make a backup copy of fstab file in /etc/backup directory.


1. Automounting Partitions :

First, we need t get the UUID of all partitions by using the blkid command :
 $ sudo blkid 
/dev/sda3: UUID="E8D8AB69D8AB34AA" TYPE="ntfs" PARTUUID="8957692f-03"
/dev/sda5: UUID="ea8075fc-0703-4a5b-9b73-644f1cdca5f4" TYPE="ext4" PARTUUID="8957692f-05"
/dev/sda6: UUID="d533e07f-01d2-4ca2-b11b-9e3b07e51997" TYPE="swap" PARTUUID="8957692f-06"
/dev/sda1: LABEL="System Reserved" UUID="961C24A01C247CFD" TYPE="ntfs" PARTUUID="8957692f-01"
/dev/sda2: UUID="783C2D063C2CC14C" TYPE="ntfs" PARTUUID="8957692f-02"
We are using nano editor to edit the fstab file,
 $ sudo nano /etc/fstab
Now in my case i want to mount /dev/sda2 and dev/sda3 devices or partitions which both are ntfs types. To do this i need to add the following lines in my fstab config file
 <file system>           <mount point>    <type>     <options>     <dump> <pass>
# for /dev/sda2
UUID=783C2D063C2CC14C  /media/ajay/drive1  ntfs  defaults,noatime    0      2
# for /dev/sda3
UUID=E8D8AB69D8AB34AA  /media/ajay/drive2  ntfs  defaults,noatime    0      2  
At file systm put the UUID of the device or partition, at mount point i put /media/username/drive1 and /media/username/drive1, at the place username, put your system's username. You can also put other locations where you want to mount your drives. At type these are ntfs partitions, so i put ntfs here, now at options put defaults, noatime. Put the number 0 under dump and finally, put the number 2 under pass. i also put the comments with # for convenience. Now the fstab file look like :
 $ cat /etc/fstab
 #  <file system>                <mount point>     <type>    <options>            <dump>   <pass>
 # / was on /dev/sda5 during installation
 UUID=ea8075fc-0703-4a5b-9b73-644f1cdca5f4    /             ext4     errors=remount-ro      0         1
 # swap was on /dev/sda6 during installation
 UUID=d533e07f-01d2-4ca2-b11b-9e3b07e51997      none            swap     sw                     0         0
# for /dev/sda2
UUID=783C2D063C2CC14C  /media/ajay/drive1  ntfs  defaults,noatime    0      2
# for /dev/sda3
UUID=E8D8AB69D8AB34AA  /media/ajay/drive2  ntfs  defaults,noatime    0      2
Note: We don’t need worry about, if the text entered fstab isn’t exactly under things like file system. All that matters is that the text is written in the right order. To save the configuration press 'Control + O' and hit enter, then to exit press 'Control + X' key. Now reboot the systems and you will see that the drives will be automatically mounted.


2. Auto-mounting Secondary HDDs :

First list all the available drives with lsblk
 $ lsblk  


Now the procedure is as same as the above, but for secondary HDDs put the device name instead of UUID. Open the fstab file with nano and put the below config on fstab file, just like above :
 # for /dev/sdb2
 /dev/sdb2  /media/extrdrv  ntfs defaults,noatime 0 2  

Then save the fstab file and reboot the system, and that's it. You have successfully mounted your external drives.

Conclusion :

By with the above steps we can auto-mount partitions and drives with the help of fstab config file. And at the above process if you messed up with fstab config file and theirs any error then in order to  restore the fstab file use the below commands :
 cd /etc/
 sudo rm fstab
 sudo cp /etc/backup/fstab /etc/
Running these commands will delete your modified fstab file and put a copy of the backup file in its place. After that just reboot your machine and everything will be back to normal.