Running a dedicated computer at home to act as a server is much easier — not to mention more useful — than it used to be. It can speed up the internet while providing a great deal of privacy + security in a home build server.
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.
Thank you for the information on building a DIY NAS. I’ve been toying around with completing a project like this, so now I have no excuse.