torsocks curl https://check.torproject.org/api/ip

If that prints "IsTor":true and an IP address that isn't yours, your Tor daemon is running and torsocks is doing its job. That's usually the first thing I run on a new box, before trusting anything else.

Tor exposes a SOCKS5 proxy, normally on 127.0.0.1:9050 when you run the tor service. Any program that speaks SOCKS5 can use it. The problem is that plenty of programs don't, or do it badly, and that's where wrappers like torsocks and proxychains come in. Neither is magic, and both have gaps you need to know about.

Getting tor running first

On Debian or Ubuntu, the Tor Project's own repository gives you a current version (the distro one can lag). Once installed:

sudo systemctl enable --now tor
ss -ltnp | grep 9050

If you'd rather use Tor Browser's Tor, note that it listens on 9150 and only while the browser is open. For scripts and servers, the system service is far more predictable. Settings go in /etc/tor/torrc, which is covered in our torrc options guide.

torsocks

torsocks is maintained by the Tor Project. It uses LD_PRELOAD to intercept networking calls (connect(), DNS lookups) inside a program and redirect them through Tor. It also blocks things that would leak, such as UDP sockets and direct DNS queries, instead of letting them through quietly.

torsocks ssh [email protected]
torsocks wget https://example.org/file.tar.gz
torsocks --shell          # new shell where everything is torified
. torsocks on             # torify the current shell
. torsocks off

For circuit isolation between commands, torsocks -i (or --isolate) gives each invocation its own SOCKS credentials, so Tor builds it a separate circuit. Two different tasks then come out of different exits.

The big limitation is that LD_PRELOAD only works on dynamically linked programs that use the C library for networking. Statically linked binaries ignore it. Many Go programs do their own DNS and syscalls and ignore it. Setuid binaries ignore it for security reasons. When torsocks can't hook a program, the program just connects directly, often without any error. That's the leak to worry about, and it's why I test with the check URL above using the actual tool, not just curl.

proxychains-ng

proxychains-ng (the maintained fork; the binary is proxychains4) also uses LD_PRELOAD, so it has the same blind spots. Its selling point is chaining: you can list several proxies and send traffic through them in order. With Tor you rarely need that, and stacking random public proxies on top of Tor usually hurts more than it helps.

The config lives at /etc/proxychains4.conf (or ~/.proxychains/proxychains.conf). For Tor, the important lines are:

strict_chain
proxy_dns

[ProxyList]
socks5  127.0.0.1 9050

proxy_dns matters. Without it, DNS lookups go out over your normal resolver and reveal every hostname you're visiting to your ISP. Many default configs list socks4 on 9050; change it to socks5 so hostnames get resolved by Tor.

proxychains4 nmap -sT -Pn example.org

That example shows a common disappointment: nmap's default SYN scan uses raw packets, which no SOCKS wrapper can proxy. Only -sT (full TCP connect) goes through, and slowly. ICMP pings never will.

Often better: use the program's own proxy support

Wrappers are a fallback. When a tool supports SOCKS natively, use that, because it'll work regardless of how the binary is linked.

curl --socks5-hostname 127.0.0.1:9050 https://example.org
git config --global http.proxy socks5h://127.0.0.1:9050
ssh -o ProxyCommand='nc -X 5 -x 127.0.0.1:9050 %h %p' user@host
export ALL_PROXY=socks5h://127.0.0.1:9050

Note the h in socks5h and the -hostname in the curl flag. Both tell the client to send the hostname to the proxy instead of resolving it locally. Plain socks5:// resolves DNS on your machine first, which is a classic DNS leak. It also means .onion addresses won't resolve at all, which is one way to notice the mistake.

The nc syntax above is for the OpenBSD netcat. Other netcat variants use different flags; ncat --proxy-type socks5 --proxy 127.0.0.1:9050 %h %p works with the Nmap project's version.

When to go further

All of these approaches depend on every program doing the right thing. One misbehaving tool, a library that does its own DNS, or a UDP-based protocol can leak your real address. If you need to be sure nothing escapes, enforce it at the network level instead: Whonix does exactly this by giving the workstation no route except through Tor, and Tails uses firewall rules to block anything that isn't going through it.

For a quick download over Tor or an occasional SSH session, torsocks is perfectly fine. For anything where a single leak would be serious, don't rely on wrappers alone.

#torsocks #proxychains #socks5 #command-line #linux