How to Set up Raspberry Pi as a NAS Server

Access your files anywhere on your network.

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. The Pi is inexpensive and lets you configure a server however you like and pair it with most hard drives. When you create a Raspberry Pi NAS server, you'll have more than enough power to handle the everyday tasks of the home NAS. In addition, the Raspberry Pi 4 has USB 3.0 ports for faster file transfer.

What You'll Need

Before you get started, you'll need a few things to put together your NAS:

  • A Raspberry Pi 3B or 4.
  • A MicroSD card, 16 GB or greater.
  • A case for your version of the Pi.
  • A power supply that's compatible with the Pi.
  • An Ethernet cable.
  • A 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 by the Raspberry Pi foundation and is based on Debian Linux.

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

  1. Open a 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.

  3. Choose a utility to flash the Raspbian image file onto the SD card.

    This guide uses balenaEtcher because it's simple and works on Windows, Mac, and Linux. If you have another option that you prefer, use that instead.

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

    Etcher download page
  5. Install. This will be different depending on the operating system, but the installer is fairly straightforward. The defaults should be fine for most systems.

  6. Open balenaEtcher. In the first column to the far left, select the image file you extracted for Raspbian.

    Etcher on Windows 10
  7.  In the center column of Etcher, choose the microSD card to write to.

    Etcher with Raspbian image added
  8. Ensure that everything is correct, then select Flash.

    Etcher with SD added
  9. Before you remove the microSD, locate the new boot partition on the card using the 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 empty, and ssh shouldn't have any extension. This file tells the Pi to enable SSH remote access when it starts up.

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

  12. To assemble the Pi, put it in its case, then insert the microSD. Connect the Pi to your network using a wired connection. Leave it wired for the best performance, but you can change this later.

  13. Power on the Pi. This starts the Raspberry Pi and begins the setup process. During this process, it resizes the disk image to fill the microSD card.

    Don't stop this setup. It should always be done after everything else is connected.

  14. Give the Pi a few minutes to get started, then open a terminal on the 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 is listed under that hostname on the router.

  16. Enter the username for the Pi, which is pi, and the IP address on the SSH client. It looks something like this:

    $ ssh pi@192.168.1.150
  17. When you connect, enter the Pi's password. The default is 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 more difficult on Windows. So, if your network is primarily made 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.  Connect to the 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 contains a list of folders you want to make available over NFS. On the left side, list the path to the folder. On the right, specify which computers can connect and the options for the connection. Start a new line, and enter the path to the file share:

    /path/to/share
  5. Press Tab to move to the right side. Then, enter the IP address range of your home network. Most home networks are on the 192.168.1.X range. To tell the server to accept all IP addresses in the range, end the address with a zero followed by a backslash and the number 24. The command looks like this:

    /path/to/share 192.168.1.0/24
  6. Add the options for the share. There are several to choose from. If you want a good default, use the example below to read and write files and sync files 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 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 have access to the 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 allowing you to restrict access to certain users with a password.  

  1. Install Samba on the Raspberry Pi using the following command:

    $ sudo apt install samba
  2. With Samba installed, edit its configuration to create the shares. Use the Pi's Nano text editor to open the Samba configuration:

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

    workgroup = WORKGROUP
    Raspberry Pi Samba workgroup
  4. There are many more settings. To set up a basic server, though, you don't need most of these. Scroll down to the Share Definitions section.

  5. Go to the [print$] section, then set browseable to no. This isn't necessary, but you aren't using the Pi as a print server, so that share only clutters things up.

    Raspberry Pi Samba printer config
  6. Scroll to the bottom, where you'll create your shares.

  7. Each Samba share starts with a name in square brackets. That's the name your share appears as when you connect to it on another computer. Name the share.

    [Your Share]
    Raspberry Pi Samba share name
  8. The settings for the share go underneath it, indented three spaces. Add a comment about what the share is.

    [Your Share]
    comment = Just some networked files
    Raspberry Pi Samba share comment
  9. Add the path to the directory.

    path = /path/to/share
    Raspberry Pi Samba path
  10. Choose whether you want the share open to everyone or only certain users. Samba refers to users that aren't signed in as guests. Set guest ok to either yes or no.

    guest ok = no
  11. Choose whether you want the people connected to the share to be able to write and create files or only read files.

    read only = no
    Raspberry Pi Samba share settings
  12. If you don't allow guests, set which users are valid to access the share.

    valid users = user1, user2
  13. Your share should look something like this:

    [Your Share]
    comment = Just some networked files
    path = /path/to/share
    guest ok = no
    read only = no
    valid users = user1, user2
    Raspberry Pi Samba share configuration
  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 your users. They need 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 user.

    $ sudo passwd username
  17. Set up a Samba password for each user.

    $ sudo smbpasswd -a username
  18. Restart Samba to access your shares over the network.

    $ sudo systemctl restart smbd