Skip to content

Instantly share code, notes, and snippets.

@light9
Forked from CreatorB/automountUbuntu
Created July 15, 2023 10:53
Show Gist options
  • Save light9/1f0c1f281035b913fadaeacde3201b59 to your computer and use it in GitHub Desktop.
Save light9/1f0c1f281035b913fadaeacde3201b59 to your computer and use it in GitHub Desktop.
How to Set Ubuntu Automatically Mount Partition at Startup with fstab
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.
# res : binarytides
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment