You are on page 1of 10

How to Turn a Raspberry Pi Into a NAS for Whole-

Home File Sharing


www.pcmag.com

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. But these devices can get expensive, so one way to save money is to
build one yourself for cheap with a Raspberry Pi at the core.

What's a NAS?

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 your movies and play them
from multiple Kodi boxes, store backups on it 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.

The Raspberry Pi, on the other hand, is such a versatile little board 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. You'll want to make sure that any important data 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 you're hooked, you can upgrade to a purpose-made Synology or QNAP
model that fits your long-term needs.

What You'll Need...

A Raspberry Pi with all the trimmings


Obviously, you'll need a Raspberry Pi for this project, along with the requisite accessories: a
power supply, a microSD card, and a mouse, a keyboard, and a monitor for the initial setup.
Any of the recent-model Pis should work for this project, and you can read more about the
other accessories in our guide to getting started with the Raspberry Pi.
A disc drive (or two, or three)
Unless you're sharing just a few files, your microSD card probably isn't enough storage for a
NAS. You'll 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, though you
may need one that plugs into the wall separately—or a powered USB hub—since the Pi may
not be able to supply enough power to all your drives. If you want a cleaner setup, you can
use an internal drive designed for network attached storage, too, but that would require a
case.

A NAS-friendly case (optional):


If you want your system to have a clean look, it may behoove you to get an enclosure for
your Pi and drives, so it isn't just an octopus of wires and disks. For example, Geekworm
makes a board called the X825 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. (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're willing 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.

Install an Operating System


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've booted up Raspbian for the first time, designated a new
password, and downloaded all your updates, connect your hard drive to one of the Pi's USB
ports.

You'll see it show up on the desktop, but we'll 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.)

Unmount Your Drive

Before continuing, we'll need to erase the drive you attached, so if you have important files
on it, you'll have 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'll 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're
using a terminal over SSH, you'll 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


This 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.

Partition Your Drive

Next, we'll 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'll use /dev/sdb1, and so on):

sudo mkfs.ext4 /dev/sda1

Press y and Enter when asked if you want to proceed. Then run:

sudo e2label /dev/sda1 MyExternalDrive

Just replace MyExternalDrive with whatever you want to name your drive. Formatting will
take a few minutes, especially if you have a large drive, so be patient. When you're 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:

sudo chown -R pi /media/pi/MyExternalDrive


Share the 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're going to use a tool called Samba, which is an
open-source implementation of Windows' SMB/CIFS file-sharing protocol. It's not your only
option for sharing files, but it's 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'll 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


Then, from the command-line text editor that appears, use your arrow key to scroll to the
bottom of the document. You'll 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 you're done, press Ctrl+X to exit nano, pressing y and Enter when asked if you want to
save the file.

Create a Password and Add Users


Finally, you'll 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 add other users with sudo adduser jeff, where jeff is the user you want to add, and
run sudo smbpasswd -a jeff to give that user their own password. 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 that's all done, run the following command to restart Samba:

sudo systemctl restart smbd


And everything should be ready to rock. Head to your Windows PC, open a File Explorer
window, and type \\raspberrypi\MyMedia in the address bar (replacing MyMedia with
whatever your share is called). If you press Enter, you should be able to enter your Samba
username (pi) and password and see your shared drive. If you run into trouble, you might
have to use the Pi's IP address, like \\192.168.1.10\MyMedia instead.

To connect on a Mac, open Finder and click Go > Connect to Server, typing 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, add more users with different
permissions, or set up a RAID array to avoid data loss in the event of a hard drive failure.
Once you get to that point, though, it will likely be worth spending a little more on a
dedicated NAS device for better performance.

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.

You might also like