Friday, 19 June 2015

The Useful Family Hub

The Useful Family Hub

Keir Thomas explains how to turn a Raspberry Pi or old PC into a useful storage and sharing device for the entire family's computers and handhelds

Most households feature a corner of the living room in which electronics are stashed. Alongside the TV you might see a Sky or Virgin box, and a games console or two, an internet router, and perhaps more complex add-ons such as a Chromecast.

In this feature we discuss adding another device that acts as a go-to helper for all the computers in your household. For example, it can act as an ad blocker for every computer, a DNS cache to speed up net access, and a network-attached server to store and share files. You can add in a personal cloud or AirPlay capabilities so you can beam your music live from your iPad, iPod Touch or iPad.


We use a Raspberry Pi throughout and just about any model will do. The chief advantage of a Pi is that it's small and consumes very little power - ideal for being left switched on all the time - but you could feasibly use an old PC or laptop. All the instructions assume you're using a Linux distro based on Debian (or Debian itself), but don't worry, we take you through everything step by step and do not get bogged down in technical discussion. Note that there's no need to leave a monitor, keyboard or mouse attached to the Pi. It's designed to run 'headless', meaning all that it needs are power and Ethernet cables. However, note that an old PC might refuse to (re)boot unless a keyboard is plugged in.

Setup


A Raspberry Pi model В running the standard Raspbian release is used below and we refer to it throughout as 'the hub'. We won't go into how Raspbian is installed - instructions are available at the download site (www.raspberrypi.org/downloads). Nor do we cover how to SSH into it for configuration purposes, although as usual we recommend PuTTY if using Windows, and you'll find instructions here: goo.gl/YmabYb

Overclocking or adjusting the memory split via raspi-config isn't strictly necessary but might be useful if you intend to run more than a couple of the services described below on the Pi.

If using an old PC or laptop, then the latest release of Debian (www.debian.org) will work just as well. Again, setup is pretty elementary, and there are guides online, such as the one provided by Debian itself (goo.gl/x92H0o).

Whatever system you opt for, you should update it before doing any of the steps below: sudo apt-get update; sudo apt-get dist-upgrade.

The hub will require a static IP address on your local network so other computers will always know where to find it. Before setting this up you'll need to know the DHCP network range used by your router. Below we assume 192.168.1.2-255, which is perhaps the most common. You also need to know the router's own IP address (referred to as the gateway in network terminology). You can usually discover these details using the router's admin configuration screen. We also assume the hub is connected via an Ethernet cable to the router.

SSH into the hub and type the following:

sudo nano/etc/network/interfaces

You'll see a line that reads iface eth0 inet dhcp. Replace dhcp with static, so the whole line now reads iface eth0 inet static.

Add the following new lines directly below this line (NOT at the bottom of the file; note that you can copy and paste this from goo.gl/NMdFzt if typing it from the page proves difficult):

address 192.168.1.123
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1

Again, we assume above a standard 192.168.1.* network range, with the router located at 192.168.1.1 - as it typically is with most models. (If you have a ВТ HomeHub, it's very likely the last line should read gateway 192.168.1.254).

Above we assign the hub the address 192.168.1.123, which is nice and memorable, and unlikely to cause a clash with addresses automatically assigned to other computers.

Tap Ctrl+X, tap y, and then hit Enter to save the file, and at the command line type sudo reboot.

Note that when you reconnect you will be warned the security key of the hub has changed. This is fine. Just click to continue. If connecting from a Linux or Mac computer, you might need to remove the older entry for the hub from your ~/.ssh/known_hosts file. Just open the file for editing, then delete the relevant line that begins with the IP address you formerly used to connect to the hub.

Ad Blocker


The Pi-Hole project (goo.gl/nqtYIK) explains how to turn a Raspberry Pi or any Debian-based distro into an effective ad blocker and domain name server (DNS) cache for the entire network. The benefits are that all computers your family use - including handhelds on which you might not be able to install ad-blocking software - will receive zero advertising. This increases the speed of web page loading and reduces bandwidth consumption. Additionally, there's no need to install an ad blocker on any computer on the network, which can help avoid browsers getting loaded down with too many plug-ins.

The second benefit is that, in order to block ads, the Pi uses a DNS cache. This is like having a DNS server on your own network, avoiding the need to use your ISP's. Rather than waiting 40-80ms for a DNS look-up, your browser will have to wait just 5ms or so. Again, this can lead to quicker page loading on websites.

The instructions below are adapted from the Pi-Hole project. Start by installing the necessary software on the hub - the following is a single line:

sudo apt-get -y install dnsutils dnsmasq

Following this we need to temporarily stop the new DNS service, then replace its configuration file - the following three commands do the trick:

sudo service dnsmasq stop
sudo rm -f /eto/dnsmasq.conf
sudo nano /etc/dnsmasq.conf

Type the following into the new file (or copy and paste from the text file found at goo.gl/AwvU0D):

domain-needed
interface=eth0
min-port=4096
cache-size= 10000
log-queries
bogus-priv
no-resolv
server= 127.0.0.1
server=8.8.8.8
server=8.8.4.4

Tap Ctrl+X, tap Y, and then hit Enter to save the new file. Note that the listing above uses the Google Public DNS servers (8.8.8.8 and 8.8.4.4; see the last two lines), which are free of charge for worldwide use. You can substitute these with any DNS address, including that of your ISP or OpenDNS.

Next, you need to add a script to download the ad blocking definitions. Type the following:

sudo nano /usr/local/bin/gravity.sh

In the new file, type the following five lines (or copy and paste the data from goo.gl/Nu3Fm9). Note that you should change the piholelP line's address to that of the static IP address you set earlier if you didn't opt for 192.168.1.123:

#!/bin/bash
piholelP=' 192.168.1.123'
eventHorizion='/etc/dnsmasq.d/adList.conf'
curl -s -d mimetype=plaintext -d hostformat=unixhosts http://pgl.yoyo.org/adservers/serverlist.php? I sort I sed '/^$/d' I awk -v 'IP=$piholelP' '{sub(^r$/,"); print 'address=/'$0'/'IP}' > $eventHorizion
service dnsmasq restart

Tap Ctrl+X, tap Y, and then hit Enter to save the new file. Then type the following two commands:

sudo chmod 755 /usr/local/bin/gravity.sh
sudo /usr/local/bin/gravity.sh

We now need to set the ad-blocking list to periodically update. Type the following command:

sudo crontab -e

And in the file that appears type the following line at the very bottom:

@weekly /usr/local/bin/gravity.sh

Tap Ctrl+X, tap Y, and then hit Enter to save the file.

Setup is finished and now it's just a matter of setting all computers on the network to the hub as a DNS server. Needless to say, explaining how to manually specify a DNS server is outside the remit of this feature, but there are many, many guides online for all major operating systems and handhelds.

The changes will take effect immediately, and there's no need to restart the hub. You can test the ad-blocking capabilities of your computer(s) at ads-blocker.com/testing. However, note that most operating systems browsers cache DNS addresses so the blocking might not start immediately. Even quitting and reopening the browser might not clear the DNS cache, so a reboot/restart of the PC might be necessary.

File Server Or Backup


By attaching a USB hard disk or memory stick to the hub, you can add in an elementary but nonetheless useful network attached storage (NAS) service that all computers on the network can access. Considering USB disk adapters can be found on eBay for little money, you might choose to attach an old hard disk that you have lying around.

In the steps below, we assume the disk or stick you attach is formatted as FAT32/64. This is the case for most USB disks bought off the shelf, because this provides the maximum all round compatibility. See the note later if you can't avoid using an NTFS-formatted disk.

To start we need to identify the USB disk/memory stick once it's attached. SSH into the hub and type the following:

sudo fdisk -I

We're interested in what appears at the bottom of the list. If you're using a Pi, you'll probably see a line that begins /dev/sda1 and which will end with W95 FAT32. Make a note of the part that begins /dev.

Type the following commands, which will open a file for editing:

sudo nano /etc/fstab

Then type the following as a new line at the end (or paste it in from goo.gl/KH7IE2), substituting /dev/sda1 if you discovered different details using the fdisk -I command earlier:

/dev/sda1 /media auto user,rw,umask=111,dmask=000,noatime 0 0

Tap Ctrl+X, tap Y and hit Enter to save the file. Then reboot the hub using the following command:

sudo reboot

Once the hub has rebooted and you've reconnected via SSH, it's necessary to add in the network sharing component. Type the following two commands to install the software and open its configuration file for editing:

sudo apt-get -y install samba samba-common-bin
sudo nano /etc/samba/smb.conf

Tap Ctrl+W to open the search field, and type security = and then hit Enter. This will take you to a section of the file that discusses authentication. Look for the single line beneath it that reads as follows:

# security = user

Remove the hash and spaces from the front of the line, so it now reads:

security = user

Tap Ctrl+W and then Ctrl+V. This will take you to the bottom of the file where you should type the following new lines (or copy and paste from goo.gl/CR18v8):

[FamilyHub]
comment = Family Hub
path = /media
valid users = @users
force group = users
create mask = 0660
directory mask = 0771
read only = no

Tap Ctrl+X, tap Y and then Enter to save the file.

Then type the following two commands, entering your login password when prompted twice to enter a new password (that is, type raspberry in the case of a default Raspbian setup). Note that if you're not using a Pi for the hub, then you'll need to change pi in the second command to the username you chose during installation:

sudo /etc/init.d/samba restart
sudo smbpasswd -a pi

Your new hub-based NAS should now be accessible from computers on the network in the usual way, just like a shared folder on any Windows computer. The FamilyHub folder is where you should store your data and/or create new folders for your users.

If you have trouble connecting from Windows, click Start and then type two backslashes followed by the IP address of the hub you set earlier and then \FamilyHub. For example, if you chose 192.168.1.123 you would type the following:

\\192.168.1.123\FamilyHub

DLNA sharing


An optional extra step after configuring a NAS is to enable DLNA support on the hub so that smart TVs and/or games consoles can connect to the hub in addition to computers. Type the following commands to install the software and create the necessary new folders:

sudo apt-get -y install minidlna
mkdir /media/music /media/photos /media/videos

Once it's completed, open the config file for editing:

sudo nano/etc/minidlna.conf

Tap Ctrl+W and then Ctrl+V to jump to the bottom of the file, and type the following (or copy and paste from goo.gl/OCHQtJ):

media_dir=A,/media/music
media_dir=P,/media/photos
media_dir=V,/media/videos

Tap Ctrl+X, tap Y and then hit Enter to save the file. Then type the following to restart the DLNA service, after which it will be accessible from TVs and consoles:

sudo service minidlna restart

Users on other PCs should place their files in the respective music, photos or videos folders if they want to utilise the hub via DLNA.

If the disk you attach to the hub is formatted with NTFS, before any of the steps above you'll need to install NTFS support with the following command:

sudo apt-get -y install ntfs-3g

Following this, the remaining steps are identical.

For the ultimate low-power device, you might also choose to implement hard disk power saving for the external drive. Again, this is outside the scope of this feature, but googling the hdparm command should reveal instructions.

Personal Cloud Storage


Dropbox is a standard feature on many computers nowadays, but privacy concerns haven't been addressed satisfactorily. As an alternative you can implement your own personal cloud on the hub that can then be accessed from all your computers.

For access outside the home (that is, the local area network) -when you're in a coffee shop, for example or work environment - you should look into configuring a dynamic DNS address for your broadband router in addition the steps below, and also forwarding the Secure Shell (SSH) port to the hub via the router's configuration screen. You'll find many guides online via Google if your router is compatible, although in the instalment of this feature next week we'll look at setting up a dynamic DNS updating service using the hub alone.

We're going to use SparkleShare (sparkleshare.org) to implement a personal cloud, which is both free of charge and open source. Start by typing the following to install Git, which is the backend software it requires (note that you might find this is already installed, in which case just move to the steps afterwards):

sudo apt-get -y install git

Then type the following four commands, which will install the SparkleShare software (if you want to copy and paste you'll find the three commands at goo.gl/EegBPS):

sudo curl https://raw.githubusercontent.com/hbons/Dazzle/master/dazzle.sh --output /usr/bin/dazzle && chmod +x /usr/bin/dazzle
sudo chmod +x /usr/bin/dazzle
sudo dazzle setup
sudo dazzle create cloud_folder

Now all you have to do is install the SparkleShare client app on each computer you want to access the cloud folder. You'll find it at sparkleshare.org.

During setup on the computers on your network, you'll be provided with a Client ID. For each computer you'll need to copy this to the clipboard, SSH into the hub, and then type sudo dazzle link, then hit Enter, before pasting in the Client ID when prompted.

Click Finish on the PC client software, then right-click the new SparkleShare icon that you'll find in the System Tray and select SparkleShare > Add Hosted Project.

In the dialogue box that appears, type the hub's address into the address field, and /home/storage/cloud_folder into the Remote Path field. Then click Add. (If you set up a dynamic DNS earlier, then you should enter the dynamic DNS address into the Address field instead, rather than the hub's IP address, even if you're on the local network when doing so.)

Setup is now complete, and your personal cloud folder (/Users/ USERNAME/SparkeShare/cloud folder) will sync automatically with all the other computers.

AirPlay


If you have an iPhone, iPod Touch or iPad, you can add AirPlay audio capabilities to the hub so you can play music through a hi-fi attached to the audio port of the Pi/PC, via a 3.5mm to RCA audio cable (or to headphones plugged into the Pi/PC). Doing so is simple courtesy of the Shairplay software, which again is free and open source. Type the following after SSHing into the hub:

sudo apt-get -y install shairplay

You'll need to start Shairplay each time the hub boots or reboots, which means adding a runtime script. Start by typing the following to create the file:

sudo nano/etc/init.d/shairplay

Visit goo.gl/9kfTIo in your web browser, and copy and paste all of what you see into the file you're editing on the hub. Tap Ctrl+X, tap Y and hit Enter to save the file.

Type the following three commands to make the runtime script executable, and also reboot the hub:

sudo chmod +x /etc/init.d/shairplay
sudo update-rc.d shairplay defaults
sudo reboot

Subsequently playing music from an iOS device is a matter of bringing up the Control Center (drag up from the bottom of the screen), tapping the AirPlay icon, and then selecting the FamilyHub icon. Note that you'll need to be on the same network as the hub for AirPlay to work.

To control the output volume of the hub if it proves too quiet, SSH into it and type alsamixer. Then use the up/down cursor keys to adjust the PCM volume.