Saturday, 2 November 2013

Setting up a Raspberry Pi as a home server

A lot of people have spoken about the potential uses for a Raspberry Pi, however a lot of these are often very specific to be of use to anybody other than the project creator (using it as part of a home made weather balloon system for example).  When I purchased my Raspberry Pi it was for a specific purpose; the old PC that I used as a home server had given up the ghost and I needed a replacement.  The Raspberry Pi seemed like the ideal answer as it was small and used a lot less electricity (a big plus for a piece of equipment that is going to be on 24/7).

Before we start there are a few things you will need:

  • A Raspberry Pi
  • A computer with internet access 
  • Raspian linux distro
  • An SD Card (4GB minimum)
  • An SD Card reader/slot on your computer
  • External USB hard drive (this will need to be large if you plan to have video and music files on it, I recommend about 2TB minimum)
  • Micro USB charger (my old one from a previous Android phone did the job)
  • A wired USB keyboard
  • An ethernet cable
  • An HDMI cable
  • A monitor/TV with an HDMI connection

1. Firstly we are going to need to install Raspian onto the SD card, for full instructions on how to do this I recommend reading this how to guide. However I kept it simple and used the command line on my Linux system.

Firstly we need to extract the file out of the Zip archive.  Navigate to the place where the downloaded file is and enter the command:
unzip {thefilename}

As these change every time a new version is released the command will alter depending on the image that has been downloaded.

2. Now to get the image onto the SD card.  I needed to use the bs=1M option in the command as the install failed twice with bs=4M, it stated that it would take 'considerably' longer, but the whole process was still fairly quick:
dd bs=1M if={location of raspian.img file} of={location of SD card}

If your not sure what the location of the SD card is then make sure it is removed from the computer and in the command line type:
df -h

Then insert (and mount) the SD card and retype the command:
df -h

There will be an additional item on the list:

In the example above the location of the card is /dev/sdc, we ignore the number as this refers to the partition.

3. Once the process has been complete, unmount the SD card and insert it into the Raspberry Pi.  Connect your keyboard and external hard drive via the USB ports and then connect the Pi to your router and TV before finally giving it power.

4. When you first power up the Raspberry Pi you are asked for some initial settings:


Firstly we'll want to expand_rootfs, this means that we'll be able to use the entire SD card if we ever need to.

We need to know the password, so the next thing we'll do is change the password, to do this simply select change_pass and enter in your new password.

configure_keyboard, change_local and change_timezone allow the user to set the keyboard layout, the location and the local time zone (these are all UK by default).

As this is a server we won't have it connected to a monitor for the majority of
it's life so make sure that we do not start the graphical desktop on boot, boot_behaviour changes this setting.   Although this may look nicer, the Raspberry Pi has limited power and we want this to focus on sharing our files over a server, not looking pretty.

5. To allow us to share our files over a network we will install Samba:  
sudo apt-get install samba

6. To find UUID and the file system of the drive, enter:
sudo blkid

This will give an output similar to the one below:


We can ignore the line that has the TYPE="fat32" as this is the SD Card, take a note of the other line, you need the UUID and the Type, the labels aren't important at this point (depending on the formatting some drives may not have a label)

7.  We need to mount this drive automatically whenever the Raspberry Pi is switched on. Firstly, we'll need to create a location to mount the external hard drive, we can do this by using the command:
sudo mkdir /media/external

Now we need to edit the fstab to tell the Raspberry Pi to automount the external drive every time it is switched on
sudo nano /etc/fstab

Insert the following line at the bottom of the page:
UUID="{the UUID you noted down in step 6}"     /media/external    
{type noted in step 6}     auto,user,rw      0     0

The above should all be on one line, pressing the key to create a gap between each piece of information.

8. Now we need to set up which folders are going to be shared.  I'll assume that we are going to make the whole of the external drive available over the network
nano /etc/samba/smb.conf

at the bottom of the page add the following, note that you can call this anything you wish, just exchange the word external but you must include []
[external]
path = /media/external
available= yes
browsable = yes
public = yes
writable = yes

It's worth noting before we continue that I've set the option writable - yes, this means that you can add to or remove the contents of the external drive over the network.  If you are not comfortable having this option then simply change it to writable = no. Now save and exit (ctrl + X), make sure that you answer 'yes' to save your changes. 

9. We will now set the Raspberry Pi to log in automatically on startup, if the user Pi isn't logged in then all of things we've just set up will not work. Type:
sudo nano /etc/inittab

navigate down to the line:
1:2345:respawn:/sbin/getty --noclear 38400 tty1 

and add --autologin pi after getty to make the line:
1:2345:respawn:/sbin/getty --autologin pi --noclear 38400 tty1

save and exit (ctrl + X) make sure that you answer 'yes' to save it.

10. We're going to need one more piece of information before we finish.  I've had trouble in the past browsing Samba share folders using assigned names rather than IP addresses.  So to find the IP address of the Raspberry Pi type
ifconfig eth0

You'll be given a an output similar to what is shown below:

The part that is circled in orange (inet addr) in the above image is what you need to note down.  We'll need this to test the share drive later on.

11. We can try it out our system and auto-login by using the command
sudo reboot

Assuming that everything goes well, your system should be up and running.

Lets try this out on a different computer or device.

Load up your chosen file manager, most have location bar at the top (similar to the address bar on a browser).  Enter the following in the location bar: smb://{your Raspberry Pi's IP address from step 10}

You should now be able to access the files on the external hard drive connected to your Raspberry Pi.

Congratulations you've now set up your home server.

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice layout and page design here! Made reading the info more enjoyable. I heard about the RPi a little over a year ago, and thought "not for me, wait". I converted to using Linux (various flavours) 4-5 years ago, but still have Windows around when I need it.
    Now that RPi has caught up, I am finally going to get one, so I can do exactly what this is about; running a home server that is quite, low energy consumption, and dependable. Set, and forget!
    Thanks again for a brilliant article.

    ReplyDelete
  3. I'm using windows 8 and I can see the pi and the external folder in my network, but when I try to open it with the file explorer it says "Windows cannot access..." saying that I do not have permission to access it. Am I missing something? I'm pretty sure I followed your guide step by step.

    ReplyDelete
    Replies
    1. I actually managed somehow (mixing some suggestions from http://www.raspberrypi.org/phpBB3/viewtopic.php?t=56149) to have access from my computer, but since I'm not really sure what gave me access I have no idea as to how grant the access to the other two computers in my network with windows 7 and the same problem. I'll also set up a Lubuntu machine in the next few days and see if the same thing happens..

      Delete