Here is a complete 'HOW TO' for NOOBS (using debian) on making sure the debian-transmission user group (i.e transmission) only routes data through the vpn
DO NOT use the more lengthy 'How to' for vpn based on complex system scripts...! iptables is THE BEST (and foolproof) METHOD!!! - USING A FEW IPTABLE RULES based on the transmission user and group to control the vpn (not like many more complex 'hack' methods which use systemd scripts, up and down scripts etc...) and it's soooo simple!
Step 1 - Setup: (Assumes transmission is installed and debian-transmission user therefore exists!)
sudo apt-get install iptables
sudo apt-get install iptables-persistent
Step 2 - Create the transmission-ip-rules file
sudo nano transmission-ip-rules
and add the text in the code block below starting from #!/bin/bash
IMPORTANT
- If your local network is not of the form 192.168.1.x Change the NET variable to correspond to your own local network addressing format!!.
- Also be aware of the quirk that 192.168.1.0/25 actually gives the range 192.168.1.0-255!
- Sometimes your interfaces eth0, tun0 (which is the vpn) etc.. maybe different - check with 'ifconfig' and change if needed.
#!/bin/bash
# Set our rules so the debian-transmission user group can only route through the vpn
NET=192.168.1.0/25
GROUP=debian-transmission
IFACE_INTERNAL=eth0
IFACE_VPN=tun0
ALLOW_PORT_FROM_LOCAL=9091
iptables -A OUTPUT -d $NET -p tcp --sport $ALLOW_PORT_FROM_LOCAL -m owner --gid-owner $GROUP -o $IFACE_INTERNAL -j ACCEPT
iptables -A OUTPUT -d $NET -p udp --sport $ALLOW_PORT_FROM_LOCAL -m owner --gid-owner $GROUP -o $IFACE_INTERNAL -j ACCEPT
iptables -A OUTPUT -m owner --gid-owner $GROUP -o $IFACE_VPN -j ACCEPT
iptables -A OUTPUT -m owner --gid-owner $GROUP -o lo -j ACCEPT
iptables -A OUTPUT -m owner --gid-owner $GROUP -j REJECT
# not needed - but added these to properly track data to these interfaces....when using iptables -L -v
iptables -A INPUT -i $IFACE_VPN -j ACCEPT
iptables -A INPUT -i $IFACE_INTERNAL -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
# track any forward (NAT) data for completeness - don't care about interfaces
iptables -A FORWARD
Save the file and then run
sudo iptables -F
sudo chmod +x transmission-ip-rules
sudo ./transmission-ip-rules
then make sure these rules persist between reboots with:
sudo dpkg-reconfigure iptables-persistent
and tap yes to both prompts. DONE!
What is great about this script is that it will track all data through the device! When you issue
sudo iptables -L -v
it will show how much data is going to which interface and which side INPUT or OUTPUT so you can be assured that the vpn script is working properly.
Eg;
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1749K 661M ACCEPT all -- tun0 any anywhere anywhere
3416K 3077M ACCEPT all -- eth0 any anywhere anywhere
362K 826M ACCEPT all -- lo any anywhere anywhere
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 all -- any any anywhere anywhere
Chain OUTPUT (policy ACCEPT 2863K packets, 2884M bytes)
pkts bytes target prot opt in out source destination
1260 778K ACCEPT tcp -- any eth0 anywhere 192.168.1.0/ 25 tcp spt:9091 owner GID match debian-transmission
0 0 ACCEPT udp -- any eth0 anywhere 192.168.1.0/ 25 udp spt:9091 owner GID match debian-transmission
1973K 1832M ACCEPT all -- any tun0 anywhere anywhere owner GID match debian-transmission
8880 572K ACCEPT all -- any lo anywhere anywhere owner GID match debian-transmission
13132 939K REJECT all -- any any anywhere anywhere owner GID match debian-transmission reject-with icmp-port-unreachable
This script has been exhaustively tested on connects, disconnects, reboots from the vpn. It works great. Transmission can ONLY use the VPN. The great advantage of this script over the others is that I have made sure as you can see (via iptables -L -v
) that your data tallies with what is pulled over transmission (by adding INPUT (all) and Forward (all) rules for each interface eth0, vpn (tun0)). So you know exactly whats happening!!! The data totals will not tally exactly with transmission - Unfortunately I cannot discriminate on the INPUT side down to the debian-transmission user, and there will be both extra overhead and perhaps other processes using the same VPN, but you will see the data roughly tallies on the INPUT side and is about half on the OUTPUT for the vpn confirming its working.
Another thing to note - it take a while on a vpn disconnect (all traffic stops with transmission) and reconnect for transmission to 'get going' on the new vpn so don't worry if it takes about 5 mins to start torrenting again...
TIP - google 'MAN iptables' and see this article on bandwidth monitoring if you want to know line by line how this script works...