Working on your Raspberry Pi from the inserted SD card is fine for some applications but if you want to store a significant amount of data or perform more intensive IO operations then you’re going to need to attach an external hard drive. Connecting a drive over USB3 should mean you get a decent throughput and you have access to huge drives – 20TB is easy to get as I write.
This guide will show you how to permanently mount a USB drive to a location in the filesystem. It is assumed you have already installed Raspberry Pi OS and it is up to date and running fine. Note that if you are running the desktop version of the OS it will probably automatically mount a drive when it’s plugged in. We’ll be working with the Lite version of the OS here as we’re setting up a server and don’t need the desktop environment.
Finding the Drive
Start by plugging the drive into the Raspberry Pi. I strongly suggest you use a USB3 port (they are blue) as they are much faster than the USB2 ports. USB3 also has the ability to provide enough power to run the drive with the caveat this is usually only true for the smaller portable drives and you have to be supplying the Raspberry Pi itself with enough power. The official Raspberry Pi power supply certainly seems capable of powering one such drive.
Now, at a terminal, run the command “lsblk” with or without the -f flag like this:
doozer@sever:~ $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 3.6T 0 disk ├─sda1 8:1 0 128M 0 part └─sda2 8:2 0 3.6T 0 part mmcblk0 179:0 0 29.7G 0 disk ├─mmcblk0p1 179:1 0 256M 0 part /boot └─mmcblk0p2 179:2 0 29.5G 0 part / doozer@server:~ $ lsblk -f NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINT sda ├─sda1 └─sda2 ntfs Maxtor 38AA22BFAA227990 mmcblk0 ├─mmcblk0p1 vfat FAT32 boot 0F82-DECC 224.5M 12% /boot └─mmcblk0p2 ext4 1.0 rootfs 51c98938-6b08-4329-bd74-79c3efcf0539 25.6G 8% /
The addition of the -f flag gives information about the filesystems that are found on the disks. In the output shown above you can see that my operating system is on “mmcblk0”, that the drive is 32GB and that it has two partitions: /boot and /. The drive of interest here is “sda” as that’s the drive that I’ve just plugged in, as you can see it’s a 4TB Maxtor drive. My eventual goal is to make this drive available to my family as a backed up location for all their files.
You can also get hardware information from the “lshw” command. You may need to install this (sudo apt install lshw) before running it though. This shows us that the actual vendor of the drive is Seagate which isn’t surprising as Seagate bought Maxtor in 2006. Note, the UUIDs etc have been changed to protect the innocent.
doozer@server:~ $ sudo lshw -class disk *-disk description: SCSI Disk product: M3 Portable vendor: Seagate physical id: 0.0.0 bus info: scsi@0:0.0.0 logical name: /dev/sda version: 9300 serial: NX134NHG size: 3726GiB (4TB) capabilities: gpt-1.00 partitioned partitioned:gpt configuration: ansiversion=6 guid=46829f25-5646-4431-a49a-6f3ca22adee3 logicalsectorsize=512 sectorsize=4096
You can also use “fdisk”, “hwinfo”, “df” and examine “/dev/disk” folder to find out some or all of this information. What I’ve got is already more than enough for my needs though so I won’t go into that here.
Confirm the Disk ID and Install Drivers
From the information above I have determined that I want to mount “/dev/sda2”, that it has a UUID of “38AA22BFAA227990” and it is formatted with NTFS. These are important pieces of information so it would be good to confirm them:
doozer@server:~ $ sudo blkid /dev/sda2 /dev/sda2: LABEL="Maxtor" BLOCK_SIZE="512" UUID="38AA22BFAA227990" TYPE="ntfs" PARTLABEL="Basic data partition" PARTUUID="0213a180-2818-4a2d-9d32-bd10f896d426"
Since the partition I want to mount is formatted NTFS I’ll need to make sure the ntfs-3g driver is installed – it almost certainly will be already. If the partition is formatted as exFAT you’ll need the exFAT drivers which might be installed already. Note, installing exfat-fuse will pull in the other required packages.
sudo apt install ntfs-3g sudo apt install exfat-fuse
Mounting the Drive on the Raspberry Pi
First off we need somewhere to mount the drive so let’s create the directory “/data/general” as this is a general data drive. I tend to keep my permanent mounts under the /data directory. Some people mount things under /mnt but that is for temporary mounts. There’s also /media but that is for removable media. I think /data is a reasonable permanent option as it doesn’t exist on any Linux distribution that I can think of.
sudo mkdir -p /data/general sudo chown -R doozer:doozer /data/general
The -p flag on mkdir makes the command also create parent directories if they are required. The second command gives ownership of the directory to me.
Now it’s time to modify the fstab file. This is the configuration file for all mounts that the system knows about, we’ll be using Nano to edit it.
sudo nano /etc/fstab
The line you need to add to the fstab file needs to look something like the one shown below
UUID=38AA22BFAA227990 /data/general ntfs-3g defaults,auto,users,rw,nofail,noatime,uid=1000,gid=1000 0 0
You’ll need to modify the UUID, mount points and type to suit your particular drive. Note: you’ll probably see some PARTUUID references in the fstab file. Depending on the drive you may be able to use that rather than the UUID (see 1, 2). When you’re finished editing save the file with Ctrl+X followed by Y and enter.
To mount the drive you simply need to issue the following mount command
sudo mount -a
This command will cause the system to try and mount everything in the fstab that isn’t already mounted. If you get an error about the drive already being mounted you may need to unmount it first, this maybe the case if you are running the desktop version of Pi OS. You can be more selective with the mount if you have other things you don’t want mounted now. You should now be able to reboot your Pi and have the drive automatically mounted at boot time.