Now we have seen how to setup a single external HDD let's take a look at creating a Network-attached storage (NAS) device. This is particularly handy if you have a number of machines on your home network, such as Macs or Windows PCs, and want to create a central storage device for files that they can all access.
Setting up the NAS builds up the work we completed in the first part of this chapter.
The technology we will use to achieve this is Samba. Samba is an open source application suite that provides both file and print sharing services. It re-implements the SMB/CIF protocol and was originally aimed at Windows users.
You can read more about this at the Samba website by visiting https://www.samba.org/samba/what_is_samba.html.
For now, all you need to know is that it will allow you to network your Raspberry Pi 2 and use it as a storage medium on your home network.
Let's start by grabbing the packages we need to set things up.
We can use apt-get
to grab the Samba packages and install them onto our device:
sudo apt-get install samba samba-common-bin
Once the installation process has completed we need to edit the configuration file. This can be found in the ./etc/samba
directory:
sudo vim /etc/samba/smb.conf
Find and edit the following line:
# security = user
It should be changed to the following line:
security = user
What we have done here is remove the comment activating this line in the configuration. If the file does not contain this line you can add it yourself.
We can now add our NAS Samba configuration as follows. Add it to the end of the file, below any other configuration settings:
[NAS] comment = NAS directory path = /ext4/ valid users = @users force group = users create mask = 0660 directory mask = 0771 read only = no
The first line [NAS]
is the name of the share. You can label this whatever you like.
Following this we have the comment
. This is a description text associated with the share. This can be a plain text string explaining what the configuration is for.
After this we include the path
. This is the path to our mounted HDD, for example,/ext4/
or /
.
Next we include the list of valid users permitted to access the share. Here we use @users
. The next line includes force group
. Here we specify the UNIX group name that will be assigned for all users who access the share.
The create mask
and directory mask
follow on the next two lines. This contains the permissions that all directories on the share are given by default including when they are created.
The final setting read only
is set to no
. This allows users to add files to the share.
First of all, we will restart the Samba server to pick up our configuration changes.
At the command line, run the following command:
sudo /etc/init.d/samba restart
Next, connect your user to Samba as follows:
sudo smbpasswd –a pi
You will be prompted to enter a password for the pi
user.
We can now test the network share is working from external devices. Follow the steps in the next section for the device you have. Instructions are included for Mac, Linux, and Windows:
To connect your Mac to the NAS follow these steps:
This completes the steps to access your Raspberry Pi via the Mac. You can now add and remove files to test everything is working.
Linux machines can also access the Samba share via the smbclient
application.
If your device does not already have this installed, you can add it via the following command:
sudo apt-get install samba-client
Once you have installed it, follow these steps:
smbclient –L 10.0.0.64
where the IP address is your Raspberry Pi's./usr/bin/smbclient \\\\10.0.0.64\NAS <passwd>
where the IP address is your Raspberry Pi and the <passwd>
is your password.The http://www.tldp.org/HOWTO/SMB-HOWTO-8.html website provides an in depth guide to using the Samba client on Linux.
This completes the testing of the Samba client in Linux. You should now be able to use the Raspberry Pi 2 for storing files.
Connecting to the Samba NAS from Windows is relatively straightforward. The following steps illustrate this:
Try adding and removing files to confirm everything is working.
We now have a working NAS device that we can use to store and share large files across our home network.