Wireless NAS using Raspberry Pi

Sanskar Biswal
TheTeamMavericks
Published in
8 min readApr 6, 2020
R-Pi NAS

Network Attached Storage (NAS) is a very popular method to store and share files and documents on a local network. However, it is not extremely easy to get started with and often involves a bit of a steep learning curve. The most common bottleneck is that the commercially available hardware is often expensive and difficult to assemble and route. It does not help that software to run the NAS from several service providers is costly and honestly not worth all the money that they ask for.

Thankfully it is possible to create our own NAS using Raspberry Pi. There are several tutorials available online however, many of them addressed only a few of the objectives needed for our use case. Additionally, we needed to transmit the data via wireless channels whereas most NAS tutorials are directed towards Ethernet networking.

The Idea and Implementation of NAS

Network Attached Storage, as the name suggests, allows a storage media or device to be connected and accessible from a network. Traditionally, NAS is used in offices to store common and active projects on a server and the variety of peripherals are connected via an Ethernet cable and a network router. As college student who wanted to implement NAS in our dorms, this method was not efficient.

We built a wireless NAS server in our college dorm so that we could stream from a hard disk containing movies and web series and stream them directly to either our phones or laptops. It might seem a bit over the top and even redundant in the era of web streaming, but poor WiFi and signal congestion in our college dorm led us down this rabbit hole.

We sometimes needed to sit outside in the corridor to get an average reception.

WiFi speed averaged 200 Kbps and additionally we had a monthly limit of 10 GB per student. The limit was eventually raised to 100 GB per month but still not stream worthy speeds.

The poor WiFi in college was just one of the reasons. As students, we felt the need to have common entertainment media as well as books and games on a common and shareable platform, to avoid having the same content on each of our hard drives. The problem was that if one person was using it, the others had to wait. It was from this that the current project was conceptualized to build a NAS, which can be used to wirelessly stream movies and access all common content like books and game installation files. It was also necessary that the NAS could be accessed from our smartphones and laptops.

Here are the things you will to make this project:

  • Raspberry Pi 3B
  • Ethernet Cable (headless mode connection)/ HDMI Cable (if you have a monitor)
  • Hard Disk Drive
  • VLC Media Player on mobile and laptop. (You can use any media player. We just used VLC in our case)

Target Audience

As much as we would like to make a tutorial for people of all levels of technical proficiency, it is not feasible. On the same lines, this project is not a guide on how to get started on Raspberry Pi. Hence, if you are a beginner, we welcome you go through the blog but we would suggest other resources to learn how to use setup and build simple projects on Raspberry Pi before trying to build this project. You can nevertheless follow along this tutorial if you are curious.

If you are someone who wants to develop your own wireless NAS and have some experience using Raspberry Pi, this tutorial should be very easy to follow along.

This blog is a step-by-step guide to developing a wireless NAS primarily used to stream content from a Hard Disk. You are free to use this as reference to modify the project for your specific use case.

Setting Up The Raspberry Pi

We will not cover how to setup the Raspberry Pi in this article. There are many tutorials available online to do the same. So at this point we will assume that you are all set up with your Raspberry Pi.

Create a Wireless Hot-Spot on Raspberry Pi

Since our objective is to use the NAS without connecting to the Server, we obviously need to set up a hot-spot on the Raspberry Pi. To do so, open a terminal with the Pi connected (Ctrl+T)

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install hostapd dnsmasq
$ sudo systemctl stop hostapd dnsmasq

We now need to modify the config files of dns and dhcp protocols that we just installed. We use the builtin Nano editor in the terminal. If Nano is not installed, simply run the following command.

$ sudo apt-get install nano

Now we modify the dhcp config to setup WLAN and setup static IP addresses. The IP is made static because it will make it easy for us to connect to the NAS from the media player.

$ sudo nano /etc/dhcpd.conf

Navigate to the bottom of the file in the terminal and add the following config commands to the file.

interface wlan0
static ip_address=192.168.0.10/24
denyinterfaces eth0
denyinterfaces wlan0

To save a file on Nano: Ctrl+O, To exit from Nano: Ctrl+X

The IP address we chose was with reference to the builtin IP. Next we change the dnsmasq config.

$ sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig
$ sudo nano /etc/dnsmasq.conf

We add the following to the end of the file to specify the range of IP addresses that the hot-spot can generate. The following configuration will give about 60 addresses. However, while this theoretically means the 60 connections can be established, we would not recommend going beyond 12 as then the streaming of movies will be extremely buffer-prone. However, if your use case requires only small file transfers and not video streaming, up to 50 connections can be handled safely. The following config also specifies that the connection can remain active for 24 hours.

interface=wlan0
dhcp-range=192.168.137.11,192.168.137.70,255.255.255.0,24h

We now need to setup the specific configuration of the Hot-spot that we need to host, including the name of the wireless network and the password.

$ sudo nano /etc/hostapd/hostapd.confinterface=wlan0
bridge=br0
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ssid=piHub
wpa_passphrase=root1234

As can be seen from above, our NAS name will appear as piHub and the password will be root1234. You can change them to whatever you feel necessary for your specific use case. Just keep in mind that the ssid and password should not be very long.

In the next step we need to assign the above config file to the host access point config. So run the following line in the terminal and search of the line which says #DAEMON_CONF=”” — delete that # and put the path to our config file in the quotes.

$ sudo nano /etc/default/hostapd— — — — — — — — — — — — — — 
#DAEMON_CONF=””
— — — — — — — — — — — — — —
++++++++++++++++++++++++++++++
DAEMON_CONF=”/etc/hostapd/hostapd.conf”
++++++++++++++++++++++++++++++

Now we will have to update the IP Tables. IP tables keep track of the internal IP addresses and which IP, if any has a special purpose. Run the following command and find the following line #net.ipv4.ip_forward=1 and remove #:

$ sudo nano /etc/sysctl.conf— — — — — — — — — — — — — 
#net.ipv4.ip_forward=1
— — — — — — — — — — — — —
++++++++++++++++++++++++++++
net.ipv4.ip_forward=1
++++++++++++++++++++++++++++

Save the above file.

$ sudo iptables -t nat POSTROUTING -o eth0 -j MASQUERADE
$ sudo sh -c “iptables-save > /etc/iptables.ipv4.nat
$ sudo nano /etc/rc.local+++++ Add the following lines before exit(0) ++++++
iptables-restore < /etc/iptables.ipv4.nat
+++++++++++++++++++++++++++++++++++++++++++++++++++
$ sudo apt-get install bridge-utils
$ sudo brctl addbr br0
$ sudo brctl addif br0 eth0
$ sudo nano /etc/network/interfaces++++ Add the following lines at the end +++++++++
auto br0
iface br0 inet manual
bridge_ports eth0 wlan0
++++++++++++++++++++++++++++++++++++++++++++++++++
$ sudo reboot

If all steps were followed correctly, then piHub network should now be visible in your available Wifi Networks.

Setup Networked Media Player on Pi

Now that we can see the hotpost hosted on the Raspberry Pi. Connecting to it would mean that your device and the Pi are on the same connection. But the directory which needs to be shared between the Pi and your system still needs to be set.

Only the directory defined as shared directory can share information. You can also set the access to the directory and limit the users to only reading from the directory.

In our case, we needed the contents of the HDD to be available on the shared directory. We also needed to be able to stream video content from the HDD. We first setup the VLC connection.

$ sudo apt-get install vlc samba samba-common-bin
$ sudo apt-get install ntfs-3g
$ sudo mkdir /home/pi/piHub
$ sudo nano /etc/samba/smb.conf
+++++++ Add to end of File ++++++++
[RASPBERRYPI] # User Defined name. Will appear in VLC
comment=SMARTBUSentertainment
browsable=yes
writable=no
path=/home/pi/piHub
create mask = 0777
directory mask = 0777
browsable=yes
public=yes
+++++++++++++++++++++++++++++++++++
$ sudo smbpasswd -a pi #username in VLC
$ password #password for VLC network

On the VLC app on user phone, the user has to click the ☰ on top-left hand side of the app and from the options select ‘Local Storage’.

Local Network Access on VLC Media Player

Here RASPBERRYPI will be visible. This is provided the user is first connected to the Access Point created earlier (‘piNetwork’). The user has to enter the password and he/she can easily browse the contents of HDD.

Note: The HDD drive should be connected to the Pi before the system is turned on. Otherwise the code in Pi will not be able to discover a drive later on. As of the existing code, only 1 HDD may be connected.

We will however need a python script to run every time 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’
“””
Main Python Handler
“””
import os

if os.path.exists(“/sda/dev1”):
os.system(“sudo mount /dev/sda1 /home/pi/piHub”)
else:
pass

The main.py file needs to run on boot. This will ensure that Hard Disk or sda1 is mounted and accessible in the piHub folder. Earlier we set the piHub folder to be the VLC directory. piHub is a shared folder we have defined and can be accessed from mobiles as well as from laptops.

The exact process to run a python script on boot is indicated in the following blog-post: https://www.raspberrypi-spy.co.uk/2015/02/how-to-autorun-a-python-script-on-raspberry-pi-boot/

Conclusion

This project was developed in the Summer of 2018, using the exact same procedure as mentioned in this tutorial. However, we understand that the Raspbian image may have been updated since the project was developed to keep track with the latest Ubuntu version.

Thus, the link given below, we have included the working Raspbian Image. This raspbian image file is configured according the steps in the tutorial. So, if you want, you can directly download this image and burn it on the Raspberry to have a functional wireless NAS.

Useful Links:

--

--

Sanskar Biswal
TheTeamMavericks

Electronics Engineer | Firmware Developer | Programmer | Poet | Writer