NASpberry Pi

megamark16

Created by: megamark16

Published Mar. 18, 2013

It's possible to set up a Raspberry Pi to run as a lightweight, energy efficient home media and file server, running the following services:

  • MiniDLNA - A uPnP compliant media server for streaming videos, music, and pictures across your home network to any uPnP compliant client.  Here is a list of uPnP clients.
  • SambaA service that lets you serve files to Microsoft Windows clients, and to any other operating system that supports this protocol (which is most of them).
  • OpenSSH - Allows you to securely connect to and from your device across the internet.  This service should come pre-installed on the Raspbian distrobution.
  • Transmission-daemon - A torrenting service with a we interface, allowing you to manage your torrenting from any computer on your network through a web browser.
  • OpenVPN Client - Allows you to anonymize your torrenting traffic by tunneling through a secure VPN connection.

Related categories: Tutorial

Step 1: MiniDLNA

MiniDLNA is a fully complient uPnP (Universal Plug and Play) digital media server, which allows you to stream video and audio, and to access pictures, from your NASpberry Pi over your network from any uPnP complient renderer. Here is a list of uPnP clients.

Installing MiniDLNA:

sudo apt-get update
sudo apt-get install minidlna

Open /etc/minidlna.conf in your favorite text editor, as superuser:

sudo vi /etc/minidlna.conf

Tell MiniDLNA where to look for your media:

media_dir=/home/pi/media

Set the name for our media server that will show up on clients:

friendly_name=[Your Name Here] Media Device

Download a video to make sure it works:

mkdir /home/pi/media
cd /home/pi/media
wget http://archive.org/download/Debian6.0.4Lxde/Debian6.0.4LXDEPresentation.mp4
wget http://archive.org/download/Free_20s_Jazz_Collection/2to2_64kb.mp3

Restart the MiniDLNA service:

sudo service minidlna restart

Now you should be able to connect to your server from a uPNP client on the same network as your NASpberry Pi, and you should be able to stream the test video and music.

Step 2: Samba

Samba is an open source network protocol that lets you serve files to Microsoft Windows clients, and to any other operating system that supports this protocol (which is most of them).

Installing Samba Server:

sudo apt-get update
sudo apt-get install samba samba-common-bin

Create a folder for your network shares, create a public and private folder, and put some test files in them so we'll know if everything is working correctly:

cd ~/
mkdir shares
mkdir shares/public
touch shares/public/Public.txt
mkdir shares/private
touch shares/private/Private.txt

Open /etc/samba/smb.conf in your favorite text editor, as superuser:

sudo vi /etc/samba/smb.conf

Define which folders you want to share and what kind of access they will have:

[public]
    comment = Public Files
    path = /home/pi/shares/public
    browseable = yes
    writeable = yes
    guest ok = yes
    read only = no

[private]
    comment = Private Files
    path = /home/pi/shares/private
    browseable = yes
    writeable = yes
    guest ok = no
    read only = no

Restart the Samba service:

sudo service samba restart

Create a Samba user/password for accessing the private share:

sudo smbpasswd -a pi

You should be prompted to enter a new password, this is the password you will use to log into the private share from another computer on your network:

pi@raspberrypi ~ $ sudo smbpasswd -a pi
New SMB password:
Retype new SMB password:
Added user pi.

In order to be able to make changes to the public share, you need to grant write access to the /home/pi/shares/public folder:

chmod -R 777 /home/pi/shares/public

Now you should be able to connect to your server from any client computer on the same network as your NASpberry Pi and access files in the public share. The private share will require your username and password to access.


Here is a more in-depth guide to setting up a Samba server on a Raspberry Pi and configuring clients to connect to it.

Step 3: Transmission

The transmission-daemon is a full featured torrent client that runs all the time on your NASpberry Pi, and which provides a web-based front end for managing your torrents. This means that you can manage your torrenting from any computer on your network, through a web browser.

Installing the Transmission Daemon:

sudo apt-get update
sudo apt-get install transmission-daemon

Create a folder to download your torrents to and grant global write permissions so that the transmission-daemon process has access to write to it:

cd ~/
mkdir torrents
chmod -R 777 torrents

Stop the transmission-daemon:

sudo service transmission-daemon stop

Open /etc/transmission-daemon/settings.json in your favorite text editor, as superuser:

sudo vi /etc/transmission-daemon/settings.json

Tell transmission-daemon where to download your torrents to and open up access to the web interface from anywhere on your network by changing these settings:

{ 
    ...
    "download-dir": "/home/pi/torrents",
    ...
    "rpc-whitelist-enabled": false,
    ...
}

Restart the transmission-daemon service:

sudo service transmission-daemon start

Point your browser to http://localhost:9091, and when prompted to log in, use "transmission" for both the username and the password. You can replace "localhost" with the ip address of your NASpberry Pi if you're trying to access it from another machine on your network

Step 4: OpenVPN

There are two main types of VPN connections supported by linux, PPTP and OpenVPN. For simplicity we're going to focus on OpenVPN. This tutorial assumes that you have a VPN service that provided you with a config file, and either a username and password, a CA certificate, or both.

I personally use privateinternetaccess.com ($6.95/month), but there are some free ones available (vpnbook.com for example) that you can use for this tutorial if you don't want to sign up for a paid VPN right away.

Install OpenVPN:

sudo apt-get update
sudo apt-get install openvpn

Create a script to kick off your vpn connection and make it executable:

cd ~/
touch vpn.sh
chmod +x vpn.sh

Now edit your vpn.sh script to look like this:

#!/bin/sh
sudo openvpn --config /path/to/your/openvpn/config/file --script-security 2

Now you should be able to execute this script to connect to your vpn:

./vpn.sh

 

It will most likely prompt you for your username and password, and then you will be connected to the VPN and all of your traffic should tunnel through there.

How do I know I'm connected to the VPN? - An easy way to check that you are connected to your VPN is to point your browser athttps://duckduckgo.com/?q=ip before and after you connect to your VPN. This pag will show you what your current ip address is, and it should be different when you are connected to the VPN than it is when you are not.

You can also check your ip address from the command line using this command:

curl ifconfig.me/ip
How do I keep the VPN connected when I log out? - You can use a program called GNU Screen to keep programs running after you close out of terminal or disconnect from an SSH connection to your server.  You can install GNU Screen by running:
 
sudo apt-get update
sudo apt-get install screen
And just type "screen" to start a screen session.  From within a screen session you can run your vpn.sh script, and then press "Ctrl+a d" (press Ctrl+a and then press the d key) to disconnect from your screen session while leaving it running in the background.  To reconnect to your screen session later just run:
screen -r
 

Step 5: Transmission Over OpenVPN

In general, all of your network traffic should use your VPN connection, if it is active, so by default Transmission should tunnel all of your torrent traffic over the VPN. That's fine until your VPN connection drops and all of your torrent traffic starts using your regular internet connection, which is kind of what we were trying to avoid with the VPN in the first place.

While OpenVPN is usually pretty good about reconnecting if the connection is lost, it's better if that doesn't happen, so in this tutorial we'll setup Transmission to only route our torrent traffic through the VPN, and if the VPN is not connected Transmission will just stop torrenting until the VPN is reconnected.

We need to create a template of our transmission-daemon setting.json file so that we can update the ip address that we download torrents through every time the VPN connects:

sudo cp /etc/transmission-daemon/settings.json /etc/transmission-daemon/settings_template.json

Open /etc/transmission-daemon/settings_template.json in your favorite text editor (as superuser) and change the following value:

{ 
    ...
    "bind-address-ipv4": "IP_ADDRESS",
    ...
}

Now setup a new script that OpenVPN will call when a VPN connection is successfully established.

sudo touch /etc/openvpn/up.sh
sudo chmod +x /etc/openvpn/up.sh

Open /etc/openvpn/up.sh in your favorite text editor (as superuser) and set it up like this:

#!/bin/sh

/etc/init.d/transmission-daemon stop
sed s/IP_ADDRESS/$4/ /etc/transmission-daemon/settings_template.json > /etc/transmission-daemon/settings.json
/etc/init.d/transmission-daemon start

Now edit your vpn.sh script from the OpenVPN tutorial and update it to look like this:

#!/bin/sh

sudo openvpn --config /path/to/your/openvpn/config/file --script-security 2 --up /etc/openvpn/up.sh

From now on, when you connect to the VPN it will force your torrent traffic to use the VPN connection, and if you get disconnected it should stop torrenting until you reconnect. You can use a tool like CheckMyTorrentIP to ensure that your torrent traffic is going over the VPN correctly.

Comments

comments powered by Disqus