How to Set up Raspberry Pi as a NAS Server

Access your files anywhere on your network.

Raspberry Pi NAS

One of the Raspberry Pi’s many functions is as a budget Network Attached Storage (NAS) device. Commercial NAS solutions are often costly and restrictive, but the Pi is inexpensive and lets you configure your server however you like and pair it with nearly any hard drive. When you create a Raspberry Pi NAS server, you'll have more than enough power to handle the everyday tasks of the home NAS, and the Raspberry Pi 4 even sports USB 3.0 ports for even faster file transfer.

What You’ll Need

Before you can get started, you’ll need a few things to put together your NAS.

  • Raspberry Pi 3B or 4
  • MicroSD card 16GB or greater
  • Case for your version of the Pi
  • Power supply compatible with the Pi
  • Ethernet cable
  • USB hard drive with its own power supply

Install Raspbian

Raspbian is the default operating system for the Raspberry Pi. It's the only one made directly by the Raspberry Pi foundation, and it's based on Debian Linux.

The utilities used in this guide to create your network attached storage are actually Linux file sharing services that come from Raspbian.

  1. Open your browser and download the latest version of Raspbian Lite from the Raspberry Pi foundation.

    Raspbian Download page
  2. Unpack the Raspbian ZIP file. It expands significantly, so be sure you have enough space first.

  3. Next, you’ll need a utility to flash the Raspbian image file onto your SD card.

    This guide is going to use balenaEtcher because it’s simple and works on Windows, Mac, and Linux, but if you have another option that you prefer, you can certainly use that instead.

  4. Go to the balenaEtcher download page, and download the latest release for your platform.

    Etcher download page
  5. Install or run Etcher. This will be different, depending on your operating system, but the installer is fairly straightforward. The defaults should be fine for nearly everyone.

    Etcher on Windows 10
  6. Open up balenaEtcher, if you haven’t already, and turn your attention to the first column to the far left. In that column, select the image file you extracted for Raspbian.

    Etcher with Raspbian image added
  7.  In the center column of Etcher, choose your microSD card to write to.

    Etcher with SD added
  8. Lastly, make sure that everything’s correct, then select Flash in the final column.  

  9. Before you remove your microSD, locate the new boot partition on the card using your operating system’s file manager.

  10. Mount boot if you need to, and place an empty file called "ssh" in the partition. The file should be completely empty, and ssh shouldn’t have any extension. This file will tell the Pi to enable SSH remote access when it starts up.

  11. Unmount or eject any partitions from your microSD, and remove it from your computer.

  12. It’s finally time to assemble the Pi. Put it in its case, and insert the microSD. Connect the Pi to your network via a wired connection. You should leave it wired for the best performance, but you can change this later.

  13. The final assembly step is to give the Pi power. This will start the Raspberry Pi and begin the setup process where it resizes the disk image to fill your microSD card.

    It’s not a good idea to stop this setup, so it should always be done after everything else is connected.

  14. After you give the Pi a few minutes to get started, open a terminal on your computer to connect over SSH. This can be the built-in OpenSSH feature on Windows 10, PuTTY, or a regular terminal window on Linux or Mac.  

  15. Find the Raspberry Pi’s IP address. The easiest way is to open the connected device list on your router. The Pi is called “raspberrypi” by default, and it’ll be listed under that hostname on the router.

  16. Enter the username for the Pi, which is "pi," and the IP address on your SSH client. It’ll look something like this:

    $ ssh pi@192.168.1.150
  17. When you connect, you’ll be asked for the pi’s password. The default is always “raspberry.”

How to Set up a Network File System

A Network File System (NFS) is a convenient way to share files between Unix-like systems, including Linux and Mac.

However, NFS is much more of a hassle on Windows. So, if your network is primarily made up of Windows PCs, you may want to skip this option. If you have Macs, Linux PCs, or other Raspberry Pis you plan to share files with, it might be worth the effort to set up an NFS.

  1.  Make sure you’re connected to your Pi over SSH.

  2.  Install the required NFS server package with the following command:

    $ sudo apt install nfs-kernel-server
  3.  Use the Nano text editor to open the NFS exports file on the Pi:

    $ sudo nano /etc/exports
  4. This file will contain a list of folders you want to make available over NFS. On the left side, you list the path to the folder. On the right, you specify which computers can connect and the options for the connection. Start by starting a new line, and entering the path to your file share:

    /path/to/share
  5. Press Tab to move across to the right side. Don’t worry if it’s not perfect. Now, enter the the IP address range of your home network. Most home networks are on the 192.168.1.X range. You tell the server to accept all IP addresses in the range by ending with zero followed by a backslash and the number twenty-four. The whole thing looks like this:

    /path/to/share 192.168.1.0/24
  6. Next, you need to add the options for your share. There are several to choose from, but if you want a good default, use the example below. It’ll let you read and write your files, syncing them with the server to prevent conflicts and corruption.

    /path/to/share 192.168.1.0/24(rw,sync,no_subtree_check)
  7. Add as many more shares as you like. When you’re done, save and exit Nano.

    Raspberry Pi NFS exports
  8. Restart the NFS server to apply your changes.

    $ sudo systemctl restart nfs-kernel-server
  9. When the server comes back up, you’ll have access to your NFS shares.

How to Set up Samba for Network Shares

Samba lets Linux machines create Windows network shares. Most computers are compatible with Samba and can mount its shares. Samba also has the advantage of letting you restrict access to certain users with a password.  

  1. Begin by installing Samba on the Raspberry Pi using the following command:

    $ sudo apt install samba
  2. With Samba installed, you’re going to need to edit its configuration to create your shares. Use the Pi’s Nano text editor to open the Samba configuration:

    $ sudo nano /etc/samba/smb.conf
  3. First, find the workgroup entry under Global Settings. If you’ve never changed your Windows workgroup, leave this as-is. If you have, set workgroup equal to your current workgroup’s name.

    workgroup = WORKGROUP
    Raspberry Pi Samba workgroup
  4. There are a ton more settings. To set up a basic server, though, you won’t need most of them. Scroll all the way down to the Share Definitions section.  

  5. Look for "[print$]" and set browseable to no. This isn’t necessary, but you aren’t using the Pi as a print server, so that share is only going to clutter things up.

    Raspberry Pi Samba printer config
  6. Keep scrolling all the way to the bottom. Here’s where you’re going to create your own shares.

  7. Each Samba share starts with a name in square brackets. That’s the name your share will appear as when you connect to it on another computer. Start by naming your share.

    [Your Share]
    Raspberry Pi Samba share name
  8. All the settings for your share will go directly underneath it, indented three spaces. Start with a comment about what your share is.

    Raspberry Pi Samba share comment
    [Your Share]
    comment = Just some networked files
  9. Now, add in the path to your directory.

    Raspberry Pi Samba path
    path = /path/to/share
  10. From here, there are a ton of options you can choose in setting up your share, but let's cover the basics. First, choose whether you want the share open to everyone or only certain users. Samba refers to users that aren’t signed in as guests, so set guest ok to either yes or no.

    guest ok = no
  11. Now, choose whether you want the people connected to the share to be able to write and create files or just read them.

    Raspberry Pi Samba share settings
    read only = no
  12. Finally, if you aren’t allowing guests, set which user(s) are valid to access the share.

    valid users = user1, user2
  13. Take a look at your share. Altogether, it should look something like this:

    Raspberry Pi Samba share configuration
    [Your Share]
    comment = Just some networked files
    path = /path/to/share
    guest ok = no
    read only = no
    valid users = user1, user2
  14. Start your next share the same way. You can create as many as you need. When you’re done, save and exit.

  15. Create user accounts for all of your users. They need to have a user account on the Pi, so set up minimal ones.

    $ sudo useradd -m -G users -s /bin/bash username
  16. Set a password for each one too.

    $ sudo passwd username
  17. Finally, set up a Samba password for each user too.

    $ sudo smbpasswd -a username
  18. Now, restart Samba, and you’ll be able to access your shares over the network.

$ sudo systemctl restart smbd