How to route application traffic through Tor using 'torsocks' (with examples)
- Linux
- December 17, 2024
The torsocks
command is a powerful tool that allows you to route the network traffic of any application through the Tor network, enhancing your anonymity and privacy online. By default, torsocks
routes all traffic through the Tor SOCKS proxy running at 127.0.0.1:9050, the default settings of the Tor daemon. This can be particularly useful for users who are concerned about privacy or who want to circumvent local network restrictions.
Use case 1: Run a command using Tor
Code:
torsocks wget https://example.com/file.txt
Motivation:
Using the torsocks
command with another command like wget
allows users to download files from the internet while concealing their IP address and location. This can be particularly beneficial for individuals who want to perform tasks online without revealing their identity or location.
Explanation:
torsocks
: Command used to route traffic through the Tor network.wget
: A command-line utility for downloading files from the web.https://example.com/file.txt
: The URL from which the file is to be downloaded.
Example output:
--2023-10-05 12:00:00-- https://example.com/file.txt
Resolving example.com (example.com)... 93.184.216.34
Connecting to example.com (example.com)|93.184.216.34|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/plain]
Saving to: ‘file.txt’
[ <=> ] 1,024 --.-K/s in 0s
2023-10-05 12:00:01 (550 MB/s) - ‘file.txt’ saved [1024]
Use case 2: Enable or disable Tor in this shell
Code:
. torsocks on
Motivation:
This use case is useful for users who wish to toggle the use of Tor within a shell session. By enabling Tor, all subsequent commands executed in the session will automatically route through the Tor network. Conversely, disabling it will return to normal, direct internet connections. This functionality provides great flexibility, allowing users to easily switch between anonymous and direct internet access as needed.
Explanation:
. (dot)
: A shell command to source the script and execute it in the current shell.torsocks
: Command to control the Tor routing capabilities.on
: Argument to enable the Tor routing for the session.
Example output:
Tor is now enabled in this shell.
Use case 3: Spawn a new Tor-enabled shell
Code:
torsocks --shell
Motivation:
Spawning a new Tor-enabled shell is valuable for users who need a dedicated shell environment where all operations and commands are automatically routed through the Tor network. This can be useful for running multiple tasks concurrently, with some requiring Tor and others not, without needing to toggle the Tor routing on and off continuously.
Explanation:
torsocks
: Command to handle Tor routing.--shell
: Argument to spawn a new subshell with Tor routing enabled.
Example output:
user@hostname:~$ (torsocks shell)%
Use case 4: Check if the current shell is Tor-enabled
Code:
torsocks show
Motivation:
Checking if the current shell is Tor-enabled is crucial for users who want to verify the status of their network routing configuration. Particularly when working in an environment where privacy is key, confirming whether traffic is routed through Tor ensures that activities remain anonymous.
Explanation:
torsocks
: Commands that manage Tor routing.show
: Argument to display the status of Tor routing in the current shell.
Example output:
TORSOCKS_MODE: enable
LD_PRELOAD=/usr/lib/torsocks/libtorsocks.so
Use case 5: Isolate traffic through a different Tor circuit
Code:
torsocks --isolate curl https://check.torproject.org/api/ip
Motivation:
Isolating traffic through a different Tor circuit helps improve anonymity by ensuring that the traffic of the specified command is routed through a distinct Tor path from other commands. This is beneficial for users who need to make multiple anonymous requests simultaneously or want to prevent correlation of traffic based on shared circuits.
Explanation:
torsocks
: Command for Tor routing.--isolate
: Argument that isolates the traffic through a new Tor circuit.curl
: A command-line tool used to transfer data from or to a server.https://check.torproject.org/api/ip
: The URL to check the current Tor exit IP.
Example output:
{"IsTor":true,"IP":"178.62.193.182"}
Use case 6: Connect to a Tor proxy running on a specific address and port
Code:
torsocks --address 192.168.1.100 --port 9051 command
Motivation:
Specifying a particular Tor proxy address and port is essential for users who have a custom Tor setup or are connecting through a different location. This capability allows users on complex networks or multi-router systems to specify the exact proxy settings for their Tor traffic.
Explanation:
torsocks
: Command for configuring Tor routing.--address 192.168.1.100
: Argument specifying the IP address of the Tor proxy.--port 9051
: Argument specifying the port of the Tor proxy.command
: The command whose traffic you want to route through the specified Tor proxy.
Example output:
Connecting to 192.168.1.100:9051 through Tor...
Command execution successful
Conclusion:
The torsocks
command provides versatile options for routing application traffic through the Tor network, enabling greater privacy and security. By understanding and leveraging these use cases, users can effectively manage their anonymity needs depending on the requirement to toggle Tor, isolate traffic, or connect through specific proxy addresses and ports.