Turn Your Raspberry Pi Into An NAS Box

Ads by Google

featured pi nas   Turn Your Raspberry Pi Into An NAS BoxDo 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.

ubs drives   Turn Your Raspberry Pi Into An NAS Box

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.

Ads by Google

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:

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.

advanced options   Turn Your Raspberry Pi Into An 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.

fdsik   Turn Your Raspberry Pi Into An 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.

samba config   Turn Your Raspberry Pi Into An 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.

file on nas   Turn Your Raspberry Pi Into An NAS Box

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.

Ads by Google
Check out more about:

30 Comments - Write a Comment

Reply

nel6t9

what is the read/write rate to the shared drives on R.Pi? I assume the bottleneck will be the processor of the R.Pi?

Karl Rosenqvist

I don’t think so. Unlike a x86 Windows machine Pi is well architected and runs Linux so the main CPU won’t have much to do. Most likely USB transfer speeds are the bottleneck.

Reply

Richard Mullens

Everybody seems to assume that we want to use the Raspberry Pi with a windows box – but what if you want to use it to back up a linux environment where file names are case sensitive then surely NTFS is no good for this ?

Am I correct in this ?

Reply

Vassili

NTFS is probably one of the worst choices in this case. Other users have gotten 2-4 times faster transfer with ext4. Considering that the bottleneck is the 100Mbit ethernet adapter, those few extra MB/s make a real difference. Also, in general, NFS is significantly faster than Samba on the Pi.

James Bruce

Good advice Vassili, though it’s still a pretty crap and underpowered NAS by any standard. I still think NTFS is better for data recovery though – if your NAS screws up formatted with EXT4, most people won’t be able to get their data back.

Reply

Sallal

That’s a pretty awesome tutorial you got there. I’m a total Raspi newb and it helped me a lot! I’ve actually done even a little more to make my Raspi+HDD compatible with my PS3 and Samsung TV, which apparently only support DLNA Media Servers (I’m not 100% sure about that statement, but anyways…)

What I did was (root!):
mkdir /media/1/shares/Music
mkdir /media/1/shares/Video
mkdir /media/1/shares/Photo
apt-get install minidlna
nano /etc/minidlna.conf

Look for “media_dir”, you’ll find it quite at the beginning.
Change the setting to:
media_dir=A,/media/HardDrive/Media/Music
media_dir=V,/media/HardDrive/Media/Video
media_dir=P,/media/HardDrive/Media/Photo

also change (just below):
db_dir=/home/pi/.minidlna

Also uncomment (remove the #) at:
log_dir=/var/log

Save and Exit.
You are able to start the DLNA by using “service minidlna start”, You can also make it start at boot with “update-rc.d minidlna defaults”. In case you change the configuration sometime, restart minidlna using “service minidlna force-reload”.

That’s it! You should be able to see your Raspi on your PS3, Smart-TV and your WMPlayer now :)
Regards

based on: http://www.raspberrypi.org/phpBB3/viewtopic.php?t=16352

Reply

Mirko

Hi there,

I did the tutorial over twice but I am constantly landing on my SD card instead of my USB drive.
Now I am a bit lost as to what to do. I can´t redo the
mount -t auto /dev/sda1 /media/1
part as it tells me the device is mounted and umount is blocked because it is busy.

This is my fdisk -l readout:

fdisk -l

Disk /dev/mmcblk0: 31.9 GB, 31893487616 bytes
4 heads, 16 sectors/track, 973312 cylinders, total 62291968 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: 0x00047c7a

Device Boot Start End Blocks Id System
/dev/mmcblk0p1 8192 122879 57344 c W95 FAT32 (LBA)
/dev/mmcblk0p2 122880 62291967 31084544 83 Linux
Note: sector size is 4096 (not 512)

Disk /dev/sda: 3000.6 GB, 3000590401536 bytes
255 heads, 63 sectors/track, 45600 cylinders, total 732566016 sectors
Units = sectors of 1 * 4096 = 4096 bytes
Sector size (logical/physical): 4096 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000c5748

Device Boot Start End Blocks Id System
/dev/sda1 256 732566015 2930263040 7 HPFS/NTFS/exFAT

I have a 3 TB USB device atached (just one).

Since I ran things twice I am afraid other greenhornes like me run into the same problem. So any help is greatly appreciated.

Thanx,
M.

James B

Hi Mirko – Ive been confused by the first bit – “constantly landing on SD card instead of USB”. It looks like youve got a single 3tb NTFS partition there, which sounds good. Which bit are you having problems with?

Reply

Mirko

Hi James,

I can´t write. When I conect from my mac I can log in with the credentials set but if I want to copy a file MacFinder asks me for my password and then it can´t write.
In my folder there´s a file called “pistore.desktop”.
My only explanation was that my mount directories refered me to the SD card instead of the USB drive and that´s why I could not write to it.

Mirko

Reply

Mirko

OK I figured it out. In case anyone else has my problems here´s how to:

1) I did not land on my USB drive because I forgott to
mkdir /media/1/shares
But the Samba config file pointed exactly there.
100% my fault.

2) The reason I could not write to my USB drive was because I followed the tutorial blindly exactly the way it was written.

It seems that if you

mkdir /media/1/shares logged in as pi@raspberrypi or somethng else and then later

useradd jamie -m -G users (or any other user)

then this new user (as which we try to conect via samba) does not have the rights to the folder made as pi@raspberrypi

So we have to

sudo chown jamie /media/1/shares

in order to ake this “jamies” folder.

Well I guess I get laughed at now but in case anyone else is a sstupid as I am I hope this helps ;-)

Mirko

James B

Glad you got it sorted. Though the tutorial does say substitue “jamie” for your own username!

Reply

Derek

Pro Tip for Mac clients: Get rid of .DS_Store etc litter with smb.conf’s ” veto files=” option. Also a setting to auto delete vetos.

Reply

Daniel

Ran in to a bit of a problem. Same as for Mirko I had problems accessing the files, but managed to fix it using chown.

Hi guys, great guide!

But now I cant seem to change rights so that i’m able to write to the disk. when I try to use chmod it returns “Operation not supported” for each some of the files on the disk. (NTFS)

I do get “drwxrwxrwx 1 share root 4096 Sep 12 21:47 disk1″ from “ls -al /mnt/” but still cant write to the disk from windows.

Anyone with any great ideas of things to try?

Reply

Daniel Jäger

Thx al lot for your how to :-) I configured my pi that way and all is working fine.

I did one more thing to prevented the new created user “jamie ” from login via ssh with the following commands:
sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sudo nano /etc/ssh/sshd_config

I addes the following line at the end of etc/ssh/sshd_config:
AllowUsers pi

Reply

Andrew Donkin

Regarding the comparison of NTFS versus ext4, why not keep your master data in ext4 (or your choice of fast, journaled, Linux filesystem) and rsync it over to an NTFS partition of the same size? You will have the best of both worlds: speed, native permissions, and case sensitivity on the master and convenience on the slave.

Just be careful not to create two entries in a directory with the same name but different CaSeS, and tweak your rsync invocation to allow some slop in time stamps, permissions, and filenames, and don’t put more stuff in the ext4 partition than the NTFS one has room for.

Thanks for the write-up James: great instructions. You’ve made my rpi-NAS project kind of a no-brainer :-) One uncertainty remains: do you have any tips for ensuring your external USB drives spin down?

James B

Thanks Andrew, that’s genius. Unfortunately, I have no idea how to force a drive to spin down, but as far as I know that’s something handled in the drive firmware, not an OS thing. So you shouldn’t need to.

Reply

rakesh

how to access the files from android?

Reply

Dan

Try using lidroid file explorer for android. There’s a free samba client plugin. Recognized my share immediately.

Reply

Lino

Great tutorial!

I was planning on buying a Seagate Central, but I thought I’d play around with my RasPi and 1TB external HD first.

I found out that when I stream saved tv shows from the NAS to my laptop (in XBMC) and the other laptops copy files from the NAS, my videos start stuttering and web browsing becomes a bit slow.

I was planning on just buying a 3TB external hardisk and hooking it up to the RasPi if I got this to work, but will it help the stuttering and the web browsing if I just buy the commercialized NAS like the Seagate Central?

James B

Depends on your internal network, I would say. Streaming over wifi is going to be bad whatever else you’re doing – but generally I wouldnt try streaming from the Pi anyway. I would say get a real NAS, and make sure your internal cabling is gigabit; forget wifi, unless no other alternative.

Reply

Lino

Thanks for the reply, James. I think I’ll stick to my original plan of getting the Seagate Central.

Looking forward to other Pi articles!

Reply

shywolf91

Could the nas be access over the internet (outside my local network).

I need something to access my drives when I go on vacation in mexico.

James B

I believe you would need to setup a VPN server on your Pi, then connect to that remotely. Not something I know how to do I’m afraid.

Reply

Michael

Hi James!

Great tutorial, I can get the most to work; however I got major issues with autofs part. The line that should be added seem to point to a file (/etc/auto.ext-usb)? This file is missing in my system.

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

I looked into some other guides as well and to evaluate the auto.master I stop the autofs service and then I use automount -f -v to get som error messages and those complain about a directory (that is added as default in the auto.master). see messages below:

Starting automounter version 5.0.7, master map /etc/auto.master
using kernel protocol version 5.02
lookup(dir): dir map /etc/auto.master.d missing or not readable
lookup(file): failed to read included master map dir:/etc/auto.master.d
lookup(file): failed to read included master map auto.master
file map /etc/auto.ext-usb not found
mount_autofs_indirect: failed to read map for /media/
handle_mounts: mount of /media/ failed!
master_do_mount: failed to startup mount
no mounts in table

My auto.master looks like this (see below)

# Sample auto.master file
# This is an automounter map and it has the following format
# key [ -mount-options-separated-by-comma ] location
# For details of the format look at autofs(5).
#
#/misc /etc/auto.misc
#
# NOTE: mounts done from a hosts map will be mounted with the
# “nosuid” and “nodev” options unless the “suid” and “dev”
# options are explicitly given.
#
#/net -hosts
#
# Include /etc/auto.master.d/*.autofs
#
+dir:/etc/auto.master.d
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master
/media/ /etc/auto.ext-usb –timeout=10,defaults,user,exec,uid=1000

Have I missed something in the tutorial? Is it possible to get an example on how this auto.ext-usb can look like?

Best Regards
Michael

Birdy

I have the same problem !!
How does the auto.ext-usb file look like ??

James B

Hmm, sounds like you’re running a different flavour of the linux OS, maybe? That bit of the instruction might be specific to Raspian, so you’d have to check with other versions.

Birdy

I have 2013.09.25 Raspbian Weezy installed, from http://www.raspberrypi.org/downloads

which version are you running. ??
Is ist possible to post the auto.ext-usb file or email it to me ???

Thanks
Birdy

Reply

GB

Hi. Thanks for your tutorial.
I have three usb hard disks connected via an hub to raspberry ;I followed your instructions
mkdir /media/1
mkdir /media/2
mount -t auto /dev/sda1 /media/1
mount -t auto /dev/sdb1 /media/2
mount -t auto /dev/sdc1 /media/3

but sometimes with fdisk -l command I find sdb1 is associated with disk that I defined sdc1 and viceversa and also in startx I find disk inverted. This is dangerous because I make rsync between disk1 and disk2, but if sdb1 and sdc1 are inverted I loss my data.

Can you help me?

Thanks

James B

I’m afraid thats a bit beyond my linux knowledge. I’d suggest asking about how to do that on our Answers site: http://www.makeuseof.com/answers

Your comment