Turn Your Raspberry Pi Into An NAS Box

Ads by Google

raspberry pi nas controllerDo you have a couple of external hard drives lying around and a Raspberry Pi? Make a cheap, low powered networked attached storage device out of them. While the end result certainly won’t be as impressive as a $500 NAS device like the Synology DiskStation, it will give you a low-powered bit of network storage – particularly useful if you’re getting weary of having all your data rifled through by the prying eyes of the NSA – you can hide this in the attic.

You’ll need a Raspberry Pi, of course, and one or two spare drives. Smaller 2.5″ drives can be powered directly over USB, but we’re going to need a powered hub as the power provided over the RPi’s USB ports is just not enough for them. Alternatively, you can use a USB thumbdrive, or even an SD card. In fact, I’ve used a mix of a USB hard drive and a thumbdrive today, but the procedure is identical.

raspberry pi nas controller

With just one drive, you can still make a shared network storage area, but with two you’ll be able to setup data redundancy in case one fails.

Prepare Your Drives

Start by formatting your drives as NTFS from a desktop. This is for convenience, so that if anything goes wrong we’ll be able to disconnect them from the NAS and still read the data from any PC.

We could format them from the Raspberry Pi, but it will take a few hours and is far quicker to perform from a desktop. Do that now.

To configure SSH and enable the root user, first create a password for root user:

Ads by Google
sudo -i
passwd root

(type your password)

Then run the raspi-config script from the command line, either using sudo or having logged out and in again as root. From advanced options menu, enable SSH.

raspberry pi nas box

After restarting, you should be able to login from another networked machine using (use Putty if you’re on Windows)

SSH root@[IP address]

Once logged in, figure out which devices are your additional drives. I’m assuming you’ll be using two for data redundancy. Type

fdisk -l

to list the attached storage devices. You should see something like this.

raspberry pi nas box

the /dev/mmc partitions are you Pi operating system, mmc referring to the SD card. Confusingly, the /dev/sda1 and /dev/sdb1 are actually nothing to do with the SD card, and those are in fact your attached USB drives. (Originally, “SCSI device”, but now means any attached SATA or storage device)

Install ntfs-3g for linux so we can access the NTFS formatted Windows drives.

apt-get install ntfs-3g

Next, create directories to use as mount points, then mount the drives. I’m keeping it simple here and referring to them as 1 and 2.

mkdir /media/1  mkdir /media/2  mount -t auto /dev/sda1 /media/1  mount -t auto /dev/sdb1 /media/2  mkdir /media/1/shares  mkdir /media/2/shares

Samba

Next, we’ll set up Samba. Samba is the network sharing protocol used by Windows (and the newest OSX Mavericks, in fact).

apt-get install samba  apt-get install samba-common-bin  cp /etc/samba/smb.conf /etc/samba/smb.conf.bak  nano /etc/samba/smb.conf

If you’re not familiar with these kind of config files, a # at the start of the line means it is commented out, and therefore not currently set or configured. To enable something, you can either add a new line, or un-comment an existing line to make it active.

We’ll start by enabling user security; press CTRL-W and type “security” to find the relevant section. Remove the # symbol from the line that says

security = user

Lastly, scroll down to the bottom (or hold CTRL V until you reach there) and add as many network shares as you like. Use the following format:

[test]  comment = Test share  path = /media/1/shares  valid users = @users  force group = users  create mask = 0660  directory mask = 0771   read only = no

Only refer to the first mounted drive though – we’ll be syncing this later with the 2nd share to provide redundancy.

Once you’re done, hit CTRL X and then y to save.

create a raspberry pi nas box

Then restart Samba with the following command.

service samba restart

Now, add a new user to your Pi, assuming you don’t want the same login (substitute “jamie” for your own user)

useradd jamie -m -G users

After typing in the following command, you’ll be prompted to enter a password for your user, and confirm it.

passwd jamie

Then we can go ahead and add this system user to Samba. You’ll need to confirm your password again, twice.

smbpasswd -a jamie

Go ahead and test the network share now – it should be visible from your other machines (Windows or Mac), and you should be able to write files to it.

raspberry pi nas controller

The only problem at this point is that the drives will be unmounted when you restart the Pi. To solve this, install autofs.

apt-get install autofs  nano /etc/auto.master

Add the following line underneath +auto.master

/media/ /etc/auto.ext-usb --timeout=10,defaults,user,exec,uid=1000

Now you should be able to restart safely without breaking everything

Data Redundancy

Assuming you installed two drives, we can now setup an automatic script for syncing data from the 1st drive to the 2nd, thereby offering us a backup in case one fails. We’ll use the rsync utility for this.

apt-get install rsync  crontab -e

The crontab in linux is a way of automating tasks; I talked briefly about before when showing you how to automate site backups. Add following line:

30 5 * * * rsync -av --delete /media/1/shares /media/2/shares/

The numbering scheme is used like this:

minute | hour | day-of-the-month | month | day-of-the-week

So in our newly added line, the rsync command wil be run at 5:30 am , every day (the * wildcard meaning “every”, so “every day of every month”)

If you want to go ahead and run the backup immediately, just paste in the rsync command like so

rsync -av --delete /media/1/shares /media/2/shares/

Depending on what you put in the shared folder, it may take a few seconds or longer to give you a report. The great thing about rsync is that it knows which files are updated, added or should be deleted. Go ahead and try the same command again. It should finish instantly, because it knows nothing has changed.

That’s it, finished – you now have your own quick and dirty NAS. Yes, it might not have all the flashy features of a proper NAS, but it gets the job done nicely and for much less power consumption.

Are you having problems? Let us know in the comments and I’ll see what I can do, but please ensure you’re running the latest Raspian image.

Join live MakeUseOf Groups on Grouvi App Join live Groups on Grouvi
Raspberry Pi Projects
Raspberry Pi Projects
16 Members
DIY Tech Projects
DIY Tech Projects
13 Members
Arduino Projects
Arduino Projects
13 Members
Ads by Google
Comments (37)
  • Owen Page

    Could I use this for music streaming to a HiFi like Sonos?

  • Semih Tekten

    This project is on Pi Hunt’s top list! http://www.pihunt.co/toplist

  • harguer

    I thinks it’s better to use lvm/raid1 instead of using rsync…

  • Alexis Dominguez

    Thanks for the DIY.
    I got it setup but when i acces the drive from my windows machine it says that is only a 7.16GB but the HD is a 1TB?
    Any way to fix that?

    pi@ADraspberry ~ $ sudo fdisk -l

    Disk /dev/mmcblk0: 7948 MB, 7948206080 bytes
    4 heads, 16 sectors/track, 242560 cylinders, total 15523840 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0x0009bf4f

    Device Boot Start End Blocks Id System
    /dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)
    /dev/mmcblk0p2 122880 15523839 7700480 83 Linux

    Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
    255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 sectors
    Units = sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disk identifier: 0xe8900690

    Device Boot Start End Blocks Id System
    /dev/sda1 63 1953520064 976760001 7 HPFS/NTFS/exFAT

    • Owen Page

      How large is your Pi’s SD card? You might be seeing that and not the HDD.

      If not check the drives partitioning, you might have several partitions. If this is the case delete the extra partitions and expand the 7GB one to fill the entire drive.

  • Justin Rowe

    HI there I’ve tried this Tut 5 times and copied it word for word but I can’t access the folder?
    On my mac I get operation cannot be completed because the original item for ‘test’ can’t be found and on my PC the share doesn’t even show up?

    Any help would be great, thank you

Load 10 more
Affiliate Disclamer

This review may contain affiliate links, which pays us a small compensation if you do decide to make a purchase based on our recommendation. Our judgement is in no way biased, and our recommendations are always based on the merits of the items.

For more details, please read our disclosure.
Affiliate Disclamer

This review may contain affiliate links, which pays us a small compensation if you do decide to make a purchase based on our recommendation. Our judgement is in no way biased, and our recommendations are always based on the merits of the items.

For more details, please read our disclosure.