Thursday, 30 July 2015

Build a Raspberry Pi 2 Server

Build a Raspberry Pi 2 Server

Traditionally, servers are great b ig devices that consume vast amounts of power and generate a considerable amount of heat. That’s why data centres are huge, air-conditioned rooms full of racks housing multiple units. But for home or small business use, they don’t have to be that way, and with the Raspberry Pi, we have a small Linux computer that consumes very little power and generates only a small amount of heat.

In this tutorial, we’ll be using a Raspberry Pi 2 to build a small, powerful and energy-efficient server that will be a handy remote backup location and will serve files over SSH, a secure protocol that provides a level of encryption to your connection, giving you a more secure method of transporting data over the internet. Other than the hardware listed to the left, you’ll also need a good-quality Raspberry Pi power supply, an Ethernet connection to a router and an externally powered USB hard drive. We’re using Linux Mint on our host PC for this.


We’ll start by creating an SD card containing the operating system. Technically, this is a bundled collection of software based on the Linux kernel that’s called a distribution, or ‘distro’ for short. Our distro of choice is Raspbian, which is the default distro for the Raspberry Pi.

There are two ways of installing Raspbian to your SD card. First, the NOOBS method requires you to download the NOOBS ZIP archive and then extract the contents to a blank, FAT-formatted card. Once this card is inserted into your Pi, a menu will ask you which distro to install. This is where you should choose Raspbian, and then grab a cup of tea while it installs itself. This is the easiest way of installing Raspbian, but for a smaller install footprint, the best method is to download the Raspbian image and then extract it from the archive, copying that to a blank SD card and using the command ‘dd’.

The dd command is powerful but dangerous. If used incorrectly, it can destroy all of the data on a drive, so proceed with care. Before employing dd, we need to identify the SD card inserted into the computer. Open an LXTerminal (click the desktop icon) and type the following:

sudo fdisk -l


You’ll now be able to see a list of the volumes and disks that fdisk can find. Typically, anything starting with ‘/dev/sdX’ is an internal hard drive of your computer. Ignore those entries and look for ‘/dev/mmcblk0XX’, where ‘XX’ can be ‘p1’ or ‘p2’. You can ignore p1/p2, as they’re partitions on the card – we’ll be writing data to the whole card, not just a partition. If you can see those, your SD card has been identified. Make a note of the location.

With the SD card found, use the ‘dd’ command to copy the Raspbian image to the blank card. In the LXTerminal, navigate to the location where you extracted the Raspbian image from the ZIP archive. This is typically Downloads.

cd ~/Downloads

Now issue the ‘dd’ command. Check everything before pressing [Enter], as once you start dd, it won’t let you stop the process. In the LXTerminal, issue the following command, replacing ‘raspbian.img’ and ‘/dev/mmcblk0’ to match what your system reported via the ‘fdisk -l’ command.

sudo dd if=./raspbian.img of=/dev/mmcblk0 bs=4M

You’ll be prompted for your password and then dd will get to work, but you’ll see no output while it works – it will do its job and report after five to 10 minutes. Time for the next cup of tea.


With your SD card ready, let’s assemble your Raspberry Pi setup. You’ll need to plug your Pi into a monitor and have a keyboard and mouse connected. Assemble your kit, insert the SD card and power up your Pi. The boot process will take less than 15 seconds. You’ll then be transferred to the raspi-config screen.

Your first task is to expand the filesystem on the SD card so that you get the maximum amount of space. To do this, navigate to ‘option 1’ and press [Enter] to start an automated process. With that complete, navigate to ‘option 8 – Advanced options’. Now navigate to ‘A3, Memory Split’ and press [Enter]. Change the value to ‘16’ and press [Enter]. This gives you 16MB of RAM for the GPU. As you’ll be using this headless, you won’t need a lot of video RAM. You’ll be returned to the main menu, but you need to navigate back to the ‘Advanced menu – option 8’. Once there, navigate to ‘A4, SSH’, move the cursor to ‘Enable’ and press [Enter]. That’s our configuration done. Return to the main menu, navigate to ‘Finish’ and press [Enter]. Reboot your Raspberry Pi to finish the configuration.


With the Pi rebooted, go ahead and log in. The default username is ‘pi’ and the password is ‘raspberry’. Next, make sure that your software is up to date. At the LXTerminal, type in the following and press [Enter].

sudo apt-get update

This will compare your software repository lists against what’s on the Raspbian servers and update it where it needs to. If there’s a lot to download, Raspbian will ask before it does so. Depending on your internet speed, this may take a while, but once it’s done, control will be returned to you.


With the updates complete, now create a fixed IP address for the Pi server. To do this, you’ll need to edit the ‘/etc/network/interfaces’ file. You’ll need to know the Pi’s current IP address, which you can find by running:


This will spit out a lot of text. As you’re using the Ethernet connection, look for the ‘eth0’ line, typically at the top of the text. In this section, look for the ‘inet addr:’. Ours looks like this:

eth0 Link encap:Ethernet HWaddr b8:27:eb:b8:d2:c8
inet addr: Bcast:

We also need to run ‘netstat’ to get more details:

netstat -nr

And pick out two numbers from here for the gateway and the destination. Make a note of both. So, with this information noted, edit the ‘/etc/network/interfaces’ config file.

sudo nano /etc/network/interfaces

The default configuration will show that interface eth0, Ethernet, is using DHCP (Dynamic Host Configuration Protocol). In other words, it will get an IP address each time it connects to the router:

iface eth0 inet dhcp

In order to keep track of where our Pi server is on the network, we’ll fix its IP to what we found in ‘ifconfig’, which in our case was, but yours will differ. We’ll make changes only to the ‘iface eth0’ section; the other sections must be left as they are.

Our new section looks like this, yours will reflect the setup of your network:

iface eth0 inet static
#The IP address that we wish to use
#This is used to divide IP addresses into subnets, this is the standard and will work for you
#This is the IP address structure for our network, yours might be
#We found this via ifconfig earlier it was labelled BCAST
#This is the gateway address that we found in netstat -nr

When you’re ready, save the changes by pressing [CTRL]+[O], then exit using [CTRL]+[X].


Next, reboot your Raspberry Pi by typing:

sudo reboot

Your Raspberry Pi will reboot and return you to the login prompt. Log back in and type:


Check that your IP address is now static; if you want to check that you have internet access, type the following:


If the pings return properly, you’ve configured the network settings correctly. At this stage, you can carry on using the keyboard and monitor or you can go headless and connect to your Pi server over the network. We chose to log in via SSH from our Linux Mint laptop. If you’re going to do the same, you’ll need to open a terminal on your computer and type:


Remember to replace the IP address with the static address that we used previously. You’ll need to provide your password before you can log in, but once successful, any command issued will go through on the Raspberry Pi.


With your system built, you now need to configure it to act as a server, and the first task is to enable Raspbian to read and write our USB 2.0 external hard drive. If a drive is formatted as a typical Linux filesystem – ext3, ext4 or btrfs – you can read the drive with no configuration necessary. But because this drive is formatted NTFS – a Windows filesystem – you need to install a tool to enable read and write access to the drive, and this is called NTFS-3G. To install, type the following into the LXTerminal:

sudo apt-get install ntfs-3g

It will take a few seconds to install, and when complete, control will be returned to you.


Administrating a server is traditionally undertaken via the terminal, in our case LXTerminal. In fact, we’ve done quite a lot of this already. However, to make administrating a server a little easier, there’s a great tool called Webmin. This is a web interface for common administration tasks, such as user management and updating software.

To install Webmin on your Pi server, you’ll need to use the APT package manager to download Webmin and its dependencies.

sudo apt-get install webmin

With Webmin installed, open a new browser window on your computer and navigate to the IP address of your Pi and use the port number ‘10000’. Our IP address looked like this: ‘’.

You may receive a warning that the certificate for the website isn’t to be trusted. If so, there’s no need to worry about it. Just click ‘Advanced Options’ and proceed onwards. You’ll next see a login screen – enter the username and password that you’ve used to log in to your Pi previously. Once logged in, you’ll see the main menu split into eight sections. For this project, you need to refer to the System section, which contains controls for managing hard disks and for managing users and groups.


The next task is to mount the external USB hard drive on boot, so you’ll need the USB drive plugged in to the USB port of the Raspberry Pi. Before you manage the drive with Webmin, you’ll need to return to the LXTerminal and create a directory that will act as a share point. In the terminal, navigate to ‘/media/’ and create a new directory called ‘drive’, as follows:

cd /media
sudo mkdir drive

With the directory created, you now have an issue: Only users with sudo access, or root, can use the directory. You need to change the permissions so that users can read and write to the hard disk. To do this in the LXTerminal, type the following:

sudo chmod 770 ./drive

That’s it for now in the terminal, so let’s return to Webmin. To manage USB drives with Webmin, click the ‘System’ drop-down menu on the left of the screen, then click ‘Disk’ and ‘Network Filesystems’. After a few seconds, the screen will change and a list of all the mounted filesystems will be displayed.

Our drive will not be listed, so we need to create a new mount using ‘Add Mount’; but before you click on the button, look to the right and you’ll see a drop-down labelled ‘Apple Filesystem (HFS)’. Click the drop-down and select ‘Windows NT Filesystem NTFS’ if your drive is formatted using NTFS. If not, select the format of your drive accordingly. Once selected, click the ‘Add Mount’ button to open a new menu.

In the new menu, start by clicking ‘Mounted As’ and its ‘...’ button to the right. This will open a dialog box where you can navigate to the ‘/media/drive’ directory we created earlier. For the rest of the configuration, just follow this simple checklist:

• Save Mount? Save and Mount at Boot.
• Mount Now? Mount.
• Windows NT Filesystem: Disk. Look for your device in the dropdown menu.
• Mount Options.
• Read-only? No
• Allow users to mount this filesystem? Yes
• Disallow execution of binaries? If mountable by users
• Avoid updating last access time? No
• Buffer writes to filesystem? Yes
• Disallow device files? If mountable by users
• Disallow setuid programs? If mountable by users
• Wait until network interfaces are up? No
• User files are owned by: Leave blank
• Group files are owned by: Leave blank

When you’re ready, click ‘Save’ to write these configuration settings and mount the drive. The drive will now be available for you to connect to over the network. If you’re using Ubuntu or Linux Mint, you can use the file manager to connect to the server, but you can also use Gigolo. (Its tagline is: “It mounts what it is told to.” Classy.) This is a graphical front-end for the virtual filesystem GIO/GVFs, which mounts remote filesystems and opens them in your file manager. This will be available via your software repositories.


For the final part of this project, we’ll use our Raspberry Pi server as a remote backup device, with a great piece of open-source software called Déjà Dup ( This is actually a graphical front-end for the flexible, speedy and scriptable rysnc, which has grown into a standard Linux utility since its first release in 1996. It makes backing up really easy. To start with, you’ll need to install Déjà Dup on your computer. You don’t need to install anything on your Pi server. On your computer, open a terminal and type:

sudo apt-get install deja-dup

Once installed, open Déjà Dup and you’ll be presented with the Overview screen. You’ll need to instruct Déjà Dup which folders you wish to back up – that’s in the ‘Folders To Save’ option. Once you’ve done that, move to the ‘Storage Location’ menu and fill in the details for your Pi server, including the location to store your backup. We used ‘/media/drive/Documents’, which was an existing folder on the drive. With that complete, you’re ready to navigate back to the Overview and click ‘Back Up Now’.

Your first backup will take longer than subsequent backups, due to the first one being a full backup of the directories you’ve selected, while future backups will only cover changes to those files. To restore from a backup, go to the ‘Overview’ menu and click ‘Restore’, then follow the wizard.

So there we have i t. We’ve built a server that uses very little power, but centralises our files and provides a remote backup solution for our digital lives – all thanks to the Raspberry Pi and a little Linux know-how.

Create A Network Printer

We tend to have multiple computers in our homes these days, but when we want to print, we only have one printer. So, what if we could create a central network printer using our Raspberry Pi server?

Well, we can using Common Unix Printing System, or CUPS, a printing service created by Apple and used on OS X and Unix systems around the world. We typically run a version of CUPS on our computer, but we can also instruct our computer to print to a network printer. To install CUPS, you’ll need to SSH into your Pi server and type the following into the LXTerminal:

sudo apt-get install cups

You’ll then need to complete a few configuration changes. We found a really good guide for this on How-To Geek ( that shows how to complete this task on your Pi. To administer a remote CUPS printer, all you need to do is open a browser on your computer and type in the IP address for your Pi server and append :631 to it:

You can then add new printers, check the status of print jobs and cancel any erroneous prints. You can even set up an extreme remote print job by using port forwarding on your router to forward print jobs from outside your network to the CUPS printer.

Accessing Your Pi Server

In this project, we’ve created a file server inside our home network that’s not accessible to anyone outside. If you’d like to enable your Pi server to be accessed by external users, then there are a number of things you’ll need to do.

Start by changing the default password for the Pi user you want to give access to. You can do this using the ‘passwd’ command in LXTerminal. Next, create a new user with no sudo or root privileges. It’s easiest to do this via the ‘System > Users and Groups’ menu.

Now, set up port forwarding on your router so that requests to your external IP address are forwarded to the Raspberry Pi server. This is a little different for every router, so please consult your router manual.

It’s likely that your ISP doesn’t provide a static external IP address. If that’s the case, head to and install noipclient, then create an account on its website ( The client software will update your IP address to a URL that you create on the website. This will enable you to easily connect to your server without having to use an IP address.

Finally, it’s really important to set up a firewall that will protect your server from external interference. The easiest way to do this is install fwbuilder and VNC on the server, as fwbuilder is a GUI application to set up the firewall rules.