315

How do you make HDDs and partitions mount at startup on Ubuntu?

I always keep my media and data files on separate partitions - one 2tb HDD and a 400 odd gig partition. I have been trying to swap the default folder location for the home directory to my other drives.

It works when I change the /home/user_name/.config/user-dirs.dirs file to the directories I want, but only until I reboot the system. I am only assuming that the drives not mounting is the problem with it, but it would be helpful either way. If any one knows another reason for why the directories change back to default each time that would be good also.

David Foerster
  • 36,264
  • 56
  • 94
  • 147
Harley Baker
  • 3,251

7 Answers7

382

GUI Method (recommended for newbies) - The Disks program will automatically edit /etc/fstab for you and not break anything. Type disks on the launcher or gnome-disks in the terminal start the Disks application.

Select Drive, then partition, then use More actions (cogs icon) button to access 'Edit Mount Options' menu. Turn off User Defaults because you would not be here if it was really set to automount. The rest should be obvious.

Disks application Disks application - cogs icon menu, about to click Edit Mount Options... Disks application - Mount Options dialog

Eliah Kagan
  • 117,780
Ev Dolzhenko
  • 4,489
  • 1
  • 15
  • 5
  • 56
    Just don't forget to uncheck "Automatic mount options". I was confused because auto-mount was not working even though the "Mount at startup" checkbox was checked in the grayed out UI. – Étienne Aug 15 '16 at 14:41
  • 25
    WAY easier than manually editing fstab. This is the best answer. – wordsforthewise Sep 28 '16 at 03:33
  • 4
    In 16.04 this way mount disks to /mnt instead of /dev – dstonek Dec 02 '16 at 15:54
  • 22
    Don't use sudo with GUI tools. gnome-disks will ask for permission when required. – nipunasudha Nov 14 '17 at 11:30
  • Note that this is not a package that can be installed or uninstalled but a built-in command. Tripped me up. – xjcl Aug 23 '18 at 16:15
  • 2
    "rest should be obvious" I liked it. – Adil Saju Oct 22 '18 at 14:06
  • 1
    The terminal answer included advice on creating a backup of fstab before editing. Doing that just saved me some trouble and might be worthwhile including in this answer as well (I changed the mount options of my system drive, ignoring the extra warning :) ) – smcs Mar 20 '19 at 12:01
  • @nipunasudha if I don't use sudo, it doesn't ask me for permissions, it just says "not authorized to...." – mcmillab Apr 04 '19 at 21:16
  • @xjcl on my operating system (elementary) it was not installed, but I could install it using apt install gnome-disks-utility – Pieter De Clercq Aug 02 '19 at 07:37
  • This is the way to do this. I messed up my boot... doing fstab. I encourage others to upvote this and downvote the fstab method so others like me do not follow the dangerous fstab method. Also remember to make a user friendly mount point. This works well. – Bhikkhu Subhuti Aug 21 '19 at 00:48
  • Just had a nasty crash after using the GUI. You better BACKUP your etc/fstab before you try anything fancy – Yuval Harpaz Feb 06 '20 at 09:00
  • This method has a drawback... you will not have any trash directory for the partition – Parag Katoch Jan 31 '21 at 09:23
  • what is the option for having writing rights on the disk as simple user? i tried rw but doesnot work...(FAT32 partition but i use 'auto' in 'disks' filesystem dialogue box). ubuntu 20.04.1 – brane stormer Feb 01 '21 at 21:04
  • 1
    Is there a reason why the default option is not to mount the other disks? – user3347814 Mar 15 '21 at 22:08
  • 2
    @Étienne. I'm using Ubuntu 22.04 but don't see the "Automatic mount options" anywhere. Could you perhaps elaborate? – Adriaan May 24 '22 at 07:57
  • @Adriaan I posted my comment 6 years ago, long before 22.04 existed. The GUI of 22.04 probably looks different. – Étienne May 25 '22 at 08:43
  • 2
    @Adriaan as far as I understand this is called "User session defaults" in 22.04 – Étienne May 25 '22 at 08:49
  • 1
    @Étienne What do you mean by unchecking automatic mount options? I don't have such a checkbox, still it does not mount drives at system startup. What I really need is asking for encryption keys when I login instead of hardcoding them to fstab plain text... – inf3rno Jul 15 '22 at 06:25
229

Terminal Method for experienced users only. This can break your boot.
If you are okay with this, fire up a terminal.

  1. [IMPORTANT] sudo cp /etc/fstab /etc/fstab.old - Create a backup of the fstab file just in case something unwanted happens. If something happens, you will need a bootable (live) usb. If you do not have one, use the GUI method instead.

  2. sudo blkid - Note the UUID of the partition you want to automount.

  3. sudo nano /etc/fstab - Copy the following line to the end of the file, save it and reboot afterwards to check if it worked.

  4. mkdir /my/path/tomount # to quote : "you must create the mount point before you mount the partition." see https://help.ubuntu.com/community/Fstab

Examples

A common setup is:

UUID=<uuid> <pathtomount> <filesystem> defaults 0 0

Use lsblk -o NAME,FSTYPE,UUID to find out the UUIDs and filesystems of the partition you want to mount. For example:

$ lsblk -o NAME,FSTYPE,UUID
NAME   FSTYPE UUID
sda
├─sda2
├─sda5 swap   498d24e5-7755-422f-be45-1b78d50b44e8
└─sda1 ext4   d4873b63-0956-42a7-9dcf-bd64e495a9ff

NTFS

UUID=<uuid> <pathtomount> ntfs uid=<userid>,gid=<groupid>,umask=0022,sync,auto,rw 0 0

Examples for the <> variables:

  • <uuid>=3087106951D2FA7E
  • <pathtomount>=/home/data/ # create this before rebooting
  • <userid>=1000
  • <groupid>=1000

Use id -u <username> to get the userid and id -g <username> to get the groupid.

(Note that specifying the sync option can slow down write performance as it disables the cache. async is the default.)

Screenshot of my computer's _fstab_ file

  • 15
    +1'd, fstab tutorial for free: http://www.linuxstall.com/fstab/ – Andrejs Cainikovs Jul 19 '12 at 10:13
  • hey sorry it has been a while I tried the solutions provided but none worked, with psydm I turned on the mount at start up function but it didnt work and one hdd didnt have that option, I also tried changing the fstab file both through the file itself and using the gedit function still nothing is there anything else that would do it? – Harley Baker Jul 31 '12 at 04:22
  • 9
    Careful about being too prescriptive with the mount options; not everybody's uid & gid will be the default 1000. – IlluminAce Mar 17 '13 at 22:17
  • 1
    Make sure you read more in to fstab. For me the example settings resulted in write speeds of KB/s. See https://help.ubuntu.com/community/Fstab for some helpful tips. – bbodenmiller Nov 15 '14 at 08:27
  • 9
    use 'id -u ' to get the uid and 'id -g ' to get the group id. this should be stated in the answer. –  Apr 02 '16 at 20:41
  • Very good, but I wondering the parameters on your command, like nouser, nosuid, is it affect anything? – Finn Dec 29 '16 at 01:37
  • 6
    @bbodenmiller sync option should be avoided here. It slows down disk writes because you ask for every byte to be flushed instantly instead of letting your computer optimize that for you. The answer should remove sync – Masadow May 24 '17 at 21:27
  • @Finn - In this case, nosuid was of no effect. This computer was a local machine where I was the admin. And these flags were used only out of my own ignorance regarding them. Check out the first answer and following comments in this thread to learn more - link Same goes for nouser. – Akshit Baunthiyal Jun 01 '17 at 05:05
  • Sorry for bringing this up from the dead but question. Why use group and user IDs exactly? Couldn't you just use the UUID and Mount point and other options and call it good? – BobserLuck Jun 01 '18 at 01:33
  • 3
    I read in man mount that in the case of media with a limited number of write cycles (e.g. some flash drives), sync may cause life-cycle shortening. – Enlico Oct 15 '18 at 17:31
  • Point 3: "Copy the following line to the end of the file" It is unclear to me what you mean by 'following line' here – smcs Mar 20 '19 at 12:14
  • @smcs don't think it means anything as far as i can tell... just write the UUID lines into your fstab file and fill in the blanks. Use blkid and lsblk to get information to help. – Eugene K May 10 '19 at 15:57
  • @EugeneK In the process became clear to me that the UUID line was meant by that. For anyone as clueless as me, this answer might benefit from pointing that out in Point 3 :) – smcs May 13 '19 at 09:55
  • This is not recommended for people who have never done this before. Use the GUI method instead. If you make a mistake, your machine will not boot. – Bhikkhu Subhuti Aug 21 '19 at 22:54
  • To reload after config without resetting, use sudo mount -av – Ryall Nov 10 '19 at 11:36
  • The key point to answer the question is that the options passed to mount (the 4th argument in the /etc/fstab row, after disk ID, mount point, and file system type) should contain the auto option. The defaults option provided in the example is a shortcut for a set of options, one of which is auto. – robertspierre Mar 13 '21 at 11:46
30

In Ubuntu follow these steps to auto-mount your partition:

  1. Open file manager and look left side on the devices listed.

  2. Choose the device you want to auto-mount on start-up by just clicking it and you will see the folders in the right pane shown for that device (partition), keep this window open.

  3. Go to your desktop Ubuntu search and type disks, you'll see disks shown in results.

  4. Click on disks and disks manager will start.

  5. You will see HDD's listed on the left pane and details of partitions on the right pane for each HDD you have.

  6. Now highlight the partition by clicking it, IMPORTANT (don't set mount on start-up for all partitions as it slows down the system).

  7. Make sure that the partition name you want to auto-mount have the same name by comparing the partitions in disk manager with the open partition window which you opened previously, by looking at the name of device at the top-left part of file manager (opened earlier), and looking the same name of drive or partition in the disks manager at the Contents field of the chosen partition in disks manager.

  8. Now after making sure that you have chosen the correct partition, in disks manager just click more actions icon, sub-menu list will open, choose edit mount options, mount options will open with Automatic mount options = ON, so you turn this off and by default you'll see that mount at start-up is checked and show in user interface is checked, then hit OK and restart your system, after log-in go to file manager you'll see the part you choose to mount at start-up is already mounted.

  • In 16.04 this way mount disks to /mnt instead of /dev – dstonek Dec 02 '16 at 15:57
  • @dstonek -- You have option to specify Mount Point in the Edit Mount Options at the step 8 above . That is, you can tell it to mount in /dev if you want it there.. – gare Apr 10 '18 at 15:47
11

When the system boots, it checks the file /etc/fstab (which stands for "filesystem table") for the list of partitions to mount automatically.

How to use this file is described here.

In brief:

The syntax of a fstab entry is :

[Device] [Mount Point] [File System Type] [Options] [Dump] [Pass]

You can add your own entries, but you'll need to know the device path (eg, /dev/sdb1), or their UUID (a big long string of numbers) to identify them.

thomasrutter
  • 36,774
7

use fstab

echo '/dev/[VolumeGroup]/[VolumeName] [custom-directory] ext4 defaults 0 2' >> /etc/fstab

Example:

echo '/dev/hdd/hdd /mnt/hdd ext4 defaults 0 2' >> /etc/fstab
echo '/dev/ssd/ssd /mnt/ssd ext4 defaults 0 2' >> /etc/fstab
Amin
  • 173
5

All the answers use a GUI tool, or /etc/fstab. My problem with these is that I run headless servers (no GUI) and, most importantly, that I am usually mounting secondary USB hard-drives, where fstab will prevent your system from booting up if the drive is missing, damaged, etc. This has happened to me in the past, and it has left stranded otherwise perfectly functioning systems.

My solution is to use cron. In particular, add this line to your crontab -e with the specific info of your system.

@reboot mount /dev/sdX /mnt/my-usb/

With this, cron will try and mount your drive upon every system start.

Warning: this relies on /dev/sdX to remain unchanged through reboots, which is not guaranteed. In other words, the drive under /dev/sda might become /dev/sdb on the next boot. But in my non-professional setting, it's been working.

If you need more reliability, you can create a shell or python script to search for the right /dev/sdX based on the serial number, and then mount it, and execute the script with cron and the @reboot trigger.

  • Here's how to mount using UUID to guarantee the same mounting across boots: https://unix.stackexchange.com/questions/349118/how-to-mount-by-uuid-without-using-etc-fstab – Barkles Apr 14 '22 at 16:11
1

Try "usbmount" package

sudo apt-get install usbmount

It will automatically mount USB devices

loo3y35
  • 511
  • 4
  • 9
  • 19
  • 10
    -1. It would be better to also include instructions on how to use usbmount. Just telling someone "install this software" isn't enough. – Parto Jun 18 '14 at 07:06
  • 1
    This is a script intended for non-desktop installs to automatically mount USB devices in /media/usb[0-7] when plugged in. If the drives need to be mounted during startup this might not be suitable, since it may not mount them until too late, and the mount point may not be predictable, though if all you need is for it to be mounted somewhere once startup is complete it should be fine. If you use a graphical desktop you shouldn't need this as it will have its own way of automatically mounting USB devices. – thomasrutter Oct 04 '18 at 01:38