Ubuntu – automatically mount partition at startup with fstab

Auto mount partitions

On linux the hard drive partitions are generally not mounted at boot. Clicking on the drive icon inside a file manager like nautilus or dolphin mounts them first and then shows the files. But it is possible to get the drives mounted right at boot.

The configuration file in this case is /etc/fstab. It contains options for each drive and the settings for mounting it at startup. Check your /etc/fstab file. It should look similar to this

# /etc/fstab: static file system information.
#
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
# / was on /dev/sda5 during installation
UUID=9de0aab4-e64c-49c8-af55-cc7375a97dd6 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda6 during installation
UUID=31a6807b-3b3e-4f9d-95c2-ead64d0c7009 none            swap    sw              0       0

Now if a partition is to be mounted at startup , a line for that partition has to be added to this fstab file.

List out the partitions

Use the fdisk command to list of all partitions

$ sudo fdisk -l

Here is what the ouput would look like

Disk /dev/sda: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000ef50d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        9138    73400953+   7  HPFS/NTFS
/dev/sda2            9139       60801   414982985+   f  W95 Ext'd (LBA)
/dev/sda5            9139       22192   104856192   83  Linux
/dev/sda6           22193       22323     1052226   82  Linux swap / Solaris
/dev/sda7           22324       35377   104856223+  83  Linux
/dev/sda8           44942       60801   127395418+  83  Linux

Get the UUID of the partition

Next use the blkid command to get the UUID of partitions. The uuid is necessary to add the partitions to the fstab file. UUID numbers are used to uniquely identify a storage device like hard disk partition or a usb flash drive.







$ sudo blkid
/dev/sda1: UUID="2A64794864791831" TYPE="ntfs" 
/dev/sda5: UUID="9de0aab4-e64c-49c8-af55-cc7375a97dd6" TYPE="ext4" 
/dev/sda6: UUID="31a6807b-3b3e-4f9d-95c2-ead64d0c7009" TYPE="swap" 
/dev/sda7: UUID="eba07f1f-b287-456a-b3d6-1c40d7b28a60" TYPE="ext4" 
/dev/sda8: UUID="475abb5b-471f-4a6f-a589-782f3afc427f" TYPE="ext4"

Note down the UUID of the partitions that you want to mount at startup.

Add partition to fstab

Now add the partition to the fstab file. If the partition already exists in fstab, then you just need to modify the options column to get it mounted every time. The fstab file can have comment lines, starting with the hash symbol.

# /etc/fstab: static file system information.
#
# Use 'blkid -o value -s UUID' to print the universally unique identifier
# for a device; this may be used with UUID= as a more robust way to name
# devices that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    nodev,noexec,nosuid 0       0
# / was on /dev/sda5 during installation
UUID=9de0aab4-e64c-49c8-af55-cc7375a97dd6 /               ext4    errors=remount-ro 0       1
# swap was on /dev/sda6 during installation
UUID=31a6807b-3b3e-4f9d-95c2-ead64d0c7009 none            swap    sw              0       0


# 100GB /dev/sda7
UUID=eba07f1f-b287-456a-b3d6-1c40d7b28a60 /media/eba07f1f-b287-456a-b3d6-1c40d7b28a60               ext4    errors=remount-ro,auto,exec,rw,user 0       0

# 121GB /dev/sda8
UUID=475abb5b-471f-4a6f-a589-782f3afc427f /media/475abb5b-471f-4a6f-a589-782f3afc427f               ext4    errors=remount-ro,auto,exec,rw,user 0       0

For example take the line :

UUID=eba07f1f-b287-456a-b3d6-1c40d7b28a60 /media/eba07f1f-b287-456a-b3d6-1c40d7b28a60               ext4    errors=remount-ro,auto,exec,rw,user 0       0

First part is the UUID , which is fetched from the command blkid.

Next is the path where the drive should be mounted. So first the directory /media/eba07f1f-b287-456a-b3d6-1c40d7b28a60 should be created

Next the file system type, here its ext4. It can be ext3 for older file systems.
Then comes the options errors=remount-ro,auto,exec,rw,user

remount-ro means remount partitions incase of read errors.
auto - Automatically mount partitions at startup
exec - Give users permission to execute files on this partition
rw - Give read write permission
user - Allow all non-root users to mount this partition

In the options column, make sure that exec comes after user. Because the option user will automatically specify noexec. So it won't be possible to execute files in that particular partition.

user,errors=remount-ro,auto,exec,rw
user permits any user to mount the filesystem. This automatically implies noexec, nosuid, nodev unless overridden. If nouser is specified, only root can mount the filesystem. If users is specified, every user in group users will be able to unmount the volume.

So if you want files to be executable in the other partitions, make sure that exec comes after user.

Save the fstab file and next time your restart Ubuntu , the partitions should be already mounted.

Last Updated On : 26th July 2013

Subscribe to get updates delivered to your inbox

6 Comments + Add Comment

  • Man… that order of user + exec thing… finally you help me get it solved ;))

  • Oh man, how long do I browse blogs and all the forums to learn to mount via CLI. Then when using the UUIDs in ‘Raspberry Pi’ I read the first time about an order in the mount-options. Don’t know how much problems I wouldn’t have had when mounting as USER if someone before YOU would have talked about ‘exec’ after ‘user’.

    “And the light shineth in darkness; and…”
    Keep on blogging, Silver Moon!

    Yours, Dashia

  • It was useful, but I got a bit lost when you say “Now add the partition to the fstab file.”
    For future references, I did this:

    Write in terminal “gksudo gedit /etc/fstab” —> this will open the file so you can edit it.

    Thanks for the help :)

    • thanks for pointing it out, will add that information to the post.

  • good explanation

  • thanks! Pretty useful!

Leave a comment