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 buster main

Run this to install Tor Project public keys:

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

Install needed Tor packages:

apt-get install
apt-get install tor

Now edit /etc/tor/torrc:

AutomapHostsOnResolve 1
TransPort 9040
DNSPort 53

Add this on /etc/resolv.conf:


Now restart Tor service:

/etc/init.d/tor restart

Put this script in /root/

# destinations you don't want routed through Tor
# 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)
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; do
 /usr/sbin/iptables -t nat -A OUTPUT -d $NET -j RETURN
/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; do
 /usr/sbin/iptables -A OUTPUT -d $NET -j ACCEPT
/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

Run the bash script with: ./

Now try to use cURL to check your IP address:


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 ./

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