How to install and use Tor as a proxy in Ubuntu or Linux Mint well explained

in #linux5 years ago

tor-project.png
Tor (The Onion Router) directs Internet traffic through an overlapping network of volunteers, in order to protect user privacy and freedom.
This article explains how to install the latest version of Tor on Ubuntu 18.04 or 16.04 or Linux Mint 19 and 18.x, and how to use it as a proxy with several applications.
This should work on Ubuntu 16.04 / Linux Mint 18.x, Ubuntu 18.04 (and 18.10) / Linux Mint 19 (and 19.x), and newer, as well as Linux distributions based on Ubuntu, such as SO Elementary, and Ubuntu as Kubuntu, Xubuntu, Ubuntu MATE, etc.
Ubuntu versions prior to 16.04 / Linux Mint before 18.x do not use systemd, so some things in this article do not work for both versions earlier.
Tor does not prevent online services from determining that you are using Tor, so keep this in mind. This should be used for privacy, and not anonymity. If all you need is a web browser with Tor, using Tor Browser, which uses Firefox ESR as a base, is very easy to use and includes everything you need. This article is for using Tor with other applications.
Installing Tor and optionally Privoxy on Ubuntu or Linux Mint
I. Install Tor on Ubuntu or Linux Mint

The Tor package of the Ubuntu repositories is generally not updated frequently, so it is recommended that you use the official Tor repository.
Start by installing apt-transport-https, necessary to use the repositories:

sudo apt install apt-transport-https
And then add the Tor repository (and the key) in Ubuntu using these commands:
sudo sh -c 'echo "deb https://deb.torproject.org/torproject.org/ $ (lsb_release -cs) main" >> /etc/apt/sources.list.d/tor.list'

sudo apt-key adv --keyserver hkp: //keyserver.ubuntu.com: 80 --recv-keys A3C4F0F979CAA22CDBA8F512EE8CBC9E886DDD89

sudo apt update

For Linux Mint, replace $ (lsb_release -cs) in the first command with the code name of the Ubuntu version on which your version of Linux Mint is based. For Linux Mint 19 (and 19.x), use bionic, while for Linux Mint 18.x use xenial.
Now you can install Tor, tor-geoipdb (to be able to use country-specific output nodes), torsocks (a library for easily torching applications) and deb.torproject.org-keyring (a package that makes sure you have the last signature of key repository):

sudo apt install tor tor-geoipdb torsocks deb.torproject.org-keyring

II. Optionally, install Privoxy and configure it to use with Tor
Privoxy is a web proxy without a cache. You can use it with Tor if you plan to use Tor with applications that only support HTTP proxies (not compatible with SOCKS5).
To install Privoxy on Ubuntu or Linux Mint, use:

sudo apt install privoxy

To use Privoxy with Tor you must edit your configuration file ed / etc / privoxy / config with a text editor, such as Nano (as root):

sudo nano / etc / privoxy / config

In this configuration file, paste the following line at the end, without changing anything else:
forward-socks5 / localhost: 9050.
There is a space and then a period at the end of the line, which is not a typo.
Then save the file and sa (to save the file using the Nano command line text editor, press Ctrl + O, then Enter; exit using Ctrl + X).
Now you will need to restart Privoxy:
sudo sudo systemctl restart privoxy
Using Tor and Privoxy in Ubuntu or Linux Mint

I. Tor use through torsocks
To easily start an application and make it use Tor, you can use torsocks, which works with both GUI and command line programs. This library ensures that DNS requests are handled securely and explicitly rejects any non-TCP traffic from the application you are using.
For example, to start Sporify with torsocks, use:
spotify torsocks
To test it, run this command to get your real IP address:
curl ipv4.icanhazip.com
(In case curl is not installed, you can install it using: sudo apt install curl)
And then execute the same command but using torsocks:
torsocks curl ipv4.icanhazip.com
The torsocks command should return a different IP because it was executed through the Tor proxy.
If you receive an error, the Tor service may not be running. It should start automatically when it is installed, but if it is not, you can start it with this command:

sudo systemctl start tor

However, there is a problem with torsocks: it fails to start some applications. For example, running torsocks firefox or torsocks google-chrome does nothing. For such cases, you must enter the Tor SOCKS5 proxy manually in the application you wish to use; see below.
II. Enter the proxy address manually
Once you install Tor and Privoxy, you can use the following proxy information (you only need to configure one of these two) in the application's network / proxy settings:

SOCKS5 proxy: use localhost as the proxy / IP server, and 9050 as the HTTP proxy port (requires Privoxy to be installed and configured as explained): use localhost as the proxy address and 8118 as the port.
Use an SOCKS5 proxy whenever possible or, if the application does not support it, use a normal HTTP proxy instead.
You can also use Tor as a proxy for the entire system. For example, in Gnome, go to System Settings> Network, click Network Proxy, set the proxy to Manual, then use localhost as Host Socks and 9050 as the port:

gnome-network-proxy-settings-tor-1024x740.png

Keep in mind that Chromium-based web browsers (Google Chrome, Opera, Vivaldi, etc.) do not allow you to configure a proxy in your configuration and instead use the entire system proxy.
III. How to change the IP address given by Tor
In case you need to change the IP address provided by Tor, the easiest way is to recharge the Tor service, like this:

sudo systemctl reload tor

This command will reload the Tor configuration and make Tor configure a new circuit, giving you a new IP address.
Verify the IP address using the torsocks curl ipv4.icanhazip.com command.
IV. How to configure Tor to use country-specific exit nodes (choose from which country your new IP should be)
In order to specify country-specific exit nodes in Tor, the tor-geoipdb package must be installed. This package is mentioned in the previous instructions, so it should already be installed, but I thought I would mention it in case you already had Tor installed and omitted the installation instructions in this article.
To use Tor to change your IP to a specific country IP, you must edit the Tor configuration file / etc / tor / torrc as root:

sudo nano / etc / tor / torrc

Add the following two lines to the file, without changing anything else:
ExitNodes {COUNTRY_CODE}
StrictNodes 1
Replace COUNTRY_CODE with the 2-letter country code ISO3166, for example, use us for the United States, it is for Spain, and so on. You can find a list of country codes here. You can also specify several countries by separating them with a comma, like this: {country1}, {country2}, {country3}, for example {be}, {pl}, {ca}. Countries must be specified in parentheses: {}.
When you're done, save the file and exit (to save the file using the Nano command line text editor, press Ctrl + O, then Enter; exit using Ctrl + X), and reload the Tor settings using this command:
sudo systemctl reload tor

Sort:  

Congratulations @kelvinmz! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made your First Vote

You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness to get one more award and increased upvotes!