Turn a Raspberry Pi into a NAS (Network Attached Storage) Server

In this tutorial, I’m going to talk you through turning your Raspberry Pi into a NAS server. A NAS (Network Attached Storage) server allows you to hook up a hard drive, and access it over your local network (or over the internet if you really really wanted).

Why use a Raspberry Pi as a NAS server?

If you have an existing external hard drive lying around that does not have NAS capability, its an easy way of allowing one or more device to use its space. The Raspberry Pi is super cheap and uses hardly any power, so it’s a win-win situation.

Assumptions and what you’ll need

For this tutorial, I’m going to assume you know your way around Terminal and have followed my previous tutorial to use a USB Flash Drive to boot your Raspberry Pi.

I’m going to assume that you also have a spare external Hard Drive. I’m using a Weston Digital Essentials 2TB in this example, although you could use any hard drive. Mine’s already got a HFS+ partition as I’m a mac user.

Step 1 – Install some partition support software

Boot up your Raspberry Pi without your external hard drive plugged in. The first thing we’re going to do is install a package to allow reading and writing to NTFS partitions (assuming this is what most people are likely to use). Do the usual bit of housekeeping:

sudo bash
apt-get update

Now to install the NTFS compatibility:

apt-get install ntfs-3g

Optional – install the HFS packages to read Mac volumes:

apt-get install hfsplus hfsutils

Step 2 – Install SAMBA

Samba is the name of the package that allows us to mount our external hard drive over the SMB protocol. The SMB protocol is what windows uses for those ugly ‘\servershare’ sharing paths. Macs have native support for SMB, at least they do since around Snow Leopard. To install SAMBA and the other packages we’ll need, run this command:

sudo apt-get install samba samba-common-bin

Once that’s done, the SAMBA service should start automatically.

Step 3 – Prepare your partitions

Now plug in your external hard drive. The reason why this isn’t done at boot is because if you’re running Raspbian from a USB flash drive like me instead of from the SD card, your Raspberry Pi will look to the external hard drive for the OS. Type the following:

fdisk -l

This will list the partitions on your external hard drive. Yours may look similar to mine, or you may only have 1 partition. Either way, something will show.

Shown above are the two USB attached storage, the first (on sda) is the USB Flash Drive from which Raspbian runs (see tutorial) and the second, sdb shows the external hard drive with 4 partitions on it.

Now we’re going to create a directory to mount our external hard drive to. Create a directory within ‘/media/’. I’m calling mine ‘HDD01‘ but you can call yours whatever. Create a directory for each partition you intend on sharing:

mkdir /media/HDD01

Now mount your external hard drive to the directory created. I’m mounting sdb4 in this example, but yours will probably be sdb1. Refer to your earlier partition list.

For mounting your HFS (mac) partitions use:

mount -o force /dev/sdb4 /media/HDD01

For mounting your Fat32 and NTFS (windows) partitions use:

 mount -t auto /dev/sdb4 /media/HDD01

Next we’ll create a folder on the external hard drive in which our shares will be placed:

mkdir /media/HDD01/shares

Step 4 – Configure SAMBA

Before we start playing with the SAMBA configuration, it’s probably a good idea to backup the stock configuration, just in case something goes horribly wrong! To do this, type:

cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

Now that we’ve done this, we can start configuring SAMBA. Type the following command to begin editing the standard configuration:

nano /etc/samba/smb.conf

Nano will launch, ready to begin changing the configuration.

We’re going to configure SAMBA to make use of User Authentication. This keeps our shares safe from network guests, and adds an additional level of security. I’m a big fan of using Nano’s search, so look for ‘security‘ by pressing CTRL + W, or keep pressing down arrow until you get to the security bit. Uncomment out security = user by removing the # from the start of the line:

This will enable username/password level security. In the next step, we’ll create our network shares.

Step 5 – Create network shares

Arrow down to the very bottom of the configuration. This is where we’ll create our shares. To keep things neat, I like to put a title, but this is completely optional. In this example, I’m going to create a ‘media’ share and a ‘work’ share. Type the following into Nano:

###### SHARES ###### 

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

[work]
comment = Work share
path = /media/HDD01/shares
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no

A folder will be created in the shares directory dependent on what’s inside the square brackets, so in this example, it’s media and work. Now exit nano by pressing CTRL + X and saving the config file when prompted.

Restart the SAMBA service for the configuration to be loaded:

/etc/init.d/samba restart

Now we’re going to create a user to access the NAS. In this example, I’m using ‘ste‘ but use whatever you like:

useradd ste -m -G users

Create passwords for your user. You will be asked to confirm the password too. Type the following command:

passwd ste

And finally, we have only one last thing to do, and that’s to connect the local user password with SAMBA and confirm the password:

smbpasswd -a ste

Test your Raspberry Pi NAS

That’s it, now it’s time to test your NAS. Open Finder or Windows Explorer and navigate to your Raspberry Pi. I’ve changed my hostname to DingleberryNAS for ease of finding it:

Click on ‘connect as’ and type in the user details. You will notice a third share for the user you created, it’s safe to ignore this.

Job done! As per usual, please sound off your thoughts by commenting, feel free to share the page with friends or on your own blog.

Ste

Web Developer living in Manchester, working for Studio Skylab (http://www.studioskylab.com). Views and thoughts are my own.

You may also like...

60 Responses

  1. Awesome idea — I’ll need to try this over the weekend 🙂

  2. emu says:

    So far, so good. Thanks for the tutorial, but I can’t copy files in any of the folders via Finder with my Mac. I get the error “…no reading rights”. Da you have an Idea? Thanks

    • Ste W says:

      I need to have a look into this, a couple of other people who emailed me have had the same problem as you.

      It’s probably just a small permissions step that’s been missed off the tutorial. Will take a look over the tutorial and see what’s been missed,

      • emu says:

        Yes, I guests it is something like: sudo chown -R pi:pi /media/HDD01

        But that command did not work for me, got something like permission denied

        • John Italy says:

          Hey there! had the same problem couple of weeks ago!
          the trick is about the mounting permission i think, in fact i found many tutorials on the web bout mounting external hd drives onto Debian, but when you mount it is mounted only for root user.
          i mount them (permanently) editing the fstab
          Step 1.
          sudo fdisk -l
          find out what’s your hd label! should be something like /dev/sdb1, but it depends on your system. i’m gonna use /ddev/sdb1 as label.

          Step 2.
          sudo nano /etc/fstab
          add this line
          /dev/sdb1 /media/HDD01 auto gid=,uid=,noatime 0 0
          of course you have to change with the user you want to be able to read the file on the NAS.
          step 3.
          reboot
          you are done! In this way you will have your hd drive permanently mounted and you chose who will be able to read or write on the NAS (once you installed samba correctly).
          a little suggestion: look for a debian guide next time not just a RPi guide, this little board is magic because of Linux!
          Have Fun

          • Mike says:

            of course you have to change with the user you want to be able to read the file on the NAS.

            Sorry, what should I change with the right user?
            I found out that the id of the user and group should be after uid= and gid=. Is that correct?

            So I have

            /dev/sda1 /media/HDD01 auto gid=100,uid=1002,noatime 0 0

            for user 1002 (my created user, that I found in ls -l /dev/disk/by-uuid/ and group ‘users’ 100)

            Can’t get it to work properly though…

          • Mike says:

            Sorry, of course sudo nano /etc/group to know the id of my user.

  3. Aaron M says:

    I had the permissions error when I set up my NAS. I just used Batchmod and set Read and Write access for everyone before putting files on the drive. Took care of my problem!

  4. kiawin says:

    Hi, you can try to chmod your shared directory.

    # chmod -R 3755 /media/HD001

  5. Harris says:

    Great tutorial! Can we make the mounting and creation of shares persitent between reboots? (is it even possible?)

  6. Vezoan says:

    Hi I am planing to build this NAS to serve my media player i want to check reliability and performance of yours so far.

  7. Tim says:

    I can’t find my raspberry pi in windows explorer, what should I do to find it?

  8. 7YearOld says:

    hahahaha PiNAS

  9. Any idea how you could turn this into a “cloud” storage solution you could access from anywhere? Would you need something like ownCloud?

  10. yvdv says:

    Thanks for fantastic tutorial. I was able to setup NAS. But like others I am facing problems of permissions – unable to copy any files to the shares created.
    Any suggestions to overcome this problem is appreciated.

  11. Thomas says:

    To fix the permission problem you have to make the user account you added with ‘smbpasswd -a username’ the owner of the mount location and all it’s subdirectories by running this command on the Raspberry Pi ‘sudo chown -R username.users /path/to/mountpoint’. You could also alter the permission to read write (read write execute for directories) for a group and add all users that need write permission to this particular group. ‘man chmod’ can help you with that.

    To make the whole affair persistant and automounting just add the following line to /etc/fstab.
    /path/to/device /path/to/mountpoint ext4 defaults,noatime 0 0
    Alter it to match your paths and filesystems. For me this line looks like this.
    /dev/sda1 /media/freecom ext4 defaults,noatime 0 0

  12. Thomas says:

    I meant read and execute for directories but there is no way to edit.

  13. jimu says:

    I think the shares problem maybe caused by missing configure the ” read only = no ” in smb.conf

  14. mike says:

    Did you get a response at all to the problem you was having finding your shared folder on your windows pc ?

    Im also having the same issue

  15. Gregor says:

    I followed tutorial step by step. At the end everything looks similar but:
    1. Next to “work” and “media” folder I also see some strange folder called “ste” when connected over smb.
    2. Folders work and media are also there, but I can write only to “ste”.

    The question is:
    How to remove ste folder from showing up in smb?
    How to correct permissions for “work” and “media” that they will allow writing?

    I tried fixing permissions with suggestions from comments but with out success.

    Regards,
    Gregor

  16. Andrew Yiu says:

    I’m getting stuck at the fdisk part of the tutorial. It seems that when I plug in my hard drive, the pi reboots itself and the device can’t find the hard drive. Is it because the hard drive is portable and relies on the pi to power it but can’t draw enough power? If so, I also have a powered desktop external drive, but will all the data on the drive be erased when I partition it?

  17. Dunc says:

    Good stuff, I recommend a couple of extras after getting your NAS going:

    1) If the drive is spinning constantly, try http://hd-idle.sourceforge.net/
    2) If you’re mounting an NTFS partition with Samba and are bothered about performance, consider these:

    http://www.raspberrypi.org/phpBB3/viewtopic.php?t=13695&p=305794#p313789
    http://www.raspberrypi.org/phpBB3/viewtopic.php?t=13695&p=305794#p314403

    + potential overclocking. These two posts alone doubled my write speed to 6Mb/s.

  18. Ste W says:

    I think it’s pretty easy to see a lot of people are having problems with the tutorial, I’ll have a look at rewriting it. I’ve followed it myself and had problems too, sorry about this guys! Bear with me!

  19. MortenDK says:

    Hey.. Can you make a tutorial, on how to access the nas from the internet…

  20. De La Pommeroi says:

    I ordered my Pi less than 2 weeks ago , got it last Thursday , checked out the raspbian os and tried one of the XBMC flavours available. BOINC was quickly my number 2 …. But with three hard drives over 1 terabyte I most definitely had to have the Pi set up as NAS server as well. Since these drives were HFS mac formatted this tutorial gave me all the tools I needed and then my brain froze dead at step 5. I got the shares folder working then did some weird stuff and tried to mount my HDD in my home folder but … I tried this which I guess is very similar and yayyyy!

    #external media drive
    [my HDD name]
    comment = External Media Drive
    browseable = yes
    writeable = yes
    path = /media/my HDD name
    guest ok = yes

    It’s set up now serving 3.5 terabytes of files , running BOINC and a bit of use of the rasbian system and the xbmc on board when time allows! Great tutorial / help and Dingleberry flavouring the Pi . I look forward to trying the web server soon :p

    • Darren Roberts says:

      I have tried to mount a freshly formatted USB drive to my RPi and all I get is the following error:

      Mount is denied NTFS is already exclusively opened

      Any tips?

      I have plugged the drive back into the windows 7 machine and checked to see if I can “eject it” but the option isn’t there.

      Very annoying !

  21. ggvw says:

    Thank you for this article. Just a tip. There is a web interface for configuring Samba that makes configuring Samba much easier (SWAT: Samba Web Administration Tool). It prevents you from having to type in all the configuration manually.

    sudo apt-get install swat

    Then go to http://ipaddress:901/ and login as root.

  22. rob says:

    this is incredibly slow…
    there is something wrong with sharing a drive using samba (ntfs), I was trying to watch HD movie using this solution – not possible, works too slow 🙁

  23. jozka says:

    Hi, I followed the steps, but I have problem: external USB drive is already mounted:
    /dev/sdc5 fuseblk 932G 687G 246G 74% /media/VIDEO
    If I add this to samba( path = /media/VIDEO), I can not access it.
    If I umount it and mount it again as HD001 (sudo mount -t auto /dev/sdc5 /media/HDD01) – it works, but after reboot I’m back to the first mount.

    So now I do not know, how to force to mount this drive to my folder. I’m new to linux, so I’m stacked. I have tried to edit fstab, but after reboot the drive got different /dev/sdd5 instead of sdc5, so it also did not help.

    So what shall I do? set rights to this /media/VIDEO for ste user ?

    • jozka says:

      so small update to work-around this problem

      I edit rc.local

      nano /etc/rc.local

      and simply put there

      sudo umount /dev/sda5
      sudo mount -t auto /dev/sda5 /media/HDD01

      and after reboot it is working fine, disk is mounted as HDD01 and it is shared via samba on network….

  24. Martin says:

    I tried this yesterday, with a HDD that already was half full. Wasn’t able to access those files, and when I reconnected the drive to my mac it didn’t show in Finder. Later it said that the HDD was broken and needed repair.

    Any thoughts?

  25. hisham says:

    Format the USB to NTFS works fine

  26. Manuel Beer says:

    I get to following error while trying to mount the hfs+ hard drive, maybe it’s necessary to say that this hard drive used to be in a Mac and it still contains data which i need. :/
    mount -o force /dev/sda1 /media/TimeMachine
    mount: wrong fs type, bad option, bad superblock on /dev/sda1,
    missing codepage or helper program, or other error
    In some cases useful info is found in syslog – try
    dmesg | tail or so
    any ideas? I googled a lot, but was able to find a working solution :/
    cheers

  27. Plz help says:

    I cant install samba D:

  28. Pea says:

    fdisk -l
    doesn’t work for me (OS X 10.7.5) to list the partitions. I use:
    diskutil list

  29. Jay says:

    This guide is fantastic, easy to follow and extremely useful for those who are sort of new to pi but have a little experience with Linux. Thank you so much, I had no issues what so ever.

  30. I use to be able to find good informstion fropm your articles.

  31. Mark Bourke says:

    Only 3gb of my 250 is showing up. Any ideas?

  32. docsuds says:

    Is there an updated version of this project? thx!

  33. ImBeinFoRealuh says:

    Got it all working but when I connect from my mac it’s still dropping the files in the sd card, not the hard drive. Any way to make the files I save go into the hard drive?

  34. dave says:

    on the mount in fstab use the uuid of the disk you want to mount. Also, I believe you can specify the group to allow access, but you need to also change the permission right in the mount statement, and they aren’t as simple as it appears as you are taking away priviledges, not adding them. If you search the Ubuntu forums for mount external ntfs disk you can get the information. Obviously the file system type might need to be specified in the mount in fstab.

  35. robv says:

    I ran through this NASberry pi tutorial and it was the clearest, simplest one among the many out there.
    I did it on a 32GB usb stick as a sort of a trial before committing to a 2/320GB RAID that will hang off my Hackintosh.

    A puzzling thing: each ‘share’ is showing only about 240MB , what about the remainder up to 32GB?

    Another puzzle: I tried to wipe it and found a linux type website that took me through disk menu, n p etc
    and did the deed as a FAT32 scheme ( originally i used NTFS as in your tutorial ) and lo and behold whether i ssh from my MBAir or click on my raspberrypi shared folder, everything is still there ??? even though on a linux laptop it reports empty

  36. Alex says:

    I had a few difficulties with the guide, but eventually I got there thanks to the advice from some of the users. Also as a general tip, be very careful about whether there is a space between some of the text which you’ll be entering.

    For fdisk -l
    That is an L, not a capital i. Silly mistake, but I got caught up for a few minutes on that.

    Then in step 3 when you get to this stage:
    mount -t auto /dev/sdb4 /media/HDD01
    (Where you replace sdb4 with your own storage device’s location)
    I was getting the Mount is denied NTFS volume is already opened error message.
    You need to use the following first, and then you’ll be able to mount it:

    sudo umount /dev/sdb4
    (Replace sdb4 with yours)

    For step 4, I didn’t actually find the security = user section, so I ignored that step.

    When you make your network folders (the author has used two folders “media” and “work”), I’d just make one folder instead (call it whatever you want).

    Follow the remaining steps in the author’s guide to set up your user.

    Then follow the steps posted by John Italy in this thread about the fstab instructions. Make sure you watch the spaces between words – he has typed it correctly but it is hard to see. For your gid and uid, simply type:

    id

    and press enter – I think mine was 1000 and 1000. Try to do it when you aren’t in root (type exit and press enter) – I can’t remember for certain but I think I might have gotten a different result in root.

    You should now be able to access your hard drive through the Windows Network section after you enter the username and password.

    Side note:
    If you had set up 2 folders like the author’s instructions (e.g. “media” and “work”), when opening the drive from Windows, regardless of what you put in “media”, it would populate “work” as well. I don’t think it’s actually duplicating the files, but either way you really only had one folder, so it was a bit redundant to set up 2.

    The author’s original solution (without the fstab suggestion) worked perfectly for me when I was in the terminal (setting up “media” and “work” on two separate, usable folders), but when I rebooted to my OS it would stop working – hence the fstab solution was needed.

    I don’t have a Linux background and I found this a little challenging – best of luck to those who are trying this out!

  37. Marvin says:

    Putting the following into smb.conf for the share made the access denied problem when trying to write files go away for me:

    force user = root

  38. Luke Murgatroyd says:

    Love the guide! Im trying to setup something similar, two user groups. 2 shared folders. One group can see both folders, one group can only see one. Anybody managed to get this to work?

  39. Andy says:

    Right! Having spent most of 3 evenings messing about with a R Pi 3 and all the guides and helps available and getting nowhere at all, I gave up. I finally tried ubuntu mate, it took 30 mins to setup. Including setting up the shares and users etc. My advise to anyone looking for file shareing / NAS etc, just use Ubuntu Mate – dead easy.

  40. Dp says:

    Hi Andy, could you post more detail on how to make nas using ubuntu mate (article link will be appreciated) and how much is the read write speed you are getting using ubuntu mate

  1. 26th May 2013

    […] Storage) Server Posted on May 26, 2013 by News Collector — No Comments ↓ By Ste W In this tutorial, I’m going to talk you through turning your Raspberry Pi into a NAS server. A […]

  2. 9th July 2013

    […] Tried this guide ? http://www.raspipress.com/2013/05…torage-server/ […]

  3. 24th July 2013

    […] ovom linku možete pogledati detaljno upustvo za […]

  4. 3rd October 2014
  5. 8th May 2015

    […] Turn a Raspberry Pi into a NAS (Network Attached Storage) Server […]

  6. 31st July 2016

    […] Turn a Raspberry Pi into a NAS (Network … – In this tutorial, I’m going to talk you through turning your Raspberry Pi into a NAS server. A NAS (Network Attached Storage) server allows you to hook … […]

Leave a Reply