Raspberry Pi – Set a Static IP Address
- Expand the storage space of the Raspberry Pi.
- Add quicker and more stable storage.
- Auto mount the external storage.
By default the Raspberry Pi only uses a microSD card for it’s storage. While this is great in terms of power consumption and space used, microSD cards aren’t very fast and the data on them is easily corrupted.
SSH stands for secure shell. SSH is an encrypted connection established between two computer programs. On the server side (the computer being connected to) a service is running that listens for another computer trying to contact it via SSH.
Click here for a full detailed description of SSH.
SSD stands for Solid State Drive. Unlike traditional spindle hard drives an SSD uses integrated circuits to store data. They are quicker than spindle hard drives and require less power.
Click here for a full detailed description of SSD.
This guide assumes you have a fresh install of Raspbian on a headless server.
You have attached a powered external hard drive to the Raspberry Pi. A bus powered hard drive will most likely try to draw more current than the Raspberry Pi can deliver unless you are using an SSD.
Format Hard Drive
When you’ve selected your hard drive and plugged it in, the first thing we’ll want to do is format it to ensure there is no data left on it before continuing. To do this we need to know the name and location of the hard drive, we can do this using:
ls -l /dev/disk/by-uuid/
You should see something like the image below. This shows my two partitions of the microSD card as well as ../../sda1 which is the partition on my external hard drive. Note by default one external hard drive will be assigned to sda.
Now we know the name and unique ID of the drive we can start the format process.
To format the drive we’re going to be using fdisk:
sudo fdisk /dev/sda
We need to make sure there are no partitions so when we create a new one it will use all of the space on the hard drive.
Press “d” and hit return, if there is more than one partition it will ask which to delete. Keep selecting 1 and repeating the process until all are deleted. If there is just a single partition it will automatically delete it.
Now we have deleted all the old partitions it’s time to create a brand new one.
- Press “n” then return to create a new partition.
- Press “p” then return to create a new primary partition.
- Accept all of the defaults by pressing return.
Once you’ve created the partition we need to write these changes to the disk and then exit fdisk.
- Press “w” to write changes to the disk.
- Press “q” to quit out of fdisk.
Now we’ve got the new partition we need to format it as ext4:
sudo mkfs.ext4 /dev/sda1
This may give a warning if it used to contain an old ext4 file system. If you see this prompt press Y to continue and allow the drive to be formatted.
The external drive is now formatted and ready to be mounted.
Before we mount the drive we need to create a folder that it will be mounted to. For the purpose of this guild I’ll be creating the folder /mnt/USBDrive, but feel free to use a folder name and location that suits your purposes:
sudo mkdir /mnt/USBDrive
Once we’ve created the folder we need to assign it a user and group as it will have defaulted to root:root. I’ll be using pi:pi, but feel free to use a user and group that better suits you:
sudo chown -R pi:pi /mnt/USBDrive
We can check this has worked with the following command:
ls -al /mnt
Now we have a folder to mount the drive to we’re ready to setup an auto mount on boot. First things first we need to know the drives new UUID:
ls -l /dev/disk/by-uuid/
Copy the unique Id of your hard drive (sda1), shown in blue in the image below.
To auto mount a drive on boot we’re going to edit the fstab file that’s located at /etc/fstab:
sudo nano /etc/fstab
Add a new line at the bottom of the file, make sure to update the UUID and mount position to match your own:
UUID=e3395033-d124-4b11-8e6e-9ef34967be50 /mnt/USBDrive ext4 rw,relatime 0 2
Press Ctrl+x to exit and you’ll be prompted to Save modified. Type Y and then return to save the file.
We’re not going to mount the hard drive manually, we’re going to make sure the auto-mount is working. To do this, give the Raspberry Pi a reboot:
Once it’s rebooted we are going to check the free disk space of all mounted disks:
You should see your drive listed on the left side as /dev/sda1 along with it’s size, used space, available space and mount point.
Let’s check that the drive has been mounted with the correct permissions. If you have mounted it as user and group “pi” then you can go ahead and try to create a test folder. If you have mounted it as a different user we’ll need to spoof the user.
As user pi:
ls -al /mnt/USBDrive
If you are mounting the hard drive with a different user, let’s spoof the user. For this guide I’ll be using qbtuser:
sudo su qbtuser
Then we want to return to user pi so we’ll exit user qbtuser:
With everything working, lets clean up after ourselves:
rm -R /mnt/USBDrive/Test
Congratulations, you’ve added an auto mounting external hard drive to your Raspberry Pi!