USB hard disk or flash drive with Raspberry Pi

Note: You will need a powered hub to use an external USB drive as the Raspberry Pi cannot usually supply enough power for reliable operation.

List your file systems

Use any or all of these commands:

sudo fdisk -l
sudo mount -l
df -h

To find the UUID of each drive’s partition(s) (These are assigned during partition creation.) ou are looking for sda* drives.

ls -laF /dev/disk/by-uuid/

total 0
drwxr-xr-x 2 root root 160 Oct 25 22:05 ./
drwxr-xr-x 8 root root 160 Oct 25 22:05 ../
lrwxrwxrwx 1 root root 10 Oct 25 22:05 6602fe7b-58f8-4d71-87fa-612f4848c94c -> ../../sda1
lrwxrwxrwx 1 root root 15 Oct 25 22:00 74BD-74CF -> ../../mmcblk0p1
lrwxrwxrwx 1 root root 10 Oct 25 22:05 c1623057-0041-38cc-9b91-66bf9ad29c28 -> ../../sda2
lrwxrwxrwx 1 root root 10 Oct 25 22:05 de6b9855-3a0c-456b-96f3-b2029b41632d -> ../../sda4
lrwxrwxrwx 1 root root 10 Oct 25 22:05 df335544-e195-3569-9fc5-52d5f88d121c -> ../../sda3
lrwxrwxrwx 1 root root 15 Oct 25 22:00 ec2aa3d2-eee7-454e-8260-d145df5ddcba -> ../../mmcblk0p2

To mount a USB drive

sudo mkdir /mnt/usbdrive — or whatever you want to call it
sudo mount /dev/sda[number] /mnt/usbdrive — this mounts the specified drive
ls /mnt/usbdrive — this should show that the drive is mounted

Before disconnecting a USB drive

sudo umount /dev/sda[number]

Unmount a drive

sudo umount /dev/sda1

You may need to use the -f force option if the drive will not dismount.

sudo umount -f /dev/sda[number]

Note: If you use the shutdown -P -h 0 command to power down the Pi you do not need to use umount.

Format A Drive

To change the file system of a drive you must first format it. You will lose all data on the drive when you format it. Linux allows you to format any supported disk format using the mkfs tool. In the examples below you will notice an option followed by ‘untitled‘. These are optional volume labels to name your drive.

First unmount the drive, then use any of the following commands to format the drive to any of these file formats:

To format a drive to EXT3 (Linux) -> sudo mkfs.ext3 /dev/sda1 -L untitled
To format a drive to EXT4 (Linux) -> sudo mkfs.ext4 /dev/sda1 -L untitled
To format a drive to HFS+ (Mac OS X) -> sudo mkfs.hfsplus /dev/sda1 -v untitled
To format a drive to FAT32 (DOS and legacy Windows): -> sudo mkfs.vfat /dev/sda1 -n untitled
To format a drive to NTFS (Windows) -> sudo mkfs.ntfs /dev/sda1 -f -v -I -L untitled

  • -f Fast Format. Due to the poor performance of 3g.ntfs on the Pi, highly recommend using the less CPU intensive fast format mode.
  • -v Verbose. By default the NTFS status output is limited so this lets you know what is happening.
  • -I Disable Windows Indexing. This improves the write performance of the drive but it will mean Windows Search queries used on this drive will take longer.

You may have to install additional disk formats:

Add Apple OS X HFS+ read/write supportsudo apt-get install hfsutils hfsprogs hfsutils
Add Windows NTFS read/write support -> sudo apt-get install ntfs-3g
Add Windows/DOS FAT32 read/write support -> sudo apt-get install dosfstools

Automatically Mount A Drive

You must add the drive’s information to the fstab settings file located in /etc/fstab. Run nano to edit /etc/fstab. The -Bw options backup the file and not to use line-wrap.

sudo nano -Bw /etc/fstab

Do NOT change existing entries as the two /mnt/mmcblk0p entries are there to mount the SD card. Add the following to the bottom of the file:

/dev/sda1 /mnt/usbdisk auto defaults,user 0 1

  • /dev/sda1 Is the location of the drive to mount.
  • /mnt/usbdisk Is the mount point, which is the folder to access the content of the drive.
  • auto Is the file system type, here you can set ‘auto‘ or force a file system type such as ext2, ext3, ext4, hfsplus, ntfs, vfat.
  • defaults,user are mount options. You normally need to only supply ‘defaults‘. Though there are some others that maybe useful such as ‘ro‘ for read-only or ‘user‘ to enable write permission for all users.
  • Use a non-spaced comma to separate multiple options:
  • 0 a binary value used for debugging. It is best to keep this set at zero.
  • 1 is the pass number for a file system check at boot. 0 (zero) to disable or 2 to enable.

Save the changes to fstab:

[Ctrl] x
Y at the Save modified buffer prompt.
[Enter] for the File name to Write: /etc/fstab prompt.

The drive will mount at boot as long as it is attached to the Pi. If you want to mount the drive after you have plugged it in, use mount with the automatic option.

sudo mount -a

See also

Use USB hard disk & flash drives with your Raspberry Pi