Family Encyclopedia >> Electronics

How to build a NAS server with Raspberry Pi

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

ContentsWhat You'll Need Step 1:Prepare your storage If you're using an external drive:If you're using your SD card:Step 2:Install Samba Step 3:Configure Samba Step 4:Create a Samba user Step 5:Access your NAS from Windows Final Notes

There are different ways to achieve this, but OpenMediaVault (OMV) and Samba are by far the two most popular. Here we are going to use Samba, mainly because you can run it on your current Raspbian installation, whereas OMV replaces the main operating system and prevents you from using the Pi for anything else. OMV has a lot more features though, including the ability to easily access files from external networks. If you're planning on using a Pi for anything other than 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 high-end hardware.
  • An SD card installed in the Pi and configured with raspberry.
  • A computer connected to the same network as the Pi.
  • [Optional] An external storage device, such as a hard drive, plugged into the Pi.

I'm using a Raspberry Pi 4 with 4GB of RAM because that's what I have, but gen 2 and 3 users have also reported success with Samba. 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 attached to the Pi will significantly increase your storage space. You can even use two external devices and set up RAID to make sure you don't lose data if one drive fails, but we'll keep it simple for now.

If your system isn't fully updated, now is a good time to do so! Courses

sudo apt update &&sudo apt -y upgrade

to update the list of packages and install updates.

Step 1:Prepare 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 will 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.

How to build a NAS server with Raspberry Pi

To do this, simply run:

mkdir /home/pi/FOLDERNAME

That's it! You can change the directory or folder name to any other name you want. I call mine "raspsharey".

Step 2:Install Samba

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

sudo apt install samba samba-common-bin
How to build a NAS server with Raspberry Pi

Step 3:Configure Samba

You can access the Samba configuration file by running:

sudo nano /etc/samba/smb.conf

Once you're there, scroll down and add a few more lines to tell Samba what you want to share. It should look something like this:

[sambadrive]path=/media/pi/pisharedrivewriteable=Yesread only=nocreate mask=0777directory mask=0777public=no

[sambadrive] is the address you will use to access the shared folder from another PC.

How to build a NAS server with Raspberry Pi

For the path, if you use a drive, it will look like the above. If you are using a folder on your SD card, this will be the directory you set using mkdir in step 1 above. For me it is:

/home/pi/raspsharey

If the public parameter is "no", you will need a username/password to access the folder. Setting it to "yes" will allow anyone on the network to enter with just the address.

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

With that, you are done with the configuration file! Hit Ctrl + O and Enter to save (just to be sure!), then Ctrl + X to close the file.

Step 4:Create a Samba user

Next, you will need to create a Samba username/password. Raspberry Pi's default user is just "pi", so it's easy to use. Use

sudo smbpasswd -a pi

to create a Samba user called "pi" with a password. You will receive a prompt to enter a password. Type it, hit enter and start again.

How to build a NAS server with Raspberry Pi

Then restart Samba to load the changes using:

sudo systemctl restart smbd

With that, you are done with the setup! Now all you have to do is access your shared folder from another machine.

Step 5:Access your NAS from Windows

1. Open File Explorer.

How to build a NAS server with Raspberry Pi

2. Find the button on the top bar labeled with a variation 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 in its own larger button under the "Computer" tab.

3. Clicking on it will take you to a configuration dialog. In the Folder box, you will need to enter the address of your Raspberry Pi and the address you entered in parentheses in the configuration file above. To me this looks like \raspberrypisambadrive .

4. The same goes for the backrest. Using your pi's local IP address instead of "raspberrypi" also works.

How to build a NAS server with Raspberry Pi

5. Make sure to check the box "Connect using different credentials".

6. Click Finish and enter the credentials you configured when you created the Samba user.

How to build a NAS server with Raspberry Pi

Open your folders and enjoy.

Concluding remarks

If you're having trouble accessing your folders, like needing write permission, it may be because your Pi user doesn't have ownership of them. To resolve this issue, type:

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

On mine it's:

sudo chown -R pi pi/raspsharey

You may come across advice online telling you to edit the "/etc/fstab" file to fix this permissions issue, but be aware that editing this file can potentially mess your Pi up badly if you get it wrong. This should probably be a last resort.

If all goes well, however, this is a project you should be able to complete in less than half an hour, after which you'll have your own NAS whenever your Pi is up and running.

Image credits:Raspberry 4 Model B

Read more:

  • How to turn your Raspberry Pi 4 into an Edge Gateway
  • How to install OSMC on a Raspberry Pi
  • How to build a DIY wireless printer with a Raspberry Pi