In this tutorial, we will learn how to set up a Raspberry Pi as a network attached storage device. You will need a Raspberry Pi (Model B or B+), a powered USB hard drive and a wired internet connection. The instructions below assume that you have already installed the Raspbian operating system on your device. If you are new to Raspberry Pi, you should start with theNOOBS install. You may want to access your Raspberry Pi remotely (therefore removing the need for a keyboard and display). If you choose to do so, the Raspberry Pi website has documentation on connecting to your Raspberry Pi via SSH.

We will be using the command line throughout this article. As such, it is recommended that you have some familiarity with how that command line works. If you are confused about a particular command, you can always use man to get more information. For example: man mkdir will return a description of the command and any options that exist for that command. When you are finished, type q to return to the command line.

Test Internet Connection

The first thing we need to do is check that our Raspberry Pi is connected to the internet. To do so, type ping into the command line. If you are connected to the internet, you should begin to see output similar to this:

64 bytes from icmp_seq=3 ttl=58 time=26.482 ms
64 bytes from icmp_seq=4 ttl=58 time=27.024 ms
64 bytes from icmp_seq=5 ttl=58 time=27.460 ms
64 bytes from icmp_seq=6 ttl=58 time=27.666 ms

Once you have determined that your connection is good, press Ctrl + C to cancel the request.

Update Raspbian

Now we need to update Raspbian. This will ensure that all our packages are up to date and reduce the risk of any security errors we might face. In general, this is a good practice to follow before starting any new project.

Run the command sudo apt-get update to check the database for available updates. Once that process has finished, run sudo apt-get upgrade to install any updates to your Raspberry Pi.

Mount USB Drives

Next we will mount our hard drives to the Raspberry Pi. Unlike a typical operating system, the Raspberry Pi does not automatically add available drives to the file system. We must manually mount each drive and define it’s location in the file system.

Run the command sudo fdisk -l to list the drives connected to the Raspberry Pi. The output should look something like this:

Raspberry Pi NAS Tutorial

The first disk in this list is the SD card, which is running our operating system. The second disk is our USB hard drive. You can further identify the correct disk by checking the capacity and disk format (2000.4 GB and HPFS/NTFS/exFAT in the example above). The part we need to remember is the /dev/sda1, which identifies the location of the drive. We will use this location to mount the disk to a directory within the file system.

I am going to mount the drive in the directory /media/USBHDD1. This directory does not exist by default, so we will need to create it. You can use a different path if you prefer, but be sure to substitute that path throughout the rest of this article.

Run sudo mkdir /media/USBHDD1 to create the USBHDD1 directory within the media directory.

Now we can mount the hard drive to the directory by running sudo mount -t auto /dev/sda1 /media/USBHDD1. If your hard drive already contains files, you can test this connection by running ‘ls /media/USBHDD1’. This command will populate a list of files at the root level of your hard drive.

Before we move on, we need to ensure that this disk will remount to the same location when we reboot the device. To do this we need to edit a system configuration file. Run sudo nano /etc/fstab to open the file editor. The contents of the file should look similar to the example below.

Raspberry Pi NAS Tutorial

Add the following line to the bottom of the file:
/dev/sda1 /media/USBHDD1 auto nonempty,uid=1000,gid=1000,umask=007 0 2

Exit the file editor by typing Ctrl + X followed by Y to save the edits that were made.

If you would like to test this change, run sudo reboot to restart the Raspberry Pi. Once the device has rebooted, run the ‘ls /media/USBHDD1’ command to list the files at the root level of your hard drive.

Configure Samba File Server

We will be installing a file server called Samba, so other machines on the local network can access the files on our Raspberry Pi.

Install the Samba package by running sudo apt-get install samba samba-common-bin. You will be prompted to type Y to continue. Samba will then take a few minutes to download and install.

Next we need to edit the Samba configuration file. Run sudo nano /etc/samba/smb.conf to open the file editor. Find the Authentication section and delete the # from the security = user line. Scroll to the bottom of the file and add the following lines of code:

comment= Public Storage
path = /media/USBHDD1
valid users = @users
force group = users
create mask = 0777
directory mask = 0777
read only = no
writeable = yes
browseable = yes
public = yes

Exit the file editor by typing Ctrl + X followed by Y to save the edits that were made.

At this point, we need to add the default user pi as Samba user. If you’ve created another user account, you can add that user to Samba by substituting that username for pi in the following command. Run sudo smbpasswd -a pi to create a new Samba user. You will be prompted twice to enter a password for the account.

The last thing we need to do is restart the Samba server for the changes to take effect. Run sudo service samba restart to restart.

Now the Raspberry Pi should be noticed by other computers on the local network. You may be prompted to enter the username and password before accessing the device.

Final Thoughts

This tutorial is outside of the scope of what I typically write about. However, I recently got a RaspberryPi and have had a difficult time finding clear instructions on this topic. I hope sharing my process will save others some time.

4 Questions To Ask When
Planning Your Website Redesign

A Goal-Oriented Approach To Web Strategy

Matt Litzinger headshot

Matt Litzinger

Matt is a New Hampshire-based web developer focused on UX and digital accessibility.