How to Turn a Raspberry Pi Into a NAS for Whole-Home File Sharing

My house is incredibly neat and organized, but when it comes to my digital life, the word “hoarder” comes to mind. If that sounds like you, a network attached storage device—or NAS for short—is the perfect investment to make your files wirelessly available on any device in your home.

Unfortunately, these devices can get expensive. Instead of skipping out on improved file storage, one way to save money is to build one yourself for cheap! This might sound complicated, but it’s easy to do if you have an inexpensive Raspberry Pi board at the core. Here’s how to build your own NAS device.


What is Netwrok Attached Storage?

Asustor Lockerstor 2 Gen2 (AS6702T) NAS


Asustor Lockerstor 2 Gen2 (AS6702T)
(Credit: Asustor)

Network attached storage allows you to share files from one, always-on device throughout your house. With a NAS on your network, you can use it to store movies for your Kodi box, save backups from your PC, or use it as a BitTorrent box that seeds your files 24/7.

Sure, you could do all this with your main PC, but a NAS is lower-power, and it is designed to run day and night, even if your desktop is out of commission. Once you start using one, it’s hard to go back.

There are plenty of ready-built NAS devices out there, from companies such as Synology, QNAP, and Asustor. Just buy one, pop in a disc drive, and you’re off to the races. But they can get expensive, and if you aren’t sure whether a NAS is for you, it’s hard to justify the investment—especially if you want something that can grow with your storage needs.


What is Raspberry Pi?

raspberry pi 4


Raspberry Pi 4
(Credit: Zlata Ivleva)

Then there’s the Raspberry Pi, a low-cost, Linux-based computer board that can be used for various purposes. We have used it to build a retro gaming console, a VPN server, and voice assistant, among other projects. Its price and versatility mean that it can act as a cheap trial NAS that—once you grow out of it—can be repurposed for something else.

It isn’t as rock-solid as, say, a Synology NAS unit, and RAID doesn’t work particularly well on the Pi if you want data redundancy. So if you do use as Raspberry Pi for data storage, you want to make sure that any important information on your Pi-based NAS is also backed up elsewhere.

However, it’s a great project if you have a Pi lying around and want to see what NAS life is all about. Then, once hooked, you can upgrade to a purpose-made Synology or QNAP model that fits your long-term needs.


What You Need to Buy

raspberry pi

Before You can get started, there are a few things you will need to build your own NAS device. Note that the Raspberry Pi Foundation’s last major board release was the Raspberry Pi 4 in 2019. While many of its approved retailers(Opens in a new window) are sold out, you can still find it on Amazon(Opens in a new window). Here’s what you need:

A Raspberry Pi (with all the trimmings)

Obviously, you need a Raspberry Pi for this project, but you also need a power supply, microSD card, a mouse and keyboard, and a monitor for the initial setup. Any of the recent-model Pis should work for this project, and you can buy kits with (almost) everything included.

For more information about the other accessories you need for this project, you can check out our guide to getting started with the Raspberry Pi.

A disc drive (or two, or three)

Unless you are sharing just a few files, your microSD card probably isn’t enough storage for a NAS. You will need some drives to fill up with your movies, music, or other files you want to share among devices. A standard external drive will do the trick in most cases.

However, since the Pi may not be able to supply enough power to all your drives, you may need one that plugs into the wall separately or a powered USB hub(Opens in a new window). If you want a cleaner setup, you can use an internal drive(Opens in a new window) designed for network attached storage, but that would require a big enough case.

A NAS-friendly case (optional)

If you want your system to have a clean look, it may behoove you to get an enclosure that not just fits your Pi, but your drives as well. This will also help to organize the mess of wires and cables this project will create.

For example, Geekworm makes a board called the X825(Opens in a new window) that allows you to dock a 2.5-inch internal hard drive, connect it to your Pi, and mount it all in a trim little case(Opens in a new window). (Just make sure you get one that’s compatible with your Pi—there are different cases for the Pi 3, the Pi 4, and so on.)

For now, I’m just using a standard Raspberry Pi case with a USB external drive Velcroed to the top, but if you want to get creative, the world is your oyster here. Once you have all your components in hand, it’s time to get your NAS up and running.


1. Install an Operating System

raspbian

There are special operating systems like Openmediavault that turn your Pi into a NAS, but for a beginner setup, I actually recommend regular old Raspbian—it’s flexible, easy to use, and good enough for sharing a few files over the network. Start by installing Raspbian with the recommended software as described in our beginner’s guide.

I recommend hooking up your Pi to your network via Ethernet for fast file transfer, but Wi-Fi will do in a pinch. Once you have booted up Raspbian for the first time, designated a new password, and downloaded all your updates, connect your drive to one of the Pi’s USB ports.

The drive will show up on the desktop, but we will be doing most of our work in the Terminal. (If you prefer, you can SSH into your Pi and perform these commands from another PC.)


2. Unmount Your Drive

unmount

Before continuing, we’ll need to erase the drive you attached, so if you have important files on it, you need to store them somewhere else before transferring them to your Pi-NAS. From a Terminal window, run the following command to see the disks connected to your Pi:

sudo fdisk -l

Find the external drive you want to use for your files—in my case, it’s an 80GB drive called “MyExternalDrive”—and note its path. In the screenshot above, the 80GB drive plugged into my Pi corresponds to /dev/sda. (Make absolutely sure you note the correct drive, as we’re about to erase it!)

First, you need to unmount the drive. If you’re using the Raspbian interface, you can just click the eject button next to the drive to unmount it. But if you are using a terminal over SSH, you have to run:

umount /dev/sda1

Depending on the drive, you may need to also run umount /dev/sda2, umount /dev/sda3, and so on, depending on how many partitions are on the drive from previous usage. Then, to erase and format your flash drive for Linux usage, run:

sudo parted /dev/sda


3. Partition Your Drive

raspberry pi partition

When you run that code, it will open up a wizard called Parted, which will allow you to create a new partition on the drive. Run this command, pressing Enter after each answer in the wizard and replacing MyExternalDrive with the name you want to use for the drive:

mklabel gpt

If prompted to erase the drive, type Y and press Enter. Then run:

mkpartMyExternalDriveext40%100%quit

The final Quit command will exit the Parted wizard. Obviously, you can adjust these commands to fit the name of your drive, the number and size of partitions you want to make on it, and so on—but for most basic users just starting out, these commands should work well.


4. Format the Partition

raspberry pi partition

Next, we need to format that partition. If your drive is located at /dev/sda, the new partition will be located at /dev/sda1 (if the drive is /dev/sdb, you will use /dev/sdb1, and so on). Run this code:

sudo mkfs.ext4 /dev/sda1

Press Y and Enter when asked if you want to proceed. Then run and replace MyExternalDrive with whatever you want to name your drive:

sudo e2label /dev/sda1 MyExternalDrive

Formatting will take a few minutes, especially if you have a large drive, so be patient. When it is finished, run this command to reboot your Pi:

sudo shutdown -r now

When your Pi boots back up, you should find that the external drive appears automatically on the desktop, ready for action. You will, however, have to run one final command to give yourself permission to write new files to the drive. In a Terminal, run:

Recommended by Our Editors

sudo chown -R pi /media/pi/MyExternalDrive


5. Share the Drive

share drive

Now it’s time to share that drive on your network, so you can add your files and access them from any device in the house. To do this, we need a tool called Samba(Opens in a new window), which is an open-source implementation of Windows’ SMB/CIFS file-sharing protocol.

It’s not your only option for sharing files, but Samba is easy to set up and compatible with just about any system you might have on the network, so it’s what I recommend. Raspbian doesn’t come with Samba installed by default, so you need to make sure your repositories are up to date and install it with the following commands:

sudo apt updatesudo apt upgradesudo apt install samba samba-common

The installer will ask if you want to modify smb.conf to use WINS settings from DHCP. Choose Yes and press Enter. Now you edit that configuration file yourself, to share your drive. Run:

sudo nano /etc/samba/smb.conf
samba config

Then, from the command-line text editor that appears, use your arrow key to scroll to the bottom of the document. You want to add a block of text that looks something like this:

[MyMedia]path = /media/pi/MyExternalDrive/writeable = yescreate mask = 0775directory mask = 0775public=no

In your version of this, MyMedia would be the name of your share (name it whatever you want) and /media/pi/MyExternalDrive would be the mounted location of your drive. (You may need to open up the file manager and head to /media/pi/ to figure out what it’s called.)

When finished, press Ctrl + X to exit nano, pressing Y and Enter when asked if you want to save the file.


6. Create a Password and Add Users

add users

Finally, you will need to create a password for Samba so you can see your share from other machines. (There are ways to configure Samba without requiring a password, but this generally isn’t good security practice, so I recommend adding a password.) To add a password to the existing Pi user, run:

sudo smbpasswd -a pi

Enter your desired password when prompted—it doesn’t have to be the same as your user password on the Pi itself, but it can be—and press Enter. You can then add other users with the following code (where jeff is the user you want to add):

sudo adduser jeff

You can also choose to give that user their own password by next running:

sudo smbpasswd -a jeff

This isn’t strictly necessary, but it can be useful if you have multiple people in your household to whom you want to give different read and write permissions on certain shares. Once all users are added, run the following command to restart Samba:

sudo systemctl restart smbd

From here, the setup process is finished. You can now access your media from the network.


On your Windows PC, open File Explorer and type \raspberrypiMyMedia into the address bar (replacing MyMedia with whatever your share is called) and press Enter. You should then be able to enter your Samba username (pi) and password to see your shared drive. If you run into trouble, you might have to use the Pi’s IP address, like \192.168.1.10MyMedia instead.

If you want to connect to your media from a Mac, open Finder and click Go > Connect to Server, then type in smb://raspberrypi when prompted.

This just scratches the surface of what you can do with a Pi-based NAS. As your storage needs evolve, you can add more drives and shares, create more users with different permissions, and even set up a RAID array to avoid data loss in the event of a hard drive failure. Though by that point, it might be worth spending a little more on a dedicated NAS device for better performance.

Tips & Tricks newsletter for expert advice to get the most out of your technology.”,”first_published_at”:”2021-09-30T21:23:24.000000Z”,”published_at”:”2022-08-31T18:37:00.000000Z”,”last_published_at”:”2022-08-31T18:36:55.000000Z”,”created_at”:null,”updated_at”:”2022-08-31T18:37:00.000000Z”})” x-show=”showEmailSignUp()” class=”rounded bg-gray-lightest text-center md:px-32 md:py-8 p-4 mt-8 container-xs” readability=”30.860215053763″>

Like What You’re Reading?

Sign up for Tips & Tricks newsletter for expert advice to get the most out of your technology.

This newsletter may contain advertising, deals, or affiliate links. Subscribing to a newsletter indicates your consent to our Terms of Use and Privacy Policy. You may unsubscribe from the newsletters at any time.

Facebook Comments Box

Hits: 0