I enjoy working with computers, and I find the Raspberry Pi to be a fascinating little device to work on.
Using a Raspberry Pi
The SD card that boots and runs your Raspberry Pi is likely very small, offering only a few GBs worth of space. This can be filled very quickly. The first upgrade to a Raspberry Pi is usually adding a USB hard drive. This guide will go through permanently mounting a USB NTFS formatted hard drive to your Raspberry Pi.
This guide assumes you already have a Raspberry Pi up and running with Rasbian as the operating system. If you're not up to this point yet, you will have to read up on how to set up a Raspberry Pi.
Your New Best Friend: A Hard Disk Drive
The ingredients here are simple. You will need:
- A Raspberry Pi. Ours has Rasbian installed as the operating system.
- A way to communicate with the Raspberry Pi, such as directly connecting keyboard, mouse, and monitor or going "headless" and connecting with another computer.
- A USB Hard Drive
For the hard drive, we chose a Seagate 4TB External, available for $139.99 at Amazon.com.
This setup has some pros and cons.
- 4TB is rather large.
- The hard drive uses an external power source. Therefore, it doesn't drain power from the Raspberry Pi's USB port.
- It's pricey at $139.99.
- Because it uses an external power source, it requires another cable and another outlet.
Another option is the WD 1TB Portable Drive, also available from Amazon.com. This comes at a nicer price point, only $69.99. The drive is large enough (1TB) to meet all immediate needs and doesn't require an external power source.
If you have experience with Linux and BASH, here is the code you will need to use. If this looks confusing or intimidating, keep reading! We will break down each and every line of code and command used. By the end of the tutorial, you'll be able to come back up to this point, read the code, and congratulate yourself for knowing exactly what each command is doing.
Let's get started!
Step 1: Connecting and Finding the Drive
First thing's first: boot up your Raspberry Pi and log in. Plug in the USB hard drive into a USB port on the Raspberry Pi and plug in the power source for the drive if it requires one.
Now let's see if the Raspberry Pi "knows" about the new hard drive you've connected to it. Open the terminal window. If you're in a Desktop style screen, click on the menu at the bottom left of the screen, navigate to "Accessories" and then click on "LXTerminal."
- "sudo"— This essentially means "Run as Administrator." We'll be using this a lot.
- "fdisk" — This is short for "Fixed Disk" and is a command line utility built into the operating system that allows you to manage partitions on a hard drive. Right now we're using it to look at what partitions on what hard drives your Raspberry Pi knows about by using "-I."
- "-l" — This is a "list" command that is passed to the fdisk utility. It tells fdisk to list what it knows.
Looking through the list outputted we see, from top to bottom.
- "Disk /dev/mmcblk0 4110MB"
This is the SD card which the Raspberry Pi boots off of. It has two partitions:
- "/dev/mmcblk0p1" and "/dev/mmcblk0p2"
Neither of these are what we're interested in. The drive that we're interested in is the 4TB Seagate External.
That drive is listed as:
- "Disk /dev/sda 4000.0 GB"
And has one partition:
Remember that. "/dev/sda1" That is the name that the drive is going by and the name we'll use to mount it.
Step 2: Loading the Correct Drivers
We found the drive and the Raspberry Pi knows about it, so we're done right? Not quite. The Raspberry Pi knows about the drive, but it isn't accessible yet. You wouldn't be able to find the drive anywhere in the filesystem. To solve this issue, we need to mount the drive.
First, we need to download the software that will handle talking to the drive. Drives come in different file system formats. The drive you have, if it's newly bought, has ever been used or will ever be used on a Windows PC, is most likely formatted as NTFS. NTFS is great for Windows but isn't native in Linux, which means Linux doesn't like to communicate with NTFS drives. This is easy enough to fix. Make sure your Raspberry Pi is connected to the internet, open the terminal window and type:
- "apt-get" — "apt" is short for "Advanced Packaging Tool" and "apt-get" is another command line utility. It allows you to grab software packages online and install them onto the local computer.
- "install" — This is the keyword we pass to "apt-get" so that it knows that we want it to install something.
- "ntfs-3g" — This is the something we want installed. "ntfs-3g" is a read-write driver for NTFS, which means it enables communication between Linux and NTFS formatted drives.
Step 3: Creating a Mount Point
Now we need a place in the filesystem to mount the drive to, so let's create that.
The next two lines of code will create the file location and then change the user that "owns" that location.
For the first line of code:
- "mkdir" — This is short for "Make Directory." It creates a folder in the file system.
- "/media/pidrive" — This is the folder you want to create. The Linux convention is to mount USB connected devices in the "Media" folder. The folder I want to use to access the USB drive I'm calling "pidrive." You can call yours whatever you want.
For the second line of code:
- "chown" — This is short for "Change Owner."
- "pi:pi" — This is choosing the owner:group for the folder. If you're using the Raspberry Pi, the default user is "pi." The default user group is also "pi." Thus, user:group becomes pi:pi.
- "/media/pidrive" — This is the folder we want to change ownership of.
We change ownership of the folder so that you don't need "sudo" or root permission to use the hard drive.
Step 4: Getting the Drive Mounted Temporarily
Okay, let's get mounting!
Remember the hard drive location? In our example, it was "/dev/sda1." In the terminal type the following code:
- "mount" — Mount is an operating system action that will mount the drive to the filesystem.
- "-t ntsf-3g" — "-t" is the filesystem type flag and "ntfs-3g" is telling the mount command what filesystem to use.
- "-o uid=po,gid=pi" — This is setting the owner of the drive. "uid" is user ID and "gid" is group ID. This is exactly like the ownership settings we put on the mount point folder.
- "/dev/sda1" — This is the reference to the hard drive itself
- "/media/pidrive" — Is the reference to the mount point folder.
Congratulations! Now your hard drive is mounted to the filesystem, accessible, and ready to use! However, this approach has one downside. Every time you reboot the Raspberry Pi, you'll have to execute this line of code again. Now we're lazy and don't want to do that, so let's set up this mount point permanently!
Step 5: Getting the Drive Mounted Permanently!
To get this drive to mount itself every time the Raspberry Pi is rebooted, we have to edit a configuration file on the Raspberry Pi called fstab short for "File System Table." Now, this sounds a lot more complex than it really is. Fstab is just a text file that tells the Raspberry Pi what to do with the hard drives connected to it. To edit it, we're going to use a text editor that comes with the Rasbian Operating System and works in the terminal called Nano.
In the terminal type in this code:
The terminal window will look different. We are now in the text editor Nano and are ready to edit this file.
Press the down arrow to navigate to one line below the line that says "/dev/mmcblk0ps" and press enter to insert a new line.
Arrow up one to place your cursor on the new line and type in:
This code should look familiar since it's very similar to the mount command we used to manually mount the drive.
Now press Ctrl+O (not Ctrl+Shift+O) to save the file. Press Enter to acknowledge the file name and then press Ctrl+X to exit Nano.
Now the terminal should return to the way it originally looked.
Congratulations! You're done! Your USB hard drive is now permanently mounted to the Raspberry Pi and will be remounted on every reboot. Now you're ready to use the USB drive to store your files, music, documents, videos, solutions to all the world's problems, and whatever else you want on your Raspberry Pi!
PiDrive Ready to Roll
Drop Me a Line
Please let me know if this guide has been helpful to you. Take the poll below and leave a comment! Thank you for listening.
Give me feedback!
This article is accurate and true to the best of the author’s knowledge. Content is for informational or entertainment purposes only and does not substitute for personal counsel or professional advice in business, financial, legal, or technical matters.
© 2014 John Himics
Mahesh on June 08, 2020:
Thanks for the very useful post.
Dr James Langtry on December 10, 2018:
Learned a lot from this...
Winston on December 22, 2017:
This was a great help! Thank you so much:D
Ho on April 06, 2016:
I have a problem, "sudo chown pi:pi /media/pidrive" doesn't work he says pi:pi invalid user. can you help me?