How to Set Up a Simple DIY NAS Build

How to Set Up a Simple DIY NAS Build

DIY NAS Server at Home

Sharing files at home is easier with a shared network drive. A DIY NAS build can turn an existing server into a multimedia server with automatic backups—accessible from any computer or device on the network.

Why Build Your own NAS?

There are many approaches to configuring a NAS.

NAS stands for “Network Attached Storage Adapter”

It’s a generic term for any device that can provide storage on a network.

In many cases, people end up thinking in terms of “NAS vs. server.” In fact, a decent server machine can act as a DIY NAS. It’s even possible to build a Raspberry Pi NAS with the approach described here. Usually, the only reason to invest in dedicated NAS hardware is when extreme performance and redundancy become concerns. For most home uses, this will not be the case.

In our home, a Linux (Ubuntu) server acts as a NAS. We use it for:

  • Multimedia Server: perhaps the most popular use of a NAS is to store multimedia (video, audio, and picture) files. This is especially useful in conjunction with software like Kodi (a media player application that can run on many many different devices).
  • CCTV (security footage): In the cabin, we have an elaborate DIY IOT setup which uses a combination of motion detection and computer vision to detect people, cars, pets, etc. Suspicious events are automatically saved to the NAS for review.
  • Kubernetes: I use the NFS client provisioner storage add-on for Kubernetes. This lets me use the NAS as a single drive which contains all of the configuration files for every single application I run on the network.

This DIY NAS can be accessed from Mac OS, Linux, and Windows machines which are on the network. It has built-in backups and is performant enough to support all of the above common uses.

How to Set Up a NAS Build (DIY)

You’ll need a Linux computer for the DIY NAS, preferably connected to the network via an ethernet cable (as opposed to WiFi) for greater reliability.

It is generally advisable to use a dedicated hard drive for a NAS—a separate physical disk from that used for the operating system. A separate partition on the same drive can also work, but the advantage to a dedicated disk is that you can tinker with it (and swap it out) without affecting the operating system (and thus the computer’s ability to boot).

That said, if you only have one large, fast hard drive available—as I did—using it for both the operating system and the NAS storage may have positive performance implications (as compared to using a slower USB drive). To compare the performance of different hard drives, run sudo lsblk to find the device (e.g., /dev/sda) and then sudo hdparm -Tt /dev/sda to test the drive.

If you don’t yet have a disk mounted, you’ll need it to be located some known location (let’s call it /mnt/nas). It also should be owned by your user (usually, user #1000). To automatically mount a partition (on Debian-based systems, like Ubuntu an Raspbian) first run sudo blkid and find the UUID of the partition, and then add the appropriate line to /etc/fstab; something like:

UUID=XXX-AA-BB-CCCC /mnt/nas        ext4    defaults      0       0

Before doing anything else, verify that the mounted partition is working correctly by running sudo findmnt --verify --verbose. Then try actually mounting the drive with sudo mount -a (at which point you should see the contents of the partition at /mnt/nas). If either of these reports errors, do not reboot the machine until the errors have been corrected. Failure to do so could result in the primary partition not mounting and the machine failing to boot.

Now that you have some place to store the data, there are multiple protocols for accessing files over a network. NFS is sufficient for Mac OS and Linux (as well as most consumer electronic devices; for Windows support, see the next section). To begin with NFS, install nfs-utils (e.g., sudo apt-get install nfs-utils). Then edit /etc/exports to include the folder to be shared:

/mnt/nas *(rw,sync,no_subtree_check)

Finally, restart the NFS server with sudo systemctl restart nfs-server.

At this point, NFS clients should be able to connect to the server. If you don’t already have a client handy, you can test it on the server by mounting it to a different directory:

sudo mkdir -p /mnt/test
sudo mount localhost:/mnt/nas /mnt/test
echo "hello" > /mnt/test/world
cat /mnt/nas/world

If all went well, you should see “hello” printed out.

NFS clients can now connect to the DIY NAS.

Samba Share (for Windows)

Samba is the Windows-preferred protocol.

Start by installing samba (e.g., sudo apt-get install samba smbfs) on the server. Then edit /etc/samba/smb.conf to include the new section:

[nas]
    path = /mnt/nas
    valid users = myusername
    read only = no

This approach restricts access to myusername. It is generally preferred for security reasons. However, if you’re just using the NAS for media and just want to make sure that anybody can access it, this may work better:

[nas]
    path = /mnt/nas
    read only = no
    writeable = yes
    browseable = yes
    public = yes
    create mask = 0777
    directory mask = 0777
    force user = root

This configuration instead makes everyone who connects to the drive act as the root user and gives them full access to reading and writing. It’s “insecure” in that anybody on the network can do anything to the data. But for some use-cases, this may be a feature rather than a bug (e.g., a dedicated media drive which friends can dump files into).

Finally, use sudo systemctl restart smbd to restart the Samba server before testing from the Windows device. To connect to the Samba drive, type the IP address (\\192.168.0.100\nas in my case) in the windows File Explorer. Note that both machines must be in the same Workgroup (on the server side, the Workgroup is also a line in smb.cnf).

Automatic Backups

The simplest approach to automatic backups is rsync.

Let’s assume you have a second directory, /mnt/backups, which is either a second hard drive or some sort of safe location to backup files. The rsync utility (sudo apt-get install rsync) has long been the favorite simple way to make a backup. Open up crontab -e and add something like:

0 2 * * * rsync -a --delete /mnt/nas/ /mnt/backup/
  • 0 2 * * * executes at 2am each day.
  • -a uses archive (backup) format—which preserves permissions, symlinks, ownership, etc.
  • --delete will remove files from the target which have been deleted from the source.
  • (Optional) add the -v flag to see what’s going on.

If your backup destination is on a different computer (or off-site), add the -e ssh flag and specify the IP address for the backup (it uses the same syntax as scp):

0 2 * * * rsync -a --delete -e ssh /mnt/nas/ user@192.168.0.100:backup/

The Home NAS Build

At last, it’s time to use the DIY NAS to build a multimedia server.

Stay tuned or subscribe for the next post—turning a NAS into a multimedia server.

Build Guides

Looking for even more detail?

Drop your email in the form below and you'll receive links to the individual build-guides and projects on this site, as well as updates with the newest projects.

... but this site has no paywalls. If you do choose to sign up for this mailing list I promise I'll keep the content worth your time.

Written by
(zane) / Technically Wizardry
Join the discussion

1 comment