The Raspberry Pi's operating system is installed on a limited-sized microSD card. Here we show how to mount a removable external USB drive for additional space.
Table of contents
Introduction
Apart from primary storage, the Raspberry Pi can also utilise external storage such as USB sticks, USB storage drives or Flash drives). This post will show how to mount an external USB drive to a Raspberry Pi using terminal commands.
- Get the Raspberry Pi 4B 4GB Starter Kit from Amazon.com
- Get the Raspberry Pi 4B 8GB Starter Kit from Amazon.com
- Get the 512GB USB 3.0 Flash Drive from Amazon.com or BangGood
Assumptions and requirements
Fully workable Raspberry Pi with Raspbian installed and connected to the internet. Without a keyboard and screen, the process can also be done using a PC and PuTTY.
Step 1 Check the hardware
External USB drives can be in the form of a USB stick drive, Flash drive or a USB storage drive. With regards to additional power requirements, USB memory sticks should be fine, but most external HDDs will require either their own power supply or a USB hub to be able to work properly.
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
terminal command:
blkid
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. It can be installed using apt-get
:
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 the chmod
and the chown
terminal commands:
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 point directory known and prepared, the external USB drive can be mounted with the mount
terminal command:
sudo mount /dev/sda1 /mnt/usb
To test whether mounting took place correctly, use the df
terminal command:
df
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
using nano
:
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:
- <name> is the device name or other means of locating the partition or data source. The UUID or boot partition name can be used.
- <dir> is where the data is to be attached to the file system (the local mount point directory).
- <type> is the file system type or the algorithm used to interpret the file system.
- <options>, including if the file system should be mounted at boot. In this case, it will be the default.
- <dump> is the dump frequency that adjusts the archiving schedule for the partition (used by dump). In this case, it will be 0.
- <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
Press Ctrl + X and then Y to save and exit Nano.
Step 5 Unmounting the USB drive
To unmount the USB again, the umount
terminal command can be used:
sudo umount /mnt/usb
Conclusion
The Raspberry Pi can utilise external storage such as USB sticks, USB storage drives or Flash drives). Here we showed how to mount an external USB drive to a Raspberry Pi using terminal commands.