Network attached storage (NAS) servers provide centralized file storage and sharing capabilities over a network. While commercial NAS units can be expensive, you can build your own low-cost NAS with a Raspberry Pi. In this comprehensive guide, I‘ll walk you through the entire process of setting up a NAS server with Raspberry Pi.

Why Set Up a Raspberry Pi NAS?

Here are some of the main benefits of using a Raspberry Pi NAS:

  • Low Cost – The Raspberry Pi board and accessories are very affordable compared to off-the-shelf NAS units. You can build a capable NAS for less than $100.

  • Energy Efficient – The Raspberry Pi uses very little power, especially compared to repurposed desktop PCs. This saves on electricity costs.

  • Quiet – With no noisy fans, the Raspberry Pi makes for quiet operation. You can tuck it away anywhere without noise or vibration issues.

  • Customizable – You can add as much or as little storage as you need. Want to start small? Add a larger drive later? No problem!

  • Learning Experience – Setting up a DIY Raspberry Pi NAS is fun and educational. You‘ll learn Linux skills in the process.

Now let‘s look at the hardware and software you‘ll need.

Hardware and Software Requirements

To build your NAS, you‘ll need the following core hardware components:

  • Raspberry Pi board (any model will do)
  • MicroSD card (8GB Class 10 minimum)
  • Power supply
  • USB storage drives
  • Ethernet cable

In addition to the hardware, you‘ll need an operating system and some software packages:

  • Raspberry Pi OS – The official Linux-based OS for Raspberry Pi. Easy to use with great community support.

  • Samba – An open source SMB server allows file sharing across Windows, macOS, and Linux devices on your network.

  • SSH – Lets you control your Pi remotely via SSH terminal for headless operation.

  • rsync – Useful for syncing files between devices and performing data backups.

I‘ll be using Raspberry Pi OS Lite for a minimal install focused on NAS functionality. But the regular version with desktop works too.

Okay, with the ingredients covered, let‘s start cooking!

Installing Raspberry Pi OS and Enabling SSH

Download the Raspberry Pi Imager from the Raspberry Pi Foundation and install it on your computer. Use the Imager to flash Raspberry Pi OS Lite to your microSD card.

With the OS flashed insert the microSD into your Pi, connect ethernet, and apply power. By default Raspberry Pi OS has SSH disabled. Let‘s enable that now.

Create an empty file named ssh (no extension) on the root of the flashed card. This will enable SSH when it boots.

Eject the microSD card from your computer, insert it into the Pi, and power it on. Determine your Pi‘s IP address using the router admin interface.

Now you can SSH into your Pi remotely using terminal with the ssh command. Log in using the default credentials:

ssh pi@192.168.1.123
password: raspberry

Awesome! You now have command line access to configure your server.

Mounting External USB Drives

Now it‘s time to connect external USB drives to use for storage. For this you‘ll need:

  • USB hard drive enclosure and HDD
  • Large USB thumb drive

Attach your USB storage devices to Raspberry Pi. Then verify that Linux detects them with lsblk:

lsblk
NAME        MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda           8:0    1   3.7T  0 disk 
└─sda1        8:1    1   3.7T  0 part
sdb           8:16   1  14.9G  0 disk

The first drive sda is a large 4TB HDD. The second sdb is a 16GB USB thumb drive. Linux has assigned device names for the drive‘s partitions as well.

Use mkdir to create mount point directories for the drives:

sudo mkdir /mnt/USBHDD
sudo mkdir /mnt/USBThumbdrive

Next mount the drive partitions to these directories:

sudo mount /dev/sda1 /mnt/USBHDD
sudo mount /dev/sdb /mnt/USBThumbdrive

Check that the drives mounted properly using df -h:

df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root        29G  2.1G   27G   8% /
devtmpfs        459M     0  459M   0% /dev
tmpfs           463M     0  463M   0% /dev/shm
tmpfs           463M  6.8M  457M   2% /run
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           463M     0  463M   0% /sys/fs/cgroup
/dev/mmcblk0p1   44M   22M   22M  51% /boot
/dev/sda1       3.7T   33M  3.7T   1% /mnt/USBHDD   
/dev/sdb        14.9G   61M  14.4G   1% /mnt/USBThumbdrive

The mounts were successful! Our external storage is now available at /mnt/USBHDD and /mnt/USBThumbdrive.

Installing and Configuring Samba for Sharing

With our storage mounted, it‘s time to install and configure Samba for network file sharing.

Use apt to install Samba like so:

sudo apt update
sudo apt install samba

Once installed, back up the default Samba config file:

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.backup

Edit the smb.conf file to configure shares:

sudo nano /etc/samba/smb.conf  

Scroll to the bottom of smb.conf and add a share definition for each storage mount:

[USBHDD]
       comment = USB Hard Drive
       path = /mnt/USBHDD
       read only = no
       create mask = 0775
       directory mask = 0775
       public = yes

[USBThumbdrive]
       comment = USB Thumb Drive
       path = /mnt/USBThumbdrive
       read only = no
       create mask = 0775
       directory mask = 0775
       public = yes

This creates two network shares called USBHDD and USBThumbdrive. Authentication and permissions are wide open for now with public = yes.

Save and exit smb.conf. Restart the Samba service to apply the new configs:

sudo systemctl restart smbd

We‘re ready to connect!

Connecting to Shared Drives from Linux and Windows

With Samba fired up, our NAS shares are ready for access. We can connect in a couple ways:

1. Connect via Linux command line:

Use the smbclient command to view available shares:

smbclient -L //192.168.1.123

Now connect using smbclient with your pi username and password:

smbclient //192.168.1.123/USBHDD -U pi

You now have interactive file access. List files with ls, get and put files like normal. Exit back to terminal with exit.

2. Connect via File Explorer on Windows 10:

Click the File Explorer icon on your Windows desktop taskbar. Click Network in the left pane. You should see workgroup computers and your Raspberry Pi NAS!

Double click on the Pi‘s name or IP address. Enter your Samba credentials when prompted to connect.

Now your NAS shares appear under Network locations. Access them like any shared network folder on Windows!

So we‘ve verified connectivity locally and remotely over both Linux and Windows. Pretty slick!

Let‘s lock things down and enhance security a bit.

Improving NAS Security

Having open public shares is convenient but not very secure. Let‘s create a dedicated NAS user and set some permission policies.

Step 1 – Add a NAS User

We‘ll create a separate user account to manage the storage for better permission separation.

Log into the Pi and add the new user:

sudo adduser nasuser

Next give it a Samba password:

sudo smbpasswd -a nasuser

Step 2 – Update Shares for Private Access

Edit /etc/samba/smb.conf once again:

sudo nano /etc/samba/smb.conf

Update your shares by changing public = yes to public = no and adding valid users:

[USBHDD]
       comment = USB Hard Drive 
       path = /mnt/USBHDD
       read only = no
       create mask = 0775
       directory mask = 0775
       public = no
       valid users = pi, nasuser

[USBThumbdrive]       
       comment = USB Thumbdrive
       path = /mnt/USBThumbdrive
       read only = no
       create mask = 0775  
       directory mask = 0775
       public = no
       valid users = pi, nasuser

This restricts access to only the pi and nasuser accounts that have Samba credentials configured.

Restart Samba:

sudo systemctl restart smbd

The shares will now require authentication for access. Nice!

Step 3 – Apply Folder Permissions

As one final hardening step, restrict NAS access to only the nasuser account:

sudo chown -R nasuser:nasuser /mnt/USBHDD
sudo chown -R nasuser:nasuser /mnt/USBThumbdrive

The nasuser now has exclusive filesystem rights for reading, writing, deleting, and creating files on those partitions.

Even if someone compromised or guessed the pi user password they could not access the NAS storage thanks to these strict permissions. Awesome!

Configuring Rsync for Automatic Backups

At this point we have a solid NAS file server going. Now let‘s enable some more automation by configuring Rsync to perform regular data backups.

In addition to network shares, Rsync is handy for syncing data between devices on LAN. Our NAS makes the perfect little network backup server!

Here is how to schedule a cron job that rsyncs files from our Ubuntu desktop to the NAS each night:

Step 1 – Install Rsync

sudo apt install rsync -y

Step 2 – Configure Public Key Authentication

Generate SSH keys on the backup source server (our Ubuntu desktop for example):

ssh-keygen -t rsa

Copy the public key to the NAS Pi:

ssh-copy-id nasuser@192.168.1.123

Enter the nasuser password. Now you can SSH/Rsync without entering passwords.

Step 3 – Test Rsync

Manually perform an initial rsync from Ubuntu to the NAS share:

rsync -r -a -v -h --progress /home/user/Documents/ nasuser@192.168.1.123:/mnt/USBHDD

This recursively syncs (-r) all files/folders (-a) from my Documents folder into /mnt/USBHDD on the NAS. Cool!

Step 4 – Make a Cron Job

Add a line like this to crontab for scheduled backups:

0 2 * * * rsync -r -a -v -h --delete --progress /home/user/Documents nasuser@192.168.1.123:/mnt/USBHDD

That will sync files daily at 2 AM. The --delete parameter removes stale data on the destination not on the source anymore.

Schedule multiple cron jobs to back up any folders you want!

With that our DIY Raspberry Pi NAS project is complete! We now have a low-cost, energy efficient 24/7 storage server for all devices on our home network. Pretty awesome!

Here are some ideas for extending this project even further:

  • Set up dynamic DNS for easy remote access
  • Install Plex Media Server for network streaming
  • Add cloud syncing to backup your backups
  • Configure monitoring tools and email alerts
  • Build a dashboard to track stats

I hope you found this guide helpful. Setting up your own NAS with Raspberry Pi is very rewarding. Thanks for reading and happy building!

Similar Posts