56tvmao: How-to instructions you can trust. Linux How to Build a NAS Server with Raspberry Pi And Samba

How to Build a NAS Server with Raspberry Pi And Samba

A network-attached storage device (NAS) allows you to access files over your local network, and you can build one inexpensively with a Raspberry Pi. Best of all, the setup process is easier than you might think, and I’m here to guide you through it.

Content

What You’ll Need

Let’s gather all the necessary components before we start. Obviously, you’ll need a Raspberry Pi. The Pi 5 or Pi 4 are ideal for faster file transfers, but Pi 3 works too. You’ll also need a microSD card (32 GB minimum) with fully updated Raspberry Pi OS.

While you could technically use the SD card for file storage purposes, it’s much better to get an external storage device. Spinning hard drives are best for long-term file archiving, but you can also use an external SSD or even a USB flash drive.

If you’re serious about data protection, you might want to consider using multiple drives in a RAID configuration that mirroring your data across drives to protect it against drive failures. For more information, you can refer to our guide on RAID.

Setting Up Samba

1. Prep your storage

First, you need to prepare your storage device so that you can use it to store the data you want to be accessible over your local network.

If you’re creating a folder on your system SD card (which I don’t recommend), you can simply run the following command to create a directory:

mkdir /home/pi/nas-storage

To use an external drive (the recommended approach), you need to format it with a suitable file system. I strongly recommend ext4 for the best possible compatibility with Linux. Fire up your terminal and run:

sudo fdisk -l

Look through the output and spot the drive you want to use for your NAS (it will most likely be “sda” or “sda1”, but it could also be “sdb” or “sdb1” if you have multiple drives connected). Once you’ve identified the right drive, make a mental note of its name – you’ll need it for the next command, which unmounts the drive so that you can format it (replace “NAME” with whatever you found in the output of the command above):

sudo umount /dev/NAME

Now it’s formatting time. The following command will erase all data on the drive, so double-check you’re using the correct device. Once you’re ready, you can enter (make sure to change “NAME” with the device name that you made a note of earlier):

sudo mkfs -t ext4 /dev/NAME

After formatting, you might want to label your drive for easier reference later:

sudo e2label /dev/NAME YOURLABEL

Replace “NAME” with your drive’s identifier and “YOURLABEL” with your preferred label. Now let’s mount the drive so we can use it:

sudo mkdir -p /mnt/nas-storage
sudo mount /dev/NAME /mnt/nas-storage

After that, it’s a good idea to reboot, so go ahead and enter:

sudo reboot

2. Install Samba

Now that our storage is ready, we need a way for other devices to access it over the network. This is where Samba comes in. It’s a fantastic piece of software that enables file sharing between Linux and Windows machines (and yes, Macs too!).

I like using Samba because it’s reliable and can be installed with a single command:

sudo apt install samba samba-common-bin

The command will download and install everything you need for basic file sharing. Once installation completes, Samba will automatically start running as a service in the background, but we’re not quite done yet because we still need to configure it to use the storage device we prepared in step 1.

3. Configure Samba

Now we need to tell Samba what we want to share on our network by editing its configuration file. You can access the Samba configuration file by running:

sudo nano /etc/samba/smb.conf

Once you’re in, scroll down to the bottom and add a few extra lines to tell Samba what you want to share. It should look something like this:

[sambadrive]
path = /mnt/nas-storage
writeable=Yes
read only=no
create mask=0777
directory mask=0777
public=no

Here’s what each setting does:

  • [sambadrive] is the name of our NAS that other devices will see on the network.
  • path points to where we mounted our drive.
  • writeable = yes allows users to modify files.
  • create mask and directory mask set file permissions.
  • public = no means users need a password to connect.
  • browseable = yes makes the share visible in network browsers.

With that, you’re done with the config file! Hit Ctrl + O and Enter to save (just to be safe!), then Ctrl + X to close the file.

4. Create a Samba user

Next, you’ll need to make a Samba username/password. The default Raspberry Pi user is just called “pi,” so that’s an easy one to go with. Enter the following command:

sudo smbpasswd -a pi

This will create a Samba user called “pi” with a password. You’ll get a prompt to enter a password. Type it in, hit Enter, and do it again.

Next, restart Samba to load the changes using:

sudo systemctl restart smbd

You’re all done with the setup! Now, you can access your NAS from a different machine.

Access your NAS from Windows

Now comes the fun part – connecting to your new NAS from Windows. Open File Explorer and look for the “Map Network Drive” button. In newer Windows versions, you’ll find it under the “Computer” tab when you’re in “This PC”, or it might be labeled as “Map as Drive” under “Easy Access” in other folders.

Clicking this opens a setup dialog where you’ll need to enter your Pi’s network address. Type \\raspberrypi\nas-share in the Folder box (remember, “nas-share” is what we named our share in the Samba config). You can also use your Pi’s IP address instead of “raspberrypi” if you’re having trouble connecting.

Make sure to check the “Connect using different credentials” box, then click Finish. Windows will ask for your username and password – these are the Samba credentials we created earlier (username “pi” and the password you set).

Your NAS drive should now appear in File Explorer, ready for you to start storing and accessing files over your network.

Access your NAS from macOS

Connecting to your NAS from a Mac is just as straightforward as Windows. In many cases, you can simply select it from the left sidebar.

If you can see it in the left sidebar, then you can open Finder and press Cmd + K, or click “Go” in the menu bar and select “Connect to Server.” In the “Server Address” field, type smb://raspberrypi/nas-share (or use your Pi’s IP address instead of “raspberrypi” if needed). Click “Connect” when you’re ready.

A login window will pop up. Select “Registered User,” enter “pi” as the username (or whatever your username is) and the Samba password you created earlier. Check “Remember this password in my keychain” if you don’t want to enter it every time. You can now drag and drop files just like any other folder on your Mac.

Troubleshooting common NAS issues

Setting up a NAS with Samba on a Raspberry Pi is usually trouble-free, but you might encounter a few common issues. Here’s how to solve them quickly and get back to using your NAS.

Access denied or permission errors

If you’re hitting walls when trying to access or write to your shared folders, it’s often because your Pi user doesn’t have the proper ownership. To resolve this:

sudo chown -R pi /mnt/nas-storage

Replace “pi” with your username if it’s different, and “/mnt/nas-storage” with the path to your NAS folder. This command gives the Pi user ownership over the entire directory, which should fix most permission issues.

NAS share not visible

If your NAS doesn’t appear in Windows File Explorer or macOS Finder, there could be a few different reasons why. First, double-check that both your Raspberry Pi and your computer are on the same network. If they’re connected to different Wi-Fi networks or using different network adapters, they won’t see each other.

Second, make sure that the Samba service is actually running on your Raspberry Pi. You can check by running the following command:

sudo systemctl status smbd

If it’s not running (you will see an inactive status), start it up with this command:

sudo systemctl start smbd

Connection keeps dropping

One of the more frustrating issues you might encounter with your Raspberry Pi NAS is the connection dropping, especially when it’s connected via Wi-Fi. This can manifest as intermittent access to your shared files or the NAS becoming completely unreachable for periods.

Often, this problem arises due to Wi-Fi signal issues or interference from other devices. However, if you’ve confirmed that your Wi-Fi network itself isn’t the culprit, the issue might be related to the Raspberry Pi’s power-saving features.

You see, the Pi’s Wi-Fi can enter power-saving mode, which sometimes leads to these connectivity drops, particularly if it’s not actively transmitting or receiving data. Fortunately, you can easily disable this power-saving feature. All you need to do is add one line to your “/etc/rc.local” file:

/sbin/iwconfig wlan0 power off

As you can see, even annoying issues with Samba are fairly easy to troubleshoot and fix, which is just one more reason why it’s so great for basic file sharing. But if you’re looking for more advanced features like user management, scheduled backups, or a web interface might want to check out OpenMediaVault.

Image credits: Raspberry 4 Model B Screenshots by Andrew Braun and David Morelo.


David Morelo
Staff Writer

David Morelo is a professional content writer in the technology niche, covering everything from consumer products to emerging technologies and their cross-industry application. His interest in technology started at an early age and has only grown stronger over the years.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox

Sign up for all newsletters.
By signing up, you agree to our Privacy Policy and European users agree to the data transfer policy. We will not share your data and you can unsubscribe at any time. Subscribe

Related Post