On a Linux system, is there a way to block all in and outbound traffic unless it passes through the Tor network. This includes any form of IP communication, not just TCP connections. For example I want UDP to be completely blocked since it cannot pass through Tor. I want this systems Internet usage to be entirely anonymous, and I don't want any applications leaking.
I realize this might be complicated because Tor itself needs to communicate with relay nodes somehow.
Easy enough with iptables. It can have rules which match specific users, and you should have already set up
tor
to run under its own user ID; deb and rpm packages provided by major Linux distributions and the Tor Project set up a user for Tor already.Complete sample, usable iptables and Tor configurations follow. This firewall can be loaded with the
iptables-restore
command. The end result of this configuration will transparently route all traffic originating from, or being forwarded through, the host to Tor, without needing to configure proxies. This configuration should be leak-proof; though you should of course test it thoroughly.Note that the uid for the tor user (here,
998
) is stored in numeric form by iptables. Substitute the correct uid for your tor user in each place that it appears here.Note also that the IP address of the host needs to be given in the first rule to support incoming clearnet and LAN traffic addressed directly to the host (here shown as
198.51.100.212
). If you have multiple IP addresses, repeat the rule for each address.The ssh INPUT rule only allows connections if they arrive via the local host, i.e. a Tor hidden service. If you also want to permit incoming ssh connections via clearnet, remove
-d 127.0.0.1
.The corresponding
torrc
file is:This configuration requires that the host have a static IP address. For the expected use cases, it's likely that you have already planned for it to have a static IP address.
And finally, the output!