How to Build a NAS Server with Raspberry Pi

Pi Nas Featured

A network-attached storage device (NAS) is basically just a drive that lets you access it over your local network. It’s essentially the home version of cloud storage and is great for storing files that won’t fit on your computer or sharing them between multiple devices. If you have a Raspberry Pi, you can easily and inexpensively build a NAS server with the Pi.

There are a few different ways to make this happen, but OpenMediaVault (OMV) and Samba are by far the two most popular. Here, we’re going to use Samba, mainly because you can run it on your current Raspbian install, whereas OMV replaces the main operating system and prevents you from using the Pi for anything else. OMV has many more features, though, most notably the ability to easily access files from external networks. If you plan on using a Pi for nothing but NAS, it’s worth a look.

What you’ll need

  • A Raspberry Pi, preferably a 4, 3, or 2. Samba doesn’t take much to run, so there’s no need for cutting-edge stuff.
  • An SD card installed in the Pi and set up with Raspbian.
  • A computer connected to the same network as the Pi.
  • [Optional] An external storage device, like a hard drive, plugged into the Pi.

I’m using a Raspberry Pi 4 with 4GB RAM because it’s what I have, but users with generations 2 and 3 have reported success with Samba as well. Depending on how many files you want your Pi to store, you might be able to get by with a high-capacity internal MicroSD card, but setting up an external drive connected to the Pi will boost your storage space quite a bit. You can even use two external devices and set up RAID to make sure you don’t lose data in the event of one drive failing, but we’ll keep it simple for now.

If your system isn’t fully updated, now’s a good time to do that! Run

sudo apt update && sudo apt -y upgrade

to update the package list and install the updates.

Also read: 5 Ways to Put Raspberry Pi 4 to Good Use

Step 1: Prep your storage

As with most Pi projects, you’ll want to open a Terminal, either on the Pi itself or via SSH.

If you’re using an external drive:

If you have a drive connected via USB, you’ll need to format it first. If you want, you can use GParted to do this in a GUI, but the instructions below are for the Terminal.

Pi Nas Format Drive 1

Run:

lsblk

or:

sudo fdisk -l

and identify the drive you want to use as your shared storage devices. If you only have one plugged in, it’ll probably be called “sda” or “sda1,” which you’ll want to remember. I know, mine’s tiny – it’s just what I had lying around and didn’t mind formatting for this demo.

Unmount the drive using:

sudo umount /dev/NAME

where NAME is “sda” or “sda1,” or whatever else you saw in the output from lsblk or sudo fdisk -l.

To format the drive, enter:

sudo mkfs -t ext4 /dev/NAME

This will wipe and format the drive using the ext4 format. You can use something like NTFS or FAT32 if you want, but ext4 is faster and causes the fewest problems.

Formatting will take some time. Once it’s done, rename the drive to whatever custom path you want to give it using:

sudo e2label /dev/NAME YOURLABEL

where NAME is probably sda/sda, and YOURLABEL is your custom label for the drive. After that, it’s a good idea to reboot, so go ahead and enter:

sudo reboot

If you’re using your SD card:

If you don’t need external storage, you can just create a shared folder on your Pi’s SD card.

Pi Nas Mkdir

To do this, just run:

mkdir /home/pi/FOLDERNAME

That’s it! You can change the directory or folder name to anything else you like. I call mine “raspsharey.”

Step 2: Install Samba

Samba is the tool that lets Windows and Unix machines share files. Run the following command to install Samba.

sudo apt install samba samba-common-bin
Pi Nas Install Samba

Step 3: Configure Samba

You can access the Samba configuration file by running:

sudo nano /etc/samba/smb.conf

Once you’re in, scroll down to the bottom and add a few extra lines to tell Samba what you want to share. It should look something like this:

[sambadrive]
path = /media/pi/pisharedrive
writeable=Yes
read only=no
create mask=0777
directory mask=0777
public=no
[sambadrive] is the address you’ll use to access the shared folder from another PC.
Pi Nas Samba Conf

For the path, if you’re using a drive, it’ll look like the above. If you’re using a folder on your SD card, it’ll be whatever directory you set using mkdir in Step 1 above. For me, that’s:

/home/pi/raspsharey

If the public setting is “no,” you’ll need a username/password to access the folder. Setting it to “yes” will allow anyone on the network to get in with just the address.

I added both the external drive and internal folder to my config file – you can share multiple folders/drives on Samba simultaneously if you want.

With that, you’re done with the config file! Hit Ctrl + O and Enter to save (just to be safe!), then Ctrl + X to close the file.

Step 4: Create a Samba user

Next, you’ll need to make a Samba username/password. The default Raspberry Pi user is just called “pi,” so that’s an easy one to go with. Use

sudo smbpasswd -a pi

to create a Samba user called “pi” with a password. You’ll get a prompt to enter a password. Type it in, hit Enter, and do it again.

Pi Nas Samba User

Next, restart Samba to load the changes using:

sudo systemctl restart smbd

With that, you’re all done with the setup! Now all you have to do is access your shared folder from a different machine.

Step 5: Access your NAS from Windows

1. Open File Explorer.

Pi Nas Map As Drive

2. Find the button on the top bar labeled with some variant of “Map drive.” Mine is in the New group behind a button labeled “Easy Access” and is called “Map as Drive” if I’m anywhere except the main “This PC” folder, where it’s called “Map Network Drive” and is sitting in its own larger button under the “Computer” tab.

3. Clicking this will bring you to a setup dialog. In the Folder box, you’ll want to enter your Raspberry Pi’s address and the address you entered in the brackets in the config file above. For me, that looks like \\raspberrypi\sambadrive.

4. It’s the same thing for the folder. Using your pi’s local IP instead of “raspberrypi” also works.

Pi Nas Connect Sambadrive 1

5. Make sure you check the “Connect using different credentials” box.

6. Click Finish and enter the credentials you set up when you made the Samba user.

Pi Nas File Test

Open your folders and enjoy.

Final notes

If you run into any issues accessing your folders, such as needing permission to write, it may be because your Pi user doesn’t have ownership. To fix this, enter:

sudo chown -R [PI USER] [PATH TO FOLDER]

On mine, that’s:

sudo chown -R pi pi/raspsharey

You may run into some online advice telling you to edit the “/etc/fstab” file to fix this permissions problem, but be advised that changing this file has the potential to deeply mess up your Pi if you get it wrong. That should probably be a last resort.

If everything goes well, though, this is a project you should be able to complete in under half an hour, after which you’ll have your very own NAS whenever your Pi is up and running.

Image credits: Raspberry 4 Model B

Read Next:

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Andrew Braun

Andrew Braun is a lifelong tech enthusiast with a wide range of interests, including travel, economics, math, data analysis, fitness, and more. He is an advocate of cryptocurrencies and other decentralized technologies, and hopes to see new generations of innovation continue to outdo each other.