Mounting an external USB drive to the Raspberry Pi

Mounting An USB Drive To The Raspberry Pii
Published: by
Last updated:
Categories
Raspberry Pi

The Raspberry Pi’s operating system runs from a limited size, permanently inserted microSD card. This post will show how to mount a movable external USB drive for additional space, or purely for transportation purposes.

Assumptions

Fully workable Raspberry Pi with Raspbian installed and connected to the internet. Without a keyboard and screen, the process can also be done through PuTTY.

Step 1 Check the hardware

External USB drives can be in the form of an USB stick drive or USB storage drive. With regards to additional power requirements, USB memory sticks should be fine, but most external HDDs will require either its own power supply or a USB hub to run in a stable manner. Also read a little more about the Raspberry Pi’s USB ports here.

The Raspberry Pi should be switched on after the  external drive is plugged in.

To check if the USB drive is attached correctly use the blkid command:

blkid

Mounting An USB Drive To The Raspberry Pii

The USB drive should show up somewhere. In this example a USB stick drive is used for demonstration. blkid shows that a drive with the LABEL="DISK_IMG", TYPE="vfat"UUID="01F7-01E5" and a boot partition name of /dev/sda1 is connected.

The TYPE field shows the file system type and the boot partition name will be the USB drive’s mounting directory.

If TYPE is NTFS, ntfs-3g needs to be installed:

sudo apt-get install ntfs-3g

Step 2 Creating the local mount point directory

Although any directory can be used, the root /mnt directory is assumed to be used for mounting purposes. Assuming the mount directory will be /mnt/usb, it can be created by using:

mkdir /mnt/usb

Note that the Linux directory structure is case sensitive. To make it fully accessible to all users, use:

sudo chmod -R 777 /mnt/usb
sudo chown pi:pi /mnt/usb

This will be the local mount point directory.

Step 3 Run the mount command

With the USB drive’s mounting directory and the local mount mount point directory known and prepared, the external USB drive can be mounted with:

sudo mount /dev/sda1 /mnt/usb

To test whether mounting took place correctly, use:

df

Mounting An USB Drive To The Raspberry Pii

From this it is visible that the /mnt/usb directory is mounted to the /dev/sda1 directory.

Step 4 Automounting the USB drive on boot

The USB drive can either be mounted/unmounted manually every time it is required or it can be mounted automatically at each boot.

To mount the USB drive automatically every time at each boot, the mounting command can be added to fstab:

sudo nano /etc/fstab

Ignore the existing content of the fstab file. All that is needed is to add the new mounting entry at the bottom of the file.

# <name>        <dir>       <type>    <options>      <dump> <pass>
/dev/sda1       /           ext4      defaults       1      1
/dev/hdxx       /usr        ext4      defaults       1      1
/dev/sda5       swap        swap      defaults       0      0

Looking at the example fstab file above, the columns are as follows:

  1. <name> is the device name or other means of locating the partition or data source. The UUID or boot partition name can be used.
  2. <dir> is where the data is to be attached to the file system (the local mount point directory).
  3. <type> is the file system type or the algorithm used to interpret the file system.
  4. <options>, including if the file system should be mounted at boot. In this case it will be defaults.
  5. <dump> is the dump frequency which adjusts the archiving schedule for the partition (used by dump). In this case it will be 0.
  6. <number> is the pass number, which controls the order in which fsck checks the device/partition for errors at boot time. The root device should be 1. Other partitions (including external drives) should be either 2 (to check after root) or 0 (to disable checking for that partition altogether).

For this example add the following line to the bottom:

# <name>      <dir>     <type>  <options>   <dump> <pass>
/dev/sda1/    /mnt/usb  vfat    defaults    0      0

Ctrl x & y to save out.

More about the fstab file can be read on the Debian wiki.

Step 5 Unmounting the USB drive

To unmount the USB again, use:

sudo umount /mnt/usb
About the author
Renier busies himself with improving his English writing, creative web design and his websites, photoshopping, micro-electronics, multiple genres of music, superhero movies and badass series.
Behind the Scenes is a free, informative website. If you find value in any of our content, please consider making a donation to our cause.
Donate via PayPal

Save, share & Disqus

Use the buttons below, on the left or the bottom of this page to share this post. Your comment is important, but don't be a knob. Keep it constructive and polite.

Comment via Disqus

Disqus is a worldwide comment hosting service for web sites and online communities. This secure platform ensures a pleasant commenting environment which is manageable from one account. Use the Login button to sign up.

More home network related posts

Communicating between 2 Raspberry Pi's using sshpass
Communicating between 2 Raspberry Pis using sshpass
10 September 2016 | Updated 24 October 2018
sshpass is a Linux software package that can be used to establish a direct (secure) connection between two Linux operated computers. This connection allows ‘remote’ access to a ‘local’ computer to run commands ‘locally’. Communicating between 2 Raspberry Pi’s using sshpass will show you how. More…
Categories
Raspberry Pi
Understanding common network protocols
Understanding some common network protocols
7 September 2016 | Updated 17 November 2017
Deeper exploration into the world of inter-computer communications will reveal more and more types of communications (e.g. radio waves, Ethernet, Bluetooth, etc.), their interfaces and the network protocols they use. More…