Route All Internet Traffic Through Tor on Linux Debian

Posted on July 25, 2019 at 9:46 am

You can route all Internet traffic throguth Tor, here is how:

*** Please note, I am using Debian 10 Buster ***

Add this on /etc/apt/sources.list:

deb http://deb.torproject.org/torproject.org buster main

Run this to install Tor Project public keys:

gpg --keyserver keyserver.ubuntu.com --recv-key 74A941BA219EC810
gpg -a --export 74A941BA219EC810 | apt-key add -
apt-get update

Install needed Tor packages:

apt-get install deb.torproject.org-keyring
apt-get install tor

Now edit /etc/tor/torrc:

VirtualAddrNetwork 10.192.0.0/10
AutomapHostsOnResolve 1
TransPort 9040
DNSPort 53

Add this on /etc/resolv.conf:

nameserver 127.0.0.1

Now restart Tor service:

/etc/init.d/tor restart

Put this script in /root/tor.sh:

#!/bin/sh
 
# destinations you don't want routed through Tor
NON_TOR="192.168.1.0/24 192.168.0.0/24"
 
# the UID Tor runs as (generally is 108 or 109)
TOR_UID=`id -u debian-tor`
 
# Tor's TransPort (same as wrote on /etc/tor/torrc)
TRANS_PORT="9040"
 
iptables -F
/usr/sbin/iptables -t nat -F
 
/usr/sbin/iptables -t nat -A OUTPUT -m owner --uid-owner $TOR_UID -j RETURN
/usr/sbin/iptables -t nat -A OUTPUT -p udp --dport 53 -j REDIRECT --to-ports 53
for NET in $NON_TOR 127.0.0.0/9 127.128.0.0/10; do
 /usr/sbin/iptables -t nat -A OUTPUT -d $NET -j RETURN
done
/usr/sbin/iptables -t nat -A OUTPUT -p tcp --syn -j REDIRECT --to-ports $TRANS_PORT
 
/usr/sbin/iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
for NET in $NON_TOR 127.0.0.0/8; do
 /usr/sbin/iptables -A OUTPUT -d $NET -j ACCEPT
done
/usr/sbin/iptables -A OUTPUT -m owner --uid-owner $TOR_UID -j ACCEPT
/usr/sbin/iptables -A OUTPUT -j REJECT

Then set execution permission with: chmod +x tor.sh

Run the bash script with: ./tor.sh

Now try to use cURL to check your IP address:

curl https://api.ipify.org?format=json

You should see that your IP address is a Tor IP address.

If that doesn’t work, try to reboot the server.

Then restart Tor and run ./tor.sh

Other references:

Transparently Routing Traffic Through Tor

To stop Tor just type this:

/etc/init.d/tor stop

And to reset/flush iptables use this:

Reset (flush) Iptables rules Linux

Updated on September 10, 2019 at 10:02 am

Receive updates via email

Other Posts

Updated Posts