Raspberry Pi as NAS

Mona
3 min readDec 4, 2020

Raspberry Pi as NAS(Network Attached Storage)

If you have a raspberry Pi and want a network-attached storage device.

Here’s how to turn your Raspberry Pi into a NAS for file sharing.

What is a NAS?

Network attached storage is a server that makes storage available on a network.It provides centralized and shared storage for digital files.

Required Accessories:

You will need a Raspberry Pi , a power supply, a microSD card 32GB ,a keyboard ,a mouse and a monitor. A external hard drive or USB external drive to fill up with your files you want to share among devices.

Set It Up:

Start by installing Raspbian on microSD card . Download the latest version of NOOBS and install it to a microSD card.

Insert the microSd card into your Raspberry Pi and connect keyboard,mouse and HDMI cable.

Once you’ve booted up Raspberry Pi hook it to a wireless network for file transfer.

Most of the work will be done on terminal.

From a terminal window run the following command to see the disks connected to your Pi:

sudo fdisk -l

To share the drive on your network, so that we can add files and access them from any device on our network.

Raspbian doesn’t come with Samba installed by default so you need to install it with the following commands:

sudo apt update

sudo apt upgrade

sudo apt install samba samba-common

Now edit the configuration file to share your drive.

sudo nano /etc/samba/smb.conf

Scroll to the bottom of the document.

Add a block of text given below:

[SharedMedia]

path = /media/pi/NAS/

writable = yes

create mask = 0775

directory mask = 0775

public = no

SharedMedia is the name of your share. You can name it whatever you want.

And /media/pi/YourHardDrive is the mounted location of your drive. In my case I have named it NAS.

When you are done, press Ctrl+O and Ctrl+Z to save and exit nano.

Finally you have to create password for Samba so you can see your share from other machines.

To add a password to the existing Pi user :

sudo smbpasswd -a pwd

Once this is done run the following command to restart Samba:

sudo systemctl restart smbd

Samba service will start and the shared drive will now be available on the network.

We will need a python script to run everytime the Pi is booted to load and assign the HDD contents to the correct location.

The code needs to be in the following location in the Pi, /home/pi/code/

main.py

# ‘/home/pi/code/main.py’

“””

Python Handler

“””

import os

if os.path.exists(“/sda/dev1”) :

……os.system(“sudo mount /dev/sda1 /home/pi/NAS”)

else:

……pass

The main.py file needs to run on boot.

This will ensure that Hard Disk is mounted and accessible in the NAS folder.

To find SharedMedia on the computer network:

Click file explorer icon and choose Network option.

You will see SharedMedia in the list of devices.

Click it and then ‘Enter network credentials’ window will appear.

Enter the username and the password you created.

Click remember my credentials and click ok.

File explorer will display the shared directory.

You can drag and drop files and folders to the shared folder and they will be copied to your Raspberry Pi.

To access SharedMedia from mobiles :

Install Solid Explorer from Play Store.

Click on RASPBERRYPI.

Start adding files and folders to the shared folder and they will be copied to your Raspberry Pi.

Conclusion:

It was fun making this NAS. Assembling all the required Accessories was simple.

By writing few lines of code and magic happened.

Happy Sharing!!

Check my next article for something interesting!

--

--