Anything with the default settings in fstab should be mounted automatically, so you may not have it pointing to the correct device (especially since the internal drive is usually sda, and external drives are usually sdb, sdc, etc.) Here's how I would set up an external drive to automatically mount at boot:
1. Identify your device
Run sudo fdisk -l
to get a list of your connected drives. You should find one matching the size and partition settings of the drive you want to automatically mount. For instance, my 16gb flash drive looks like this:
Disk /dev/sdd: 14.9 GiB, 16007561216 bytes, 31264768 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos Disk identifier: 0x00000000
Device Boot Start End Sectors Size Id Type
/dev/sdd1 8192 31264767 31256576 14.9G c W95 FAT32 (LBA)
Since this is the only device on my system that's close to the correct size, my removable drive must be sdd, and it's primary partition must be sdd1. (Yours will likely be sdb or sdc; mine is such a high letter because I have a lot of drives in my computer.)
You can test to make sure that you have have the right device by running sudo mount /dev/device /mnt
and then checking the /mnt folder to see if the contents of that partition is correct. (Be sure to replace "device" with your own block device. For example, sudo mount /dev/sdb1 /mnt
.) To unmount the device again, use sudo umount /dev/device
.
2. Find your device's UUID
We want to do this because block devices may change. For example, right now my 16gb drive is sdd, but if I add more drives to my computer it may show up as sde, or sdf. At this point, my fstab would be trying to mount the wrong drive! Unlike the block device identifier, the UUID is built into the partition and never changes (unless you reformat your drive.) This makes it a much more reliable way of mounting removable drives.
To find your devices UUID, simly use the blkid command:
sudo blkid
For my drive, the line I'm looking for looks like this (I have replaced the actual UUID with Xs):
/dev/sdd1: UUID="XXXX-XXXX" TYPE="vfat"
Note that this tells me not only the UUID, but also the filesystem type (which we will need later.)
3. Edit your fstab file
This is the trickiest step. First, open up /etc/fstab as root in your favorite text editor:
sudo gedit /etc/fstab
(Note that if you're using the Mate desktop environment you should use pluma instead of gedit. KDE also has it's own text editor.)
Now you'll want to add a line to the end of this file that will cause your drive to be automatically mounted at boot. (It's good form to add a descriptive line (starting with #
) before your entry to let those who come after you know why this line was added.) In my case, it would look something like this (note that you WILL have to change this line to match the information gathered in the previous steps):
#External hard drive
UUID=XXXX-XXXX /media/exdrive vfat defaults,user 0 0
The first section of the line is the UUID of the partition you want mounted. Use the UUID you got from running blkid
.
The second section is what folder you want the partition mounted to. Be sure the folder actually exists! In my example, I could use sudo mkdir /media/exdrive
to create the folder I want to use. It may be a good idea to put this folder outside your home folder so that other users can still access the drive. You can always make a link to it if you want it more accessible.
The third section defines the filesystem your partition should be mounted as. Use the output from blkid
to find your filesystem type. The drive in my example has a fat32 filesystem, so I need to use vfat
. Alternately, you may have ntfs, ext4, or some other type altogether.
The fourth section specifies the mount options. This gets complicated, so if you want to do more reading on the subject you can do so here: https://help.ubuntu.com/community/Fstab#Options
For most people following this guide, defaults,user
should work fine.
The last two sections (0 and 0 in our example) have to do with the backup utility dump, and if and in what order fsck will check the disk. Neither should be needed in our setup, so it is safe to leave both at 0.
4. Test it!
Your computer should now automatically mount your drive at boot, and should still mount it to the correct place if connected after boot.
/home/yourname/mydata
is a real folder? If it isn't have you tried creating it? Sincemount -a
might create it for you when run manually, but at startup it won't. My suggestion would be to create a folder usingsudo mkdir -p /media/mystorage
so that it is owned by root and so that it exists on startup and change your fstab to use that instead. Since this could also be a permission issue along with a non-existent directory. – hazrpg Feb 27 '16 at 14:50