3698 views 5 comments

Project #1: Create a Network attached storage | Raspberry Pi 2

by on November 4, 2015
 

As you guys know, I recently bought the revolutionary $35 computer, Raspberry Pi 2. I have had quite a fun time experimenting with it’s capabilities to understand how I can utilize it in a best way possible. I did a alot of quick dirty projects like Twitter Bot, FTP/HTTP servers, SensorPi, RetroPi, Ad blocker DNS etc. So, now I plan to redo all of them in a neat manner so as to keep a log of why and how to do it.

NAS – What & Why?

Aim is to use a Raspberry Pi 2 with an external hard drive to create an ultra-low-power and always-on network storage device aka NAS (Network Attached Storage). A network storage device is one of the best ways to keep your data backed up, but it can be a little pricey to set up and leaving a computer on all the time sucks a lot of power. So, If you’re looking for a cheap and low power solution, you are at the correct blogpost.

Prerequisites.

  • Raspberry Pi (any gen).
  • USB WiFi dongle (or Ethernet cable to connect to the network).
  • USB Hard drive/Pen drive.
  • 30 minutes of your life.

Preface

  • I will be using NTFS-formatted hard disks/USB drives.
    Why? Let’s assume our NAS setup fails for some reason or may I want to quickly copy information over a USB 3.0 connection instead of via the network, having NTFS-formatted disks makes copying data as simple as taking the portable USB drives out from NAS and plug them right into one of the many Windows machines I use every day.
  • I will be using Samba for my setup because of the convenience of accessing the Raspberry Pi NAS with my predominantly Windows network.

Let’s begin!

Now, if your Raspberry Pi is not up and running with some OS, I would suggest you to read my original post and once you have it all setup, come back here. I am using Rasbian OS but any supported Linux flavour should be good.

Network attached storage setup with the Raspberry Pi 2

The Basic setup

Setting up Storage media:

  1. Since all of the setup will happen in the Terminal, you can either open LXTerminal if you have a Display hooked up with RPi or you can SSH into your headless Raspberry Pi via Putty. Either way should be good enough.
  2. Once you’re at the command line the first thing you need to do is to add in support to Rasbian for NTFS-formatted disks. To do so type the following command:
    sudo apt-get install ntfs-3g
  3. Wait for package to download & install. Plug in your HDD/Pen drive and enter the following command to get a list of all disk(mounted or unmounted) attached to the Pi.
    sudo fdisk -l

    getting a list of disk attached to Raspberry Pi

    All storage disk available to RPi

    The first disk /dev/mmcb1k0p1 is the SD card inside the Raspberry Pi that houses our installation of Raspbian. I am using a Sandisk 32GB card, we are going to leave it as it is. The second disk, /dev/sda is my 32GB USB 3.0 Pendrive. The actual partitions we’re interested is /dev/sda1.

    💡 Ideally you should use a portable HDD for NAS arrangement but mine required an external power-supply and I am a bit lazy, so bear with the Pen drive. 😎

  4. But as of now, the pendrive is not mounted. Before we can mount it, we will have to create a new directory for it to keep the things organised. I will name my directory “Skynet” for obvious reasons 😛
    sudo mkdir /media/Skynet
  5. Now, let’s mount our storage media (32GB Pendrive) to “Skynet“. 
    sudo mount -t auto /dev/sda1 /media/Skynet
  6. After mounting, lets verify the contents.
    cd /media/Skynet
    ls -l

    mounting NAS storage media Raspberry Pi

    We are up!

Setting up Samba!

Now we will begin with the second phase of installation of Samba network file system on our Pi.

  1. Install Samba via Aptitude.
    sudo apt-get install samba samba-common-bin -y
  2. Relax and make a coffee while it’s installing and setting up Samba. It may take a minute or two.
  3. Once the Samba package finishes installing, let’s get our hands little dirty and do some configuration. But first, before we mess up with wrong configurations, let’s make a backup copy of the Samba configuration file in case we need to revert to it. At the command line, type the following command line:
    sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.old
    This will simply create a backup of the *.conf file as *.conf.old.
  4. Now open smb.conf with your favourite text editor (nano/vim/leafpad etc.)
    sudo nano /etc/samba/smb.conf

    samba configuration file to create NAS with raspberry pi

    Time to configure & stuff.

    Nano is very much like Windows Notepad. Only difference is instead of a fancy GUI, it’s wholly keyboard-operated.

  5. Now make sure to follow the changes below and do the same in your smb.conf file.
    • In the beginning of file you will have an option to change the name of workgroup/NT-domain name your Samba server will part of.
      workgroup setting - NAS on Raspberry Pi
      Leave it as it is if you have no idea what it is and you will be good.
    • Next, we will setup authentication for our NAS or else anyone in the network will be able to access your precious data and we do not want that. Do we? 😉
      Scroll the conf file till you get to the Authentication section.
      adding authentication to NAS with Raspberry Pi
      Remove the # symbol from the security = user line (by highlighting it with the cursor and pressing delete) to enable username/password verification for the Samba shares.
    • Now we will add a new configuration block to the file. So, scroll to the end of the file and add this block with your respective settings.

      Let me explain what the snippet above means.
      > [Skynet] – Name of the folder on network share.
      > comment – Just to describe your NAS.
      > path – the folder you want to share over the network (here USB storage mount path).
      > valid users – users authenticated to access the NAS
      > force group – specifying primary group of users connected to the NAS
      > directory mask – file permission(s) to the valid user/group
      > read only – well, read only or not.

  6. Press CTRL+X to exit, press Y when asked if you want to keep changes and overwrite the existing configuration file.
  7. When back to shell, enter the following command to restart samba services for the new configuration to be in effect.
    sudo /etc/init.d/samba restart
  8. Now remember we added authentication user group in the smb.conf file above. Let’s create a new user that can access the Pi’s samba shares aka Skynet. 😉 I am going to create a user with following credentials
    • username: johnconnor
    • password: judgementday
  9. To create an user with above credentials, type in:
    sudo useradd johnconnor -m -G users
    and to create a password for johnconner:
    sudo passwd johnconnor
  10. You’ll be prompted to type in the password twice to confirm. After confirming the password, it’s time to add “johnconnor” as a legitimate Samba user. Enter the following command to do so:
    sudo smbpasswd -a johnconnor

After the steps above. We now have a fully function Network Attached Storage (NAS) arrangement working with Raspberry Pi. Now it’s time to give it a test run.

Showdown time!!

We can now hop onto any Samba-capable machine in our network and test connectivity to the Skynet.

>> Accessing from a Windows device:

Step #1: Open Windows Explorer

1. Open Windows Explorer - Setting up NAS with Raspberry Pi

Step #2: Click on Network in left panel
2. Click on Network - Setting up NAS with Raspberry Pi

Step #3: Double click on your Raspberry Pi hostname
3. Open Raspberry Pi - Setting up NAS with Raspberry Pi

Step #4: Enter credentials for user – johnconnor
4. Authenticating John connor user - Setting up NAS with Raspberry Pi

Step #5: Profit.

5. All the data - Setting up NAS with Raspberry Pi

>> Accessing from an iOS device:

We have plenty of apps which can access Network storage but for my iPhone, I found Remote File Manager Free to be very reliable and feature rich. Just make sure you are in same network, fire up the app and connecting to your NAS is as easy as Pie (get it?) 😉

NAS is great especially for fixed storage devices like iPhone where you can keep all your multimedia data in the central storage and access it anywhere in the network.

>> Accessing from an Android device:

Similarly you can use Asus’s File Manager (one of the best file manager client I know) to access your NAS and interact with the data. Since I didn’t have an Android device available at the moment of writing this post, I won’t be post screenshots for that. I tested it on a friend’s OnePlus One and Samsung Galaxy S6 and it was able to play videos, songs flawlessly.


So, this was it guys. You have now successful implemented a basic NAS system. Do let us know how we can further enhance it? Also, feel free to suggest or share your own DIY Raspberry Pi projects in the comment section below. Peace. 😎

comments
 
Leave a reply »

 
  • Mark
    February 13, 2017 at 4:02 am

    I’m having the same problem.
    I’ve followed as best as I can although, I have had some differences:
    I’m using a RPi3-ModB….
    Setting up Storage media – step 5: it seems the Pi has already recognised the device and mounted it for me as I get error msg saying unable to mount device. Also the directory is slightly different: “/media/pi/Maxtor_HD1”
    Setting up Samba – step 5: there is no setting for “security = user” in the smb.conf file. I tried adding it but it seemed to make no difference.
    In windows 10, I can see the RPi and HD on the network although I can’t authenticate with my user details. (double checked).
    good article though!

    Reply

  • February 1, 2016 at 8:22 pm

    Hey Dude!

    I have followed your steps, I have created NAS successfully! but the problem is that i am not accessing the pendrive when i login and it says access is denied? Am i missing something?

    Reply

Leave a Response