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 ‘\\server\share’ 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.

45 thoughts on “Turn a Raspberry Pi into a NAS (Network Attached Storage) Server

  1. 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

    1. 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,

      1. 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

        1. 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

          1. 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…

  2. 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!

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

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

  5. 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.

  6. 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

  7. 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

  8. 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

  9. 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?

  10. 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.

  11. 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!

    1. Is there an update on this issue? I havent been able to get this to work properly even after trying some of these commented solutions.

  12. 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

    1. 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 !

  13. 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.

  14. 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 :(

  15. 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 ?

    1. 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….

  16. 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?

  17. 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

  18. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>