Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Auto Attaching USB Storage To A Raspberry Pi Running Linux

TwitterFacebookRedditLinkedInHacker News

I’ve written a few articles about how I’m using my many Raspberry Pi units. Recently I wrote about using a Raspberry Pi as an automatic network backup server, but I didn’t talk about expanding the storage beyond the micro or standard sized SD card. What if you want to utilize a much larger USB hard drive or thumb drive? By default Linux won’t mount the drive, and heck, it may not even be in the correct format.

We’re going to see how to format a USB drive, mount it on a Raspberry Pi, and then auto mount it every time the Raspberry Pi reboots.

Before going any further we’re going to assume you already have a functional Raspberry Pi. If you need help installing Raspbian Linux and getting your unit up and running, you might check out my other tutorial on the subject.

As far as my setup goes, I’m using the original Raspberry Pi. Yes I know the third generation is available, but I’m putting my collection to good use. I plan to use a 64GB USB flash drive for extra storage.

Discovering the Connected USB Drive

Plug the USB device into the Raspberry Pi and power it on. SSH into the unit after it has finished booting so we can run a variety of commands.

First we need to figure out which disk we’re going to use. Execute the following to get a list of your Raspberry Pi disks:

sudo fdisk -l

Make note of the disk that represents your USB drive. It is helpful if you use a storage size different from your SD card as it will be easier to determine.

At this point we can format the drive if it is not already formatted with the FAT or Ext3 filesystem.

Formatting the USB Drive to use the Ext3 Filesystem

Formatting the drive is potentially optional. If your drive is already in FAT format, Linux can understand it. However, I want my drive to be formatted as Ext3 which is a Linux format.

Execute the following to perform operations on your disk, swapping out the actual disk with yours:

sudo fdisk /dev/sda

You can delete any existing partitions by using the d command. When you’re done, you can save the changes by using the w command. Now you can use the n command to create new partitions, but we’re going to use something else for formatting as Ext3.

To format as Ext3, execute the following command:

sudo mkfs.ext3 /dev/sda

Remember to swap out /dev/sda with that of your actual USB disk.

Mounting the USB Drive in Raspbian

We’re going to mount the USB drive to make sure it works. Before we can do this we need to define a mount location. Execute the following to create a new mount location:

sudo mkdir /usbdrive

Yes, we just created a directory, nothing really special. However, now we can use the Linux mount command to mount the drive. Execute the following to mount the USB drive:

sudo mount /dev/sda /usbdrive

Remember to swap out what I put as the disk with your actual USB disk. You can see if it mounted by execute the following:

df -h

It should show up in the list of disks. At this point the disk may be mounted, but when the Raspberry Pi reboots, the mount will not automatically happen. This may be fine for you as maybe you don’t want it to auto mount. This scenario is not alright with me.

Configuring the USB Drive to Automatically Mount on Reboot

To set the USB disk to automatically mount when the Raspberry Pi reboots we need to alter the Linux fstab file. Using your favorite editor (mine is VIM), open the /etc/fstab file. The file must be opened as a privileged user, otherwise we won’t be able to save it.

We need to add the following line:

/dev/sda        /usbdrive         ext3    defaults          0       0

We are setting the USB disk to be automatically mounted to the /usbdrive location without disk checking. Save the fstab file and reboot your Raspberry Pi. When it comes back on, your USB drive should still be mounted.

Conclusion

You just saw how to add USB storage to a Raspberry Pi machine. Now let’s be fair here. The steps mentioned above will work on any Linux machine, it isn’t specific to Raspberry Pi. I just gave a real scenario that I went through for my own personal network backup server. These Raspberry Pi microcomputers are fantastic devices and you can do a lot of cool things with them.

Nic Raboy

Nic Raboy

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in C#, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Unity. Nic writes about his development experiences related to making web and mobile development easier to understand.