Raspberry PI 2 as a NAS device with torrenting

Update: Installing Ubuntu 14.04 is here (Supported through early 2019).
Note: The RPi2 with officially supported Ubuntu is proving problematic, I want to move to something more reliable.

GOAL:

Use a Raspberry PI as a NAS Seed Box (Network Attached Storage running torrent software). RPi 2 is perfect for this due to running at very low electrical low power and being faster than a Fast Ethernet bottleneck. I initially bought a RPi version 1B which I returned as it couldn’t send files over a wired LAN at 10MB per second (basically the bottleneck for Fast Ethernet (100mbps)). Also I can use a thin client for torrenting on my PC with Deluge rather than just a webUI.

Setup:

  • RPi version 2B running Ubuntu (yay linux).
  • USB Drive formatted in NTFS.
  • Install Samba (windows file sharing).
  • Install Deluge torrent software with a web interface (deluge also supports a remote thin client 🙂 ).

Instructions:

Using a HDMI monitor and keyboard:

  1. Install Ubuntu.
  2. I set a Static IP for my RPi since I don’t want a random DHCP address from my internet router and I can’t reserve an IP on my router. At the RPi Ubuntu terminal screen, open the nano text editor to the network settings file with this command:
    sudo nano /etc/network/interfaces

    I commented out the current DHCP settings:

    #iface eth0 inet dhcp

    and put in a static address with openDNS as the DNS:

    iface eth0 inet static
    address 192.168.1.38
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 208.67.222.222

    Then I exited nano with Ctrl X, press Y to save current modifications, enter to confirm file name. I usually reopen the file just to check it looks how I wanted (the up arrow retypes the previous command in the terminal window).

  3. Then I changed the RPi hostname (which is accessible from the windows folder address bar once samba is installed):
    sudo nano /etc/hostname
  4. Now I mounted my USB drive:
    -Use this command to show drive IDs:

    sudo blkid

    -Copy the UUID for the USB Drive (note that Ctrl C isn’t copy in the terminal)
    -Create a mount point for the USB drive:

    sudo mkdir /media/Data

    Before editing /etc/fstab directly, it is a good idea to make a backup. From a terminal:

    sudo cp /etc/fstab /etc/fstab.orig

    Open fstab

    sudo nano /etc/fstab

    and I added a line for my NTFS USB drive:

    UUID=NUMBERSNSTUFF /media/Data ntfs-3g uid=ubuntu,gid=ubuntu,umask=000 0 0
    Those settings didn't work for my next ftp project :/... these worked...
    UUID=NUMBERSNSTUFF  /media/Data ntfs-3g defaults,locale=en_AU.utf8 0 0
    

    I set options uid=ubuntu,gid=ubuntu,umask=000 so that samba could access the drive as well as deluge torrent client.
    EDIT: I chose these options this time – this allows read only access for ‘others’

    defaults,nobootwait,utf8,uid=ubuntu,gid=deluge,dmask=002,fmask=113

    Mount all the drives in fstab using

    sudo mount -a
  5. Install Samba.
  6. Install Deluge (from here) NOTE for 16.04 I used these instructions to install

    sudo add-apt-repository ppa:deluge-team/ppa
    sudo apt-get update
    sudo apt-get install deluged deluge-webui
    sudo nano

    EDIT – These instructions create a user and group for deluge (note these instructions are for my distro ‘Ubuntu 11.04 Natty up to 14.10 Utopic’):

    sudo adduser --disabled-password --system --home /var/lib/deluge --group deluge

    Add my user to the deluge group:

    adduser ubuntu deluge

    then allow myself access

    sudo chmod 770 "/var/lib/deluge/.config/" -R

    Then I followed the instructions linked in the EDIT link.

  7. Stop deluge with before editing the config file
    sudo stop deluged
    Ubuntu 16.04:
    systemctl stop deluged
    systemctl stop deluge-web
    systemctl status deluge-web

    edit the config file at

    sudo nano "/var/lib/deluge/.config/deluge/core.conf"

    I changed the daemon port and set allow_remote to true, also I modified my firewall rules to allow daemon and web ports through.

     

  8. SSL can be forced to avoid the login details being man-in-the-middle attacked. For me this was a major pain in the ass. You need A. to enable SSL and B. to import your SSL Certificates…
    (if you use the thin client -step 9- rather than web-ui, you don’t need this)A. After stopping deluge, edit /etc/init/deluge-web.conf to have ‘–ssl’ at the end, like this

    exec start-stop-daemon -S -c $uid:$gid -k $umask -x /usr/bin/deluge-web --ssl

    This will force SSL for the web ui.
    Possibly you will need to modify the users deluge conf files, for me there are some here: /home/ubuntu/.config/deluge/ specifically web.conf and web_plugin.conf
    I found that the preference settings in the web ui didnt do much.

    Now put your public and private keys in the right place. For me that is /home/ubuntu/.config/deluge/ssl/
    I renamed my certs to the existing certs, daemon.cert, deamon.pkey.

  9. These instructions are vaguely related and informative for remote access to the daemon. Basically you
    1. set the client deluge to classic mode in the settings
    2. allow_remote as in step 7
    3. edit the auth file in (as described sorta in the linked instructions)
      /var/lib/deluge/.config/deluge/
  10. In the case of an external drive not being mounted while deluge boots, it will set the progress for all torrents to 0% cause havoc. I fixed this by editing /etc/init/deluged.conf with the following ‘start on’ stanza:
    (this is covered for 16.04 in  those instructions)

    start on filesystem and static-network-up and mounted MOUNTPOINT=/media/Data
  11. Better…
    Ok so I didn’t configure my firewall for the actual BitTorrent ports. This kinda helps… Also if future problems arise I can change ports.

    
    sudo ufw allow proto tcp to any port 6881:6889

    I also added a port forward rule in my internet gateway.

  12. I had an instance where for some reason my Deluge config got corrupted. This was super frustrating to fix, so I made daily backups for the config:
    #!/bin/sh
    
    # This script backups up the deluge config daily.
    #From here https://rsync.samba.org/examples.html
    
    # directory to backup
    BDIR=/var/lib/deluge/.config/deluge
    
    BACKUPDIR=`date +%A`
    OPTS="--force --ignore-errors --delete-excluded 
     --delete --backup --backup-dir=/$BACKUPDIR -a"
    
    # the following line clears the last weeks incremental directory
    [ -d /var/lib/deluge/.config/emptydir ] || mkdir /var/lib/deluge/.config/emptydir
    rsync --delete -a /var/lib/deluge/.config/emptydir/ /var/lib/deluge/.config/$BACKUPDIR/
    rmdir /var/lib/deluge/.config/emptydir
    
    # now the actual transfer
    rsync $OPTS $BDIR /var/lib/deluge/.config/$BACKUPDIR/

    and edited admin crontab with ‘sudo crontab -e’ adding this line at the bottom

    0 5 * * * /usr/local/sbin/delugeConfBackup.sh

    Better..

  13. Migrating to a new system, I got everything working and copied the state folder to the new system (renaming the original on the new machine)

1 thought on “Raspberry PI 2 as a NAS device with torrenting”

Leave a Reply

Your email address will not be published. Required fields are marked *