Mounting a USB Hard Drive on Linux

If you are using a GUI such as KDE or Gnome, USB Hard Drives will usually be mounted automatically - available to be used without any effort.

Unfortunately this isn't generally the case on headless (server) systems.

However it is easy to instruct the system to mount a drive manually, or to edit /etc/fstab so that it will be mounted when your computer boots up...

To manually mount a hard drive, we must first create a mountpoint - a directory in our filesystem from where the files on the drive will be accessible, for example:

sudo mkdir /media/usbdrive

Now we need to find out what our drive is "called" - i.e. the device it has been assigned.  The easiest way to do this is using:

sudo blkid

This will return a list of the various block-level devices (well, the partitions on them) - hard drives, SD cards, etc - available on the system, for example:

/dev/sda1: LABEL="SystemDrive" UUID="abcdef12-3456-7890-abcdef123456" TYPE="ext4"
/dev/sdb1: LABEL="MyUSBDrive" UUID="12345678-90ab-cdef-1234567890ab" TYPE="ntfs"

Work out which device you want to mount (in this case /dev/sdb1) and simply use:

mount device mountpoint

in our example this would be:

mount /dev/sdb1 /media/usbdrive

Linux should automatically work out the filesystem type (EXT2/3/4, NTFS, FAT, etc) - but if it does complain then you need to add the extra information:

mount -t ntfs /dev/sdb1 /media/usbdrive

Now if you navigate to /media/usbdrive (cd /media/usbdrive) and list the contents (ls) you will see the files from your hard drive!

If you want to leave a drive plugged in all the time, then you'll want to automate the mounting process - so it happens whenever you reboot the computer!

In order to do that, we need to add a line to the /etc/fstab file... using the information gathered from blkid earlier, the contents of mine would be:

UUID=abcdef12-3456-7890-abcdef123456 / ext4 defaults 0 1
UUID=12345678-90ab-cdef-1234567890ab /media/usbdrive ntfs-3g defaults,errors=remount-ro 0 0

To explain this a little more, each line instructs Linux to mount a specific partition to a particular mountpoint (here there are two lines, each beginning with "UUID").  The first parameter is the device to be mounted... it's safer here to use UUIDs (rather than /dev/sd*) because the device addresses may change from one boot to the next (but the UUID is a unique and static identifier for the device).  The second and third parameters are where to mount, and the filesystem type (note ntfs-3g rather than ntfs here!)  The next parameter is the options to use when mounting - there's a lot of them, but defaults is generally fine!  I also use errors=remount-ro for my USB drives to reduce the chance of corruption if something goes wrong!  The final two parameters are the "dump-frequency" (just leave this at 0) and the "fsck pass number" - this controls the order in which filesystems are checked for errors during a boot... your root partition should be 1, others can be either 2 (checked after root) or 0 (not checked during boot).

Now, when you reboot the computer, your drive should be mounted automatically!