02 December 2012

Raspberry Pi Torrent Box

Overview

This post is a pretty detailed how-to on setting up your Raspberry Pi to be a dedicated, low-power, globally accessible torrent box. Just to get an idea of the awesomesauce we're aiming for here, the final product will be capable of the following:

  • Micro USB for power draw
  • Only 1x ethernet and 1x USB connector needed, no display or anything fancy to take up space
  • Add and manage torrents remotely with a full GUI
  • Do this from anywhere in the world
  • Manage everything over HTTPS for good measure
  • Remote admin and troubleshooting with SSH
  • Retrieve downloaded files via the network (eg. Windows File Sharing)
  • Automatically and blindly start all services when booted like a true workhorse

Arrrr mateys, believe it or not, there are good and legal things released through torrent networks, and I can't wait to get my hands on all of them. This device is a beautiful servant just waiting to do its master's bidding and fetch many many things from the far corners of cyberspace.

Drones
It's just as mindless and hardworking as these drones when it comes to collecting torrent pieces

The basic setup we are aiming for consists of just a few pieces of software:

  • Arch Linux ARM as the base operating system
  • Deluge torrent client
  • Deluge Web UI
  • sshd for remote management

Not everyone is experienced with Linux or the command line, so I tried to go into quite a bit of detail with the instructions. If you would like to jump to a particular section or just get a rough overview of the steps, here you go. There is also a TL;DR section.

Contents

  1. Installing Arch Linux
  2. Booting Arch and setting up the network
  3. Set up the system clock
  4. Set up SSH
  5. Set up a download medium
  6. Install Deluge
  7. Configure Deluge and the Web UI
  8. Allow retrieval of downloads via Windows File Sharing / SMB
  9. Make it globally accessible
  10. Behold the torrent drone!
  11. Known niggles
  12. TL;DR
  13. Credits and references

Step 1 - Installing Arch Linux

Installing Arch Linux on the Pi is actually pretty simple. There is a pre-made image file available from the offical Raspberry Pi downloads. Gasp! It's even available as a torrent! Who knew?

There are already instructions on how to install images onto the Pi's SD card which I will not repeat here, but if you are on Windows you will need win32diskimager to properly write the image and make the card bootable.

Writing the .img file is a pretty straightforward process. There isn't even a complex OS install to go through after the first boot, it's just ready for business. It's not really the point of this article so if you are having any trouble please refer to this excellent guide.

Step 2 - Booting Arch and setting up the network

Ok, I lied. You will need a screen, at least for the installation part of this guide. Once the Pi is set up properly the screen can be abandoned, but, for now, in order to set everything up you will have to see what you're doing and there will be changes to the network configuration which might fail and break any SSH connection you're using.

At this point you would probably be pretty well off getting a USB keyboard and an HDMI monitor to plug into your Pi. I would also recommend plugging in an ethernet cable before booting.

Once that's done, you can boot the Pi by simply plugging in the Micro USB cable. A red light should indicate that it has power and you should briefly see a picture of a raspberry on the monitor. Once everything is loaded, you will be promted for a login. The default details are Username: root Password: root.

Initial network config and package upgrade

Arch Linux seems to be going through some serious changes (notably the switch to systemd for service management), so it would probably be beneficial to get everything up to date at this point. There should already be a working network connection configured with DHCP. You should probably try to ping some well-known host and your router to check that everything is fine. If it isn't, consult the Arch Linux Beginners' Guide for instructions. As per the guide mentioned earlier, you can upgrade all your packages with the following commands:

pacman -Sy pacman
pacman-key --init
pacman -S archlinux-keyring
pacman-key --populate archlinux
pacman -Syu --ignore filesystem
pacman -S filesystem --force

Static IP setup

I had some trouble setting up a static IP. The box would just continuously get assigned a new DHCP address. There are resources to consult on setting up a static IP, namely the beginners' guide and the Arch Wiki.

You can try first disabling DHCP with the following command:

# systemctl disable dhcpcd.service

You can check what network interfaces are available by issuing the "ifconfig" command. The default interface is usually eth0 as was the case with the Raspberry pi. Then manually assign an IP for the time being:

# ip addr add 192.168.1.123/24 dev eth0
# ip route add default via 192.168.1.1

This assumes that your router is situated at "192.168.1.1" on your local network. Some home networks use a "10.0.0.0" network, so adjust the commands to suit your needs. I decided to use "192.168.1.123" as the IP for the Pi.

Otherwise, the beginners' guide instructions are probably the easiest:

# pacman -S ifplugd                        # Install ifplugd, which is required for net-auto-wired
# cd /etc/network.d
# cp examples/ethernet-static .            # Copy a sample profile from /etc/network.d/examples to /etc/network.d
# nano ethernet-static                     # Edit the profile as needed
# systemctl enable net-auto-wired.service  # Enable the net-auto-wired service

If you can ping your router, but not any websites, you need to set up DNS servers in "/etc/resolv.conf". If your router forwards DNS requests you can add a single line:

nameserver 192.168.1.1

There are more details in the Arch Wiki as usual.

This was a bit more of a mission than I had hoped and I found the old rc.conf method a lot easier. You can check if the static IP was successful by issuing "ifconfig" and checking the addresses, and doing a ping to see if there is internet connectivity. Networks can be a chore, so if this does not work on your home network, scour the Arch Wiki and may you be successful in your travels. The joys of Linux can be overwhelming sometimes.

Step 3 - Set up the system clock

The Pi doesn't have a hardware clock to keep track of time. It will have to rely on NTP. You can check what the Pi thinks the date is with, surprise,

# date
Thu Jan  1 02:02:26 SAST 1970

You can try force an update with

# timedatectl
# ntpd -qg

There is an article on the Arch Wiki about Network Time Protocol daemon. Everything should work out of the box, but if it doesn't, I somehow ended up with 2 NTP daemons, ntpd and openntpd. I cannot remember if I installed the extra one or not, but you can try enable or disable either or both using the usual route:

# systemctl disable openntpd.service

The reason for fiddling with the date is that it affects how cookies are handled by the web interface.

Step 4 - Set up SSH

Apparently ssh is installed by default. However, you can still disable the root login via ssh and create yourself a "normal" user account. Having only a root account is generally bad practice. There are Arch Wiki articles on setting the root password and adding a regular user as well as configuring SSH. You can set up a normal user like "drone" for accessing the box.

To disable root login, in the "/etc/ssh/sshd_config" file you would look for the line

...
#PermitRootLogin yes
...

and change it to

...
PermitRootLogin no
...

Restart sshd:

systemctl restart sshd.service

At this point you can try SSH into the box with your "normal" user account. To do any admin you can still switch to root after loggin in with "su". If you can successfully SSH, try restarting the box to see if it is all still accessible after a reboot (this will obviously kill the current SSH session).

$ ssh drone@192.168.1.123
$ su
# reboot

Give the Pi a minute to start up, then try SSH again and see if the NTP is all in order:

$ ssh drone@192.168.1.123
$ date
Sun, 2012-12-02 11:24:10 SAST

Hopefully the static IP and NTP worked and everything is working so far. Then you could safely dump the keyboard and screen and do everything over SSH from now on because of the static IP.

Step 5 - Set up a download medium

Even if you still have the keyboard plugged in and aren't working over SSH, the Pi has another USB slot! This thing is amazing. In any case, I decided to use a USB external to save content. I figured a network mounted drive could work, but that kills a bit of the point if the network drive has the same availability as this bad boy.

So let's assume you're going with USB too, then. Another fair assumption is that the drive is formatted with NTFS. We will need some packages to deal with that.

# pacman -S ntfs-3g

Now you can plug in the drive and figure out where it's sitting. Check for your drive's details:

# blkid
/dev/mmcblk0: PTTYPE="dos"
/dev/mmcblk0p1: SEC_TYPE="msdos" UUID="A697-C157" TYPE="vfat"
/dev/mmcblk0p2: UUID="f9e11351-e87a-4df2-a9be-27f03d215329" TYPE="ext4"
/dev/sda1: LABEL="External" UUID="16F81B4AF81B280F" TYPE="ntfs"

It will probably be at the bottom of the list, but if it's the only thing plugged in it will be the only device that is not "dev/mmcblk0". Mine is "/dev/sda1" and this will likely be the case. Now we want to make this permanently accessible on the system. We are going to create a known mount point for the disk in the fstab.

# mkdir /mnt/usb   # create mount point
# nano /etc/fstab

Add the following to the bottom of the fstab:

/dev/sda1 /mnt/usb ntfs-3g defaults,user 0 0

And mount the drive:

# mount /mnt/usb

Now whenever the Pi is booted the USB will be available at a fixed location.

Step 6 - Install Deluge

There is actually a setup guide for Deluge on the Arch Wiki. Install the program with

# pacman -S deluge

By default it runs as the user "deluge" in the group "deluge". I did not see a reason to change these so I left it as-is.

Setting up autostart

The services should start at every system boot. We permanently enable them with the following command:

# systemctl enable deluged.service deluge-web.service

Step 7 - Configure Deluge and the Web UI

In our setup, deluge will be running as a system service, not as a user program. It will also not have any user interfaces like GTK. We will be using the Web UI, which also runs as a system service. Deluge can be started up with:

# systemctl start deluged.service deluge-web.service

You can access the Web UI by navigating to http://192.168.1.123:8112, where "192.168.1.123" is obviously the static IP you picked for the Pi. The default password is "deluge". You should probably change this first.

If you get into a login loop at this point (you correctly specify the password but keep getting the dialog), it is a problem with setting cookies because the system time is so wrong. Double check that your system time is correct.

Setting up download location

Go to Preferences -> Downloads and for the "Download to:" option choose "/mnt/usb".

Setting up HTTPS

Most of these instructions are from the Arch Wiki but I will provide them here for convenience. Before setting up HTTPS, we need to stop the Web UI:

# systemctl stop deluge-web.service

Then we need to generate new certificates in "/srv/deluge/.config/deluge/ssl/":

# cd /srv/deluge/.config/deluge/ssl/
# openssl req -new -x509 -nodes -out deluge.cert.pem -keyout deluge.key.pem

Now edit "/srv/deluge/.config/deluge/web.conf" and change the pkey and cert configuration directives to use your new self-signed certificates and also enable SSL:

# nano /srv/deluge/.config/deluge/web.conf

...
"pkey": "ssl/deluge.key.pem",
...
"cert": "ssl/deluge.cert.pem",
...
"https": true,

Setting up the Web UI port

This last step is a bit weird, but it happened to me that with SSL enabled I would only get a blank page for the Web UI. According to the Deluge forums a simple fix is to change the port from 8112 to 8110. This can also be set in the Web UI configuration file:

# nano /srv/deluge/.config/deluge/web.conf

...
"port": 8110,
...

Now the Web UI can be restarted:

# systemctl start deluge-web.service

It will be accessible at https://192.168.1.123:8110. Another stupid "gotcha" here is to make sure you type in the "https://" in the browser's address bar. If there is an autocomplete for the address in your history and you do not do this the Web UI will appear inaccessible because it is trying to connect over standard HTTP. Stupid, yes. Happens, yes.

Step 8 - Allow retrieval of downloads via Windows File Sharing / SMB

We will be using Samba to host the files on the network. Once again, there is a section on Samba in the Arch Wiki. Installation is pretty straightforward:

# pacman -S smbclient samba

Now we just need to set up a default config and edit it a little bit:

# cp /etc/samba/smb.conf.default /etc/samba/smb.conf
# nano /etc/samba/smb.conf

You should set the workgroup to match that of your home network (mine is WORKGROUP, I think that's the default). By default, printers and home directories are shared. I don't like this, so I commented those sections out:

...
# workgroup = NT-Domain-Name or Workgroup-Name, eg: MIDEARTH
   workgroup = WORKGROUP
...
#============================ Share Definitions ==============================
;[homes]
   ;comment = Home Directories
   ;browseable = no
   ;writable = yes
...

There are already examples in the file of how to define new shares, but this is what I used for the torrent downloads (you can add this at the bottom of the config file):

[Downloads]
   comment = Deluge Downloads
   path = /mnt/usb
   public = yes
   writable = no
   printable = no

In order to access the shares, you will need an existing user account on the system to log in with and set it up as a Samba user. I decided to use the deluge user, but you could also use the "normal" use account you set up earlier.

# pdbedit -a -u deluge  # set up samba user for logins

Now all that is needed is to permanently enable and start the Samba server to share files:

# systemctl enable smbd.service nmbd.service
# systemctl start smbd.service nmbd.service

At this point you should probably try to see if the shares are available on your network with a normal network share browser.

Step 9 - Make it globally accessible

This step is probably optional. If you don't mind only managing your torrents when you are at home on your home network, you can skip this step. This also requires a router that supports dynamic DNS. In a nutshell:

  1. Create an account at a free dynamic DNS service like DynDNS
  2. Set up your router to regularly update your home IP with the dynamic DNS service
  3. Set up port forwarding on your router for the Web UI. The IP will be the static IP (192.168.1.123) and the port will be 8110
  4. While you're at it, set up port forwarding for the standard Deluge torrent port for incoming connections

Now you can manage torrents from anywhere by visiting https://<your-dynamic-dns-domain>:8110. Setting up a strong password is probably a very good idea.

Depending on your router and which service you pick the steps will differ, so I would recommend Googling how to set up dynamic DNS and port forwarding.

Behold the torrent drone!

It is complete! You are now the proud owner of a special breed of torrent minion. It is mindless and hardworking and it is all yours.

Main Menu
No master, please! No more! All I do is serve you!

What else to make it do is up to you.

Known niggles

I'm still not sure exactly how the whole NTP thing works. It's been unreliable, to say the least. This machine should not reboot too often (that's the idea), but when it does, it wouldn't hurt to check if the date hasn't gone back out of sync.

I've also had a problem with Deluge itself where it was unable to load a specific magnet link that loaded fine in uTorrent.

This is a very "fire-and-forget" torrent system. A quick download could probably still go on your main machine, because transferring the files back is effort (even though it is easy with Samba). This is the kind of thing that is permanently chipping away at some huge archives of Linux ISO images, day and night.

TL;DR

If you're pretty competent and don't need the whole step-by-step business, here's what you do:

  1. Set up a static IP so that you can always access the box and set up port forwarding
  2. Set up a place for the Pi to store files such as an external USB or network drive, and add it to fstab
  3. Install Deluge and configure the Web UI for SSL
  4. Set up the download location to the USB drive (d'oh)
  5. Set up a dynamic DNS to your home machine and port forward the Web UI
  6. Remote admin via SSH

Credits and references

blog comments powered by Disqus