How to Set Up Raspberry Pi as a Headless Torrent Box with Deluged

Make sure Java libraries are installed:

sudo apt-get install openjdk-8-jre
sudo apt-get install openjdk-8-jre-headless

Start setting up the box by updating the pi to the latest and best:

sudo apt-get update
sudo apt-get upgrade

Install deluged:

sudo apt-get install deluged
sudo apt-get install deluge-console

Run Deluge to populate the configuration files:

deluged

Kill Deluge to be able to edit the files:

sudo pkill deluged

The file will be located here:

~/.config/deluge/auth

 

Create a backup file of the configuration file:

cp  ~/.config/deluge/auth  ~/.config/deluge/auth.old

Then edit it with nano file editor:

sudo nano  ~/.config/deluge/auth

Once inside the nano text editor, you’ll need to add a line to the bottom of the configuration file with the following convention:

user:password:level

Where user is the username you want for Deluge, password is the password you want, and thelevel is 10 (the full-access/administrative level for the daemon). So for our purposes, we used pi:raspberry:10. When you’re done editing, hit Ctrl+X on your keyboard and save your changes when prompted. Then, start up the daemon and console again:

deluged 
deluge-console

If starting the console gives you an error code instead of nice cleanly formatted console interface, type “exit” and then make sure you’ve started up the daemon.

Once inside the console, you’ll need to make a quick configuration change. Enter the following:

config -s allow_remote True 
config allow_remote exit

If you are running deluged under another user (e.g. deluge) you will need to temporarily login as that user to enable deluge-console to access that daemon’s config:

su --shell /bin/bash --login deluge

This enables remote connections to your Deluge daemon and double checks that the config variable has been set. Now it’s time to kill the daemon and restart it one more time so that the config changes take effect:

sudo pkill deluged 
deluged

At this point, your Deluge daemon is ready for remote access. Head to your normal PC (not the Raspberry Pi) and install the Deluge desktop program. You’ll find the installer for your operating system on the Deluge Downloads page. Once you’ve installed Deluge on your PC, run it for the first time; we need to make some quick changes.

Once launched, navigate to Preferences > Interface. Within the interface submenu, you’ll see a checkbox for “Classic Mode”. By default it is checked. Uncheck it.

Click OK and then restart the Deluge desktop client. This time, when Deluge starts, it will present you with the Connection Manager. Click the “Add” button and then input the IP address of the Raspberry Pi on your network, as well as the username and password you set during the earlier configuration. Leave the port at the default 58846. Click Add.

Back in the Connection Manager, you’ll see the entry for the Raspberry Pi; if all goes well, the indicator light will turn green.

Click Connect, and you’ll be kicked into the interface, connected to the remote machine.

Run Deluged from Boot:

User Management

For security it is best to run a service with a specific user and group. You can create one using the following command:

sudo adduser --system  --gecos "Deluge Service" --disabled-password --group --home /var/lib/deluge deluge
  • This creates a new system user and group named deluge with no login access and home directory: /var/lib/deluge

Add to the deluge group any users you wish to be able to easily manage or access files downloaded through Deluge, for example:

sudo adduser <username> deluge
Deluge Daemon (deluged) Service

Create the file /etc/systemd/system/deluged.service by entering in terminal:

sudo nano /etc/systemd/system/deluged.service

containing the following:

[Unit]
Description=Deluge Bittorrent Client Daemon
Documentation=man:deluged
After=network-online.target

[Service]
Type=simple
User=deluge
Group=deluge
UMask=007
ExecStart=/usr/bin/deluged -d
Restart=on-failure
# Time to wait before forcefully stopped.
TimeoutStopSec=300

[Install]
WantedBy=multi-user.target

Test if it is working:

sudo systemctl start deluged
sudo systemctl status deluged

Now enable it to start up on boot, start the service and verify it is running:

sudo systemctl enable deluged
Start deluged only if mount exists

Use this if you have a usb disk drive or network drive that may not be immediately available on boot or disconnected at random.

The following additions wait for those mountpoints before starting deluged. If they are unmounted or disconnected then deluged is stopped. When they become available again deluged is started.

Ensure you have added the correct drive details to fstab or equivalent so they are mounted at boot.

List the available drive mounts:

sudo systemctl -t mount

Look for your mount point in the Description column. Mounts are formatted similar to the mount point with – substituted for / in the path. Eg: media-xyz.mount

Modify the [Unit] section of the deluged.service script. Substitute xyz.mount for the mount you want the service to depend on:

[Unit]
Description=Deluge Bittorrent Client Daemon
# Start after network and specified mounts are available.
After=network-online.target xyz.mount
Requires=xyz.mount
# Stops deluged if mount points disconnect
BindsTo=xyz.mount

For multiple mount points add a space between additional entries. Eg: After=network-online.target xyz.mount abc.mount def.mount

Modify the [Install] section to ensure the deluged service is started when the mount point comes back online:

[Install]
WantedBy=multi-user.target xyz.mount

Note: WantedBy seems to work on some distros and not others. Possibly different versions of systemd?

Example code:

[Unit]

Description=Deluge Bittorrent Client Daemon

Documentation=man:deluged

After=network-online.target media-asusshare.mount
Requires=media-asusshare.mount
BindsTo=media-asusshare.mount
[Service]

Type=simple

User=deluge

Group=deluge

UMask=007

ExecStart=/usr/bin/deluged -d

Restart=on-failure

# Time to wait before forcefully stopped.

TimeoutStopSec=300

[Install]

WantedBy=multi-user.target media-asusshare.mount

Troubleshooting:

When Deluged daemon set up for automatic start on boot it may use a different folder for authentication than the one set up before ( ~/.config/deluge/auth ):

The file is located at

 /var/lib/deluge/.config/deluge/auth

Make sure that both has the same content as the /var/lib/deluge/.config/deluge/auth by coping the content to ~/.config/deluge/auth

Make sure the necessary options are set for remote access by entering into deluge-console:

deluge-console
config -s allow_remote True
config allow_remote exit

If you are running deluged under another user (e.g. deluge) you will need to temporarily login as that user to enable deluge-console to access that daemon’s config:

su --shell /bin/bash --login deluge
 
 

For those having issues with the deluge-console, it seems you need to login every time you want to hit up the console.

For example:

deluge-console "connect localhost (user) (pass); config -s allow_remote True"

That seemed to work for me, ensuring that the user exists in the auth file in the deluged config directory format (user:pass:level) eg: (imnx:yourmum01.:10).

If Deluge doesn’t downloads to the external HDD it could be because it can’t handle NTFS file systems properly. Install ntfs=3g to solve the problem:

sudo apt-get install ntfs-3g

Sources:

https://www.howtogeek.com/142044/how-to-turn-a-raspberry-pi-into-an-always-on-bittorrent-box/

https://www.techjunkie.com/create-a-headless-torrent-server-with-deluge-on-a-raspberry-pi/

https://dev.deluge-torrent.org/wiki/UserGuide/ThinClient

https://dev.deluge-torrent.org/wiki/UserGuide/Service/systemd

https://dev.deluge-torrent.org/wiki/Faq#WheredoesDelugestoreitssettingsconfig

https://superuser.com/questions/619261/remote-deluge-daemon-connection