How to auto mount hard drives in Linux


A few days ago I created a new Ubuntu 16.04 file server that hosts all of my files. I upgraded my hard drives from 5TB to 10TB. I mounted my drives, created Samba share and started working. A few days later there was a kernel updated, so I rebooted my system and found that hard drives were missing. Why? I forgot to create persistent mount or entries for those hard drives in the ‘fstab’ file that keeps track of all mounted storage devices.

Here is how we do it in Linux systems.

In this example I am using ‘ext4’ file system, if you are using other file systems such as ‘NTFS’, you need some extra work, we will talk about that later.

Finding Nemo: find your hard drives

First of all, you need to find the universally unique identifiers (UUIDs) for partitions that you want to auto-mount. I prefer UUID over block device name, such as ‘sda’, because UUID remains consistent across systems and reboots whereas ‘label’ can change from one operating system to another, depending on connected storage devices. To find the UUIDs of drives to be mounted, run this command:

$ sudo blkid

Which will give you an output like this:

swapnil@precious:~$ sudo blkid 
/dev/sda1: UUID="1C54-4788" TYPE="vfat" PARTUUID="882703bb-eea4-4014-8cc5-79dec006c151"
/dev/sda2: UUID="2c58cf82-667a-4dd3-9859-41cfa15c2b5d" TYPE="ext4" PARTUUID="d88a323e-3067-4ebe-820c-35d75304f871"
/dev/sda3: UUID="55bf412b-a26d-45bd-b313-d964d8564885" TYPE="swap" PARTUUID="0573d213-ba07-4940-b61d-1a881a60d927"
/dev/sdc1: LABEL="5tb1" UUID="b3457c55-5094-4889-8c71-8d384a99b3ca" TYPE="ext4" PARTUUID="9a9b48a4-af1d-4186-9199-4ca5194da788"
/dev/sdb1: UUID="fbba6d4a-73eb-401b-827a-85fc11207030" TYPE="ext4" PARTLABEL="primary" PARTUUID="33e185a6-8132-43b0-a14b-af2d4b63f140"
/dev/mapper/cryptswap1: UUID="3d2dd1cb-edd5-49dd-86b1-d98a5186748f" TYPE="swap"

Here ‘sdc1’ and ‘sdb1’ are my 5TB hard drives, whereas ‘sda’ is SSD where Ubuntu is installed. I will be mounting ‘sdc1’ and ‘sdb1’. If you don’t know which drive is which, as ‘blkid’ doesn’t show more information, you can always use ‘lsblk’ command to identify each hard drive by storage size.

swapnil@precious:~$ lsblk 

sda              8:0    0 111.8G  0 disk 
├─sda1           8:1    0    94M  0 part  /boot/efi
├─sda2           8:2    0   108G  0 part  /
└─sda3           8:3    0   3.8G  0 part 
  └─cryptswap1 252:0    0   3.8G  0 crypt [SWAP]
sdb              8:16   0   4.6T  0 disk 
└─sdb1           8:17   0   4.6T  0 part 
sdc              8:32   0   4.6T  0 disk 
└─sdc1           8:33   0   4.6T  0 part 

TO auto-mount these drives, so that they remain mounted between reboots, we will edit the ‘fstab’ file and create entries about these drives. Open the ‘fstab’ file using nano

$ sudo nano /etc/fstab

Add the partitions using the following structure (add new entries at the end of the current entries):

UUID="id_of_drive" /media/mount_point ext4 defaults,noatime 0 2

You can create new mount points, just keep them short and lower case. I created ‘prim_5’ and ‘sec_5’ as mount points. These are the lines that I added at the bottom of my ‘fstab’ file:

UUID="b3457c55-5094-4889-8c71-8d384a99b3ca"     /media/prim_5   ext4    defaults,noatime        0       2
UUID="fbba6d4a-73eb-401b-827a-85fc11207030"     /media/sec_5    ext4    defaults,noatime        0       2

Once done hit Ctrl+X which will ask if you want to save the file, click Y to save the file and exit. Reboot your system to see if the drives mount automatically.

Mounting NTFS partitions

If you are using NTFS then you need to be able to write to these partitions. Install ntfs-3g package for your distribution. Then follow the above step to finding the UUID and then open the fstab file.

The only difference is the name of the file system, instead of ‘ext4’, we use ‘ntfs-3g’:

UUID="b3457c55-5094-4889-8c71-8d384a99b3ca"     /media/prim_5   ntfs-3g    defaults,noatime        0       2
UUID="fbba6d4a-73eb-401b-827a-85fc11207030"     /media/sec_5    ntfs-3g    defaults,noatime        0       2

That’s it. If you have any questions, ask in the comments below.