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.