Before I go to far with this blog. There are steps where changes will be made to system files. These could render your system unusable if made incorrectly. Any changes to system files are made at your own risk.
One of the devices that has proved very useful for us is a Network Attached Storage device or NAS. These come in all sorts of shapes and sizes from the likes of Amazon. You can even build your own. That was what I did as detailed in my “A Trip into the Dark Side” blog. This went through a number of changes and evolved over the years before it was eventually replaced by a Raspberry Pi set up.
The adding of a Samba file server to a network can be quick and easy. In this blog I will detail how to do this. The file server you will end up with well be very basic but usable. It is assumed that you have a Raspbian image all ready set up on a SDCard. This is set up with a static IPAddress and is up to date. If not then have a look at my blog entry detailing how to do just that. You will also need the following items for this.
A Raspberry Pi with a suitable power supply.
A mains powered USB hub.
External hard drive/drives or a selection of USB flash drives.
Power up your Raspberry Pi. There are two ways to set this up. Either by attaching a keyboard and TV to the Raspberry Pi directly or remotely. We well use remotely to set it up here.
First we log into the Raspberry Pi by using a telnet program. I use Putty for this but there are others available. Log in as the Pi user with the details below.
User – pi
Password – raspberry
Once you have logged in make sure the system is up to date by using the following commands:
sudo apt-get update
sudo apt-get upgrade
Right now to install the software needed by the server. To do that install the following:
sudo apt-get install samba samba-common-bin libpam-smbpass
sudo apt-get install ntfs-3g
We install samba for network file sharing and ntfs-3g for NTFS drive access. Restart the server with:
We now need to set up the drives we want to share. As we need to be able to identify the drives the best way to do that is to connect them one at a time. Power off the Raspberry Pi.
sudo shutdown -h now
Attached it to the USB hub. Connect up your first drive and power it all back on. Once it has restarted log in again using the pi user as before. We now need to add the drives to the file system. This is quite straight forward but requires a couple of steps. These are identify the drive, set a mount point and mount the drive. I always add drives to the /mnt folder but you can mount them any where you want really. For example. I have a USB flash drive set up as the user home folder on our mail server due to the limitations of a small SDCard. Issue the following command:
This will list the drives that are attached to your server. It details the device number allocated to the drive, the UUID for each drive found and the type of drive. Now that you have identified the drive you need to generate a mount point for it.
sudo mkdir /mnt/Drive1
In the above example change the Drive1 part to what ever you want to call it. Now we need to mount the drive to the new folder. To do that is easy. Issue the command as detailed below. Change the sdc1 part to what ever your drive was allocated as.
sudo mount /dev/sdc1 /mnt/Drive1
Your drive is now mounted in the location you made earlier on. The sdc1 part means drive sdc partition 1. Each drive can have a number of partitions on it.
There is one major issue with this method though. Power off the system and the mounted drive well not be available when you power it up again unless you reissue the above command. To make the mount permanent you need to add the drive to the fstab file. This is a system file and any changes are made at your own risk. Issue the following command:
sudo nano /etc/fstab
This well open the fstab file in the nano editor ready for editing. There are two methods of mounting a drive at start up. The first is by the normal /dev/sd? route. This is fine if the drive stays at the same allocated point. What can happen is you add another drive or drives and this allocation changes. The safest method is to use the UUID of the drive. This is for example UUID=f5dc03a1-a577-43ce-97c4-684e799876e0. It is different for every drive as it is generated when the drive is first formatted.
Please note – This is not the case if you have bought two or more external hard drives of the same size and make from the same manufacturer. These are generally not formatted but imaged so well end up with the same UUID number. Refer to my blog post on how to change the UUID of a partition.
To find out what your UUID number is open a second telnet window and type the following:
The output you get well differ from mine but for example it could be as below:
/dev/sda1: UUID=”0b12efed-d6a8-4141-bd59-ac8d84b6d834″ TYPE=”ext4″
In the list of information that appears your drive well be listed. Make a note of the UUID and the TYPE. Now in the original window add to the the end of the fstab file for example the following:
UUID=f5dc03a1-a577-43ce-97c4-684e799876e0 /mnt/Drive1 ext4 defaults,errors=remount-ro 0 0
Change the details given for yours. This is all on one line and breaks down as follows.
Drive format (TYPE).
Method used to mount it.
How to deal with drive errors.
fsck Priority bit.
Check and double check that the information you entered is correct. Once you are happy save the file out and restart the Raspberry Pi. For more information on the fstab file look here.
Once it has rebooted enter the following command:
This well list all the drives etc that are mounted in the system. Scan down to the last entry where your new drive should be listed. To make sure your drive is accessible enter the following command:
Or what ever you called your mount point. This should get you a listing of the drives contents. So far so good. You have now added a drive to the file system but it is not shared to the wider network. That is the next step and again is simple to do.
We need to edit a system file again so enter the following command:
sudo nano /etc/samba/smb.conf
This well open the smb.conf file ready for editing. Now you have two options here. Delete all the original lines or comment them out with #. There is a lot of it so it may take a little while to add # to the start of every line. I normally just delete the contents by the use of ctrl+k but it is up to you. Either way the lines you need to add are as follows:
workgroup = workgroup
server string = %h server
wins support = no
dns proxy = no
# security = share
security = user
map to guest = Bad User
encrypt passwords = yes
panic action = /usr/share/samba/panic-action %d
log file = /var/log/samba.log
preferred master = no
Comment = Shared
path = /mnt/Drive1
writeable = yes
browseable = yes
read only = no
guest ok = yes
force user = root
This is really a minimum set up and there are lots of other options available that I will not go into here. For more information just use Google. All that is left now is to restart samba. Use the following command for that:
sudo /etc/init.d/samba restart
Now on a computer that is attached to your network test out the new shared folder. Try writing a file to it and then deleting it. There you go you now have a Network Attached Storage device. To add more drives just follow the steps above changing the settings as required.