131

I have recently installed a new hard drive to my Ubuntu 10.0.4 box. I used System -> Administration -> Disk Utils to format the disk (ext4 file type) - but did not create a partition (is this advisable?).

I have manually mounted the drive as /mydata.

I want to have the machine automatically mount the device on startup/reboot. How may I do this?

[[Update]]

This is the output from running sudo fdisk -l on my system:

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: 0x000858bf

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1       59334   476595200   83  Linux
/dev/sda2           59334       60802    11789313    5  Extended
/dev/sda5           59334       60802    11789312   82  Linux swap / Solaris

Disk /dev/sdb: 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: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

Disk /dev/sdc: 16.0 GB, 16026435072 bytes
254 heads, 63 sectors/track, 1956 cylinders
Units = cylinders of 16002 * 512 = 8193024 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1955    15641929    c  W95 FAT32 (LBA)
Suhaib
  • 4,110
  • there's a disks utility in 12.04 ... have a look you may find one in your version. It allows you to edit mount option on a partition there's one for startup – Moog Jun 21 '12 at 15:51
  • Obtain the UUID of the disk and add it to the /etc/fstab as demonstrated in this tutorial. – navule Nov 16 '20 at 17:41

8 Answers8

127

First of all your /dev/sdb isn't partitioned. I am assuming this is the disk you want to mount.

WARNING: THIS WILL DESTROY ANY DATA ON YOUR TARGET DISK


Run sudo fdisk /dev/sdb

  1. Press O and press Enter (creates a new table)

  2. Press N and press Enter (creates a new partition)

  3. Press P and press Enter (makes a primary partition)

  4. Then press 1 and press Enter (creates it as the 1st partition)

  5. Finally, press W (this will write any changes to disk)


Okay now you have a partition, now you need a filesystem.

  1. Run sudo mkfs.ext4 /dev/sdb1

  2. Now you can add it to fstab

    You need to add it to /etc/fstab use your favourite text editor

    Be careful with this file as it can quite easily cause your system not to boot.

    Add a line for the drive, the format would look like this.

    This assumes the partition is formatted as ext4 as per mkfs above

    #device        mountpoint             fstype    options  dump   fsck
    
    /dev/sdb1    /home/yourname/mydata    ext4    defaults    0    1
    

Then on the next reboot it will auto mount.

This web page has a lot of useful info on fstab

squareborg
  • 10,517
  • 5
    +1 for pointing out the danger with editing fstab (thats why I thought I'd come in here and ask first). As it happens, my new drive is not partitioned, I formatted the entire drive using file type ext4, and the device name (obtained using Disk Utility GUI app) is /dev/sdb. Following from the syntax you gave, the new entry should be /dev/sdb /mydata ext4 defaults 0 1. I want the drive to be mapped to /mydata (from the root). Is that the correct syntax to use? – Homunculus Reticulli Jun 21 '12 at 15:48
  • 3
    No it should be /dev/sdb1 if it is the only partition on the disk. you can check with "sudo fdisk -l" this will show you the disk /dev/sdb and then its partitions so /dev/sdb1 will be your ext4, and the mount point would be "/mydata" which would mean it was a folder that already exists on the root of the drive called mydata. FYI use the existing folders /mnt/mydata or possibly/var/mydata its what they're for. – squareborg Jun 21 '12 at 16:12
  • Yikes!. Thanks for the feedback. I have added the output of running sudo fdisk -l on my machine. Please advice how to proceed. MTIA. – Homunculus Reticulli Jun 21 '12 at 16:20
  • I'll add it to my answer – squareborg Jun 21 '12 at 16:25
  • Steve: Have you noticed that fdisk reports Disk /dev/sdb doesn't contain a valid partition table in the output - should I be worried?. I remember reading somewhere that some apps may expect me to have created a partition on the disk and if they attempt to write to the disk - I'll lose the data on that disk. True?. If so, I will start from scratch again (instructions please ...). I can afford to lose the data at this stage but definitely not later on. – Homunculus Reticulli Jun 21 '12 at 16:34
  • Yes i explain in answer.. – squareborg Jun 21 '12 at 16:37
  • Steve: You Sir, are a STAR! – Homunculus Reticulli Jun 21 '12 at 17:50
  • @Shutupsquare I met a similar issue today. I did some test and I found that you don't need to create partition at all. Without any partition, you can use /dev/sdb directly in /etc/fstab file instead of /dev/sdb1 – Tyler Liu May 04 '14 at 06:34
  • 4
    You should use blkid to find the uuid of the drive and use that as the filesystem. Otherwise the partition maybe moved and the incorrect device would be mounted – TameHog May 15 '15 at 18:38
  • It worked for me, good job and nicely explained. – Mercy Flicker Dec 14 '16 at 09:12
  • 2
    This actually locked me out of my Raspberry Pi root account. Very dangerous to modify this file. – IgorGanapolsky Jan 02 '17 at 23:43
  • same here as @IgorG. – Alex Szabo Jan 23 '17 at 22:15
  • 3
    It's worth using nofail as an option so the machine still boots when the drive isn't connected. More info in the arch wiki. – ki9 Mar 20 '17 at 11:24
  • 1
    Good answer, a recommendation is to use mount -a before restarting your machine. – Waheed Sep 29 '18 at 14:21
  • 1
    for anyone who gets stucked out of its raspberry, just like Igor, Alex and me, here's a link to get things back on feet: http://clarkle.com/notes/emergecy-mode-bad-fstab/ – Nikolai Dec 29 '19 at 02:08
  • other filesystems should have a fs_passno of 2 I think – Long Oct 30 '20 at 12:45
  • Isn't /dev/sdb1 /mnt/ssd ext4 defaults 0 1 more fail proof than /dev/sdb1 /home/yourname/mydata ext4 defaults 0 1? – Cloud Cho Jan 23 '21 at 07:19
  • "fsck sets the order for file system checks at boot time; For the root device it should be 1. For other partitions it should be 2, or 0 to disable checking." – run_the_race Nov 28 '21 at 15:04
92

The GUI solution for me is gnome-disks

sudo gnome-disks

gnome-disks

Then with the configuration button you can "edit mount options", feel free to give the destination of your mount point and it will be saved into the /etc/fstab automatically enter image description here

Note: the nofail option that causes the system not to hang if this mount point is not available, could be useful in case of potential unreachable filesystem on boot such as USB, NFS, etc.

Philippe Gachoud
  • 5,900
  • 3
  • 43
  • 50
  • 10
    This is the safest way I have come across ever. I like to be the command line ninja, but for very critical operations like this, fail safe ways are always preferred. – Yaksha Oct 30 '17 at 03:58
  • 1
    what is the syntax for the read-write option in ubuntu 20.04.1? i tried 'rw' but DOESNOT work. FAT32 with a single partition... – brane stormer Feb 01 '21 at 21:12
18

To set the drive to mount automatically each time you boot the computer

You’ll need to edit /etc/fstab:

Just press Ctrl+Alt+T on your keyboard to open Terminal. When it opens, run the command below.

gksudo gedit /etc/fstab

Add this line to the end:

/dev/sdaX /media/mydata ext4 defaults 0 0

Note: Make sure to replace the X with the right value for your situation.

Or you can use Storage Device Manager, from the software centre.

Dustin
  • 2,103
Mitch
  • 107,631
  • 2
    It should be noted that the /dev/hda2 in the example above can be replaced by the uuid of the device. The uuid is a long number string that identifies uniquely the hard disk. To find it, use the command "sudo blkid". It will list all drives connected on the system. – frepie Jul 11 '16 at 02:38
  • @frepie Please note, UUID is used only when Disk is GPT. Incase of MBR Disk, use /dev/sdaX . – MahendraUnlimited Jun 14 '17 at 05:25
9

The fstab file lets the system know which drives to mount (or how to mount them, at least).

sudo edit /etc/fstab

then add a line like this one:

/dev/sdb1      /mydata   ext4   defaults   0   0

Of course, replace the values to suit your actual locations:

  • /dev/sdb1 should be the device and partition for the disk you want to mount.
  • /mydata is the place where you want to mount it.
  • ext4 is the type of filesystem it's using. If you're not sure, issue the mount command without arguments when the drive is mounted (even if you mounted it manually), it will tell you the filesystem type. (in 12.04 you may simply type auto which determines the filesystem for you at launch)
  • The other values (defaults 0 0) are OK like I show them.

If you want to learn more, you can read man fstab.

roadmr
  • 34,222
  • 9
  • 81
  • 93
8

If you are using FAT32 or NTFS file system using the following:

For NTFS:

/dev/sda3   /media/entertainment    ntfs-3g rw,auto,user,fmask=0111,dmask=0000,noatime,nodiratime   0   0
/dev/sda4   /media/other    ntfs-3g rw,auto,user,fmask=0111,dmask=0000,noatime,nodiratime   0   0

noatime and nodiratime are some optimizations. If you want you can remove them.

For FAT32 replace ntfs-3g with vfat.

F Manni
  • 3
  • 2
Abhijeet
  • 1,629
7

You could do it via /etc/fstab.

For example, if you add

/dev/hda2   /myfiles    ext2    defaults    0    0

It means that the device/partition located at /dev/hda2 will be mounted to /myfiles using the file system ext2, with default mount options and no dumping and no error-checking enabled.

W. W.
  • 101
6

fstab will help you for sure.

For those lazy and cautious ones who want to use a simple and self-explanatory GUI there is a tool called Storage Device Manager (pysdm). The tutorial here will do basically the same as other answers in this thread suggest to do, but the tool will accomplish it with a couple of clicks and no need to enter configurations manually into fstab config file.

sudo apt-get install pysdm(Ubuntu) or sudo yum install pysdm(Fedora)

Nothing should go wrong, but just in case, backup the fstab conf: sudo cp /etc/fstab /etc/fstab.old

Now run the Storage Device Manager and select your preferred partition, click "Assistant":

enter image description here

PySDM assistant settings for automounting EXT4 partition: enter image description here

PySDM assistant for automounting NTFS partition: enter image description here

Reboot and see that your drive gets automatically mounted now.

You can learn more about assistant settings from this article, as it explains the process in details and my answer is based on it.

UPDATE: pysdm has been removed from the Ubuntu archives (AskUbuntu page), sudo apt-get install pysdm does not work anymore.

Avoid using pysdm for now.

It's been removed from the Ubuntu archives for a reason:

please remove Pysdm from Ubuntu repositories. It is obsolete (not updated since June 2006 !!), buggy (https://bugs.launchpad.net/ubuntu/+source/pysdm) , doesn't handle UUIDs (which is now fstab standard in all supported Ubuntu versions !!!) and it has already been removed from Debian repositories.

Erik Kaju
  • 161
  • 1
  • 3
2

If you do not want to partition or cannot afford to, you can use udisks to mount or unmount devices or disks. The UUID method is the most stable one as other ID methods can change if you rename your label for example.

udisks --mount /dev/sdb
udisks --mount /dev/disk/by-uuid/70439c63-de2c-4319-a832-0dee5ea05fc5
udisks --mount /dev/disk/by-label/HDD

It will in fact do the same as you would do by clicking the device on nautilus.

nautilus mount

Just add it as a "startup application" to preform at login.

To unmount, (if you need to):

udisks --unmount /dev/sdb

Will do the same as clicking the unmount arrow in nautilus.

One disadvantage over the fstab method is the device will be mounted in /media/disk_label with no option to choose another mount-point.

One advantage is you do not need root access to mount or unmount the drive.

More info over different methods of auto-mounting : AutomaticallyMountPartitions