Install Tor on Ubuntu 20.04
- First, we need to install Tor on our system. Open a terminal and type the following command to install it:
$ sudo apt install tor
- By default, Tor runs on port 9050. You can confirm that Tor is up and running correctly by using the
sscommand in the terminal:$ ss -nltState Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*LISTEN 0 5 127.0.0.1:631 0.0.0.0:*LISTEN 0 4096 127.0.0.1:9050 0.0.0.0:*Another quick way to check if Tor is installed and see what version you’re running is with this command:
$ tor --version Tor version 0.4.2.7.
Tor network connection test
- Let’s see Tor in action and make sure it’s functioning how it’s supposed to. We’ll do this by obtaining an external IP address from the Tor network. First, check what your current IP address is:
$ wget -qO - https://api.ipify.org; echo 147.232.135.100 Then, we’ll run the same command but preface it with torsocks. This way, the command is run through our Tor client instead.
You should see a different IP address now. That means our request was routed through the Tor network successfully.$ torsocks wget -qO - https://api.ipify.org; echo 162.247.74.200
How to “torify” your shell
- Prefacing every network-related command with
torsockswill get old quickly. If you want to use the Tor network by default for shell commands, you can torify your shell with this command:$ source torsocks on Tor mode activated. Every command will be torified for this shell. - To make sure it worked, try retrieving your IP address without using the
torsockscommand prefix:$ wget -qO - https://api.ipify.org; echo 162.247.74.200 - The torified shell will only persist for the current session. If you open new terminals or reboot your PC, the shell will go back to the default to your ordinary connection. To turn
torsockson permanently for all new shell sessions and after reboot, use this command:$ echo ". torsocks on" >> ~/.bashrc - If you need to toggle
torsocksmode off again, enter:$ source torsocks off Tor mode deactivated. Command will NOT go through Tor anymore.
Enable the Tor control port
To interact with the Tor installation on our system, we need to enable Tor’s control port. Once enabled, Tor will accept connections on the control port and allow you to control the Tor process through various commands.
- To start, we will password-protect the Tor connection with the following command. We’re using
my-tor-passwordin this example.$ torpass=$(tor --hash-password "my-tor-password") - Next, use this command to enable the Tor control port and insert our previously hashed password:
$ printf "HashedControlPassword $torpass\nControlPort 9051\n" | sudo tee -a /etc/tor/torrc - You can check the contents of your
/etc/tor/torrcconfiguration file to confirm that the hash password settings have been correctly included.$ tail -2 /etc/tor/torrc HashedControlPassword 16:5D13CF3C7511D9FC60161179F8FFA1083C99601A5257CDC622E161839B ControlPort 9051 - Restart Tor to apply the changes:
$ sudo systemctl restart tor - Now, you should be able to see the Tor service running on both ports
9050and9051:ss -nltState Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0 4096 127.0.0.53%lo:53 0.0.0.0:*LISTEN 0 5 127.0.0.1:631 0.0.0.0:*LISTEN 0 4096 127.0.0.1:9050 0.0.0.0:*LISTEN 0 4096 127.0.0.1:9051 0.0.0.0:*
Connect to the Tor control port
- Now, we can connect to the Tor control port to communicate with Tor and issue commands. For example, here we use the
telnetcommand to request a new Tor circuit and clear cache:$ telnet 127.0.0.1 9051Trying 127.0.0.1...Connected to 127.0.0.1.Escape character is '^]'.AUTHENTICATE "my-tor-password"250 OKSIGNAL NEWNYM250 OKSIGNAL CLEARDNSCACHE250 OKquit250 closing connectionConnection closed by foreign host.
On Line 5 we have enteredAUTHENTICATEcommand and our Tor password. On Lines 7 and 9 we asked Tor for a new circuit and clean cache. You need to know a few commands to get much use out of the control port, which is why we linked to a list of commands above. - Communication with the Tor control port can also be shell-scripted. Consider the following example, which will request a new circuit (IP address) from Tor:
$ source torsocks offTor mode deactivated. Command will NOT go through Tor anymore.$ torsocks wget -qO - https://api.ipify.org; echo103.1.206.100$ echo -e 'AUTHENTICATE "my-tor-password"\r\nsignal NEWNYM\r\nQUIT' | nc 127.0.0.1 9051250 OK250 OK250 closing connection$ torsocks wget -qO - https://api.ipify.org; echo185.100.87.206
The magic happens on Line 5, where multiple Tor commands are strung together. The wget commands show how our connection’s IP address has changed after requesting a clean circuit. This script can be executed any time you need to obtain a new circuit.
