What is Tor?

Tor is a free overlay network that enables anonymous communication.
It is built on free and open-source software and run by over seven thousand volunteer-operated relays worldwide, as well as by millions of users who route their internet traffic through random paths across these relays.
This technique is called onion routing.

Installing Tor

First, we need to install the Tor service on our server.
This guide uses a Debian-based distribution.
Start by updating the package lists and upgrading the installed packages:

sudo apt update && sudo apt upgrade

Next, install Tor:

sudo apt install tor

Then start the service and enable it at boot:

sudo systemctl enable --now tor

By default, Tor runs no hidden service. It only exposes a SOCKS5 proxy on port 9050, which acts as a gateway to the Tor network.

Setting up a hidden service

Now that Tor is installed, we can configure our hidden service.
This guide covers a basic configuration. More advanced options are documented in the torrc manual.

How does a hidden service work?

To configure a hidden service correctly, it helps to understand what Tor is doing.
A hidden service is essentially port forwarding through Tor.
The commented section of the default configuration file describes it like this:

HiddenServicePort x y:z says to redirect requests on port x to the address y:z.

So Tor forwards ports under a hidden service directory, which holds the generated keys and the hostname derived from them:

HiddenServiceDir /var/lib/tor/hservice_example/
HiddenServicePort 80 127.0.0.1:80
HiddenServicePort 22 127.0.0.1:22

The URL of the hidden service (its hostname) is stored in /var/lib/tor/hservice_example/hostname.
Now that we know how it works, we can set up our own.

Editing torrc

Tor’s main configuration file is /etc/tor/torrc.
If, for example, we have a web server running on port 80 that we want to expose to the Tor network, we append the following lines:

HiddenServiceDir /var/lib/tor/webserver/
HiddenServicePort 80 127.0.0.1:80

If we also want SSH access through Tor, we can add another HiddenServicePort:

HiddenServiceDir /var/lib/tor/webserver/
HiddenServicePort 80 127.0.0.1:80
HiddenServicePort 22 127.0.0.1:22

Clients then reach SSH through Tor’s SOCKS5 proxy, for example with torsocks ssh user@youraddress.onion.
You can forward any TCP port. Some protocols won’t work well (such as FTP, which opens extra connections), but most do.

After saving the file, restart Tor and read your new address:

sudo systemctl restart tor
sudo cat /var/lib/tor/webserver/hostname

Tip: make your services listen only on 127.0.0.1, so they are reachable through Tor and not directly from the internet.

Custom hostname generation

That’s nice, but how do I get a custom address like the one The Pirate Bay uses?

piratebayo3klnzokc(...).onion

The hostname is derived from the service’s key pair, so getting a custom prefix means generating keys over and over until one matches. That takes computational work, and each extra character multiplies the time needed.
For this we use mkp224o, which generates version 3 onion addresses.
Clone the repository and follow its build instructions. Then run it with the prefix you want:

./mkp224o -d ~/onions mysite

Each match is saved as a directory containing hostname, hs_ed25519_public_key and hs_ed25519_secret_key. To use one, copy it over your HiddenServiceDir and fix the ownership and permissions, or Tor will refuse to start:

sudo cp -r ~/onions/mysiteXXXX.onion/* /var/lib/tor/webserver/
sudo chown -R debian-tor:debian-tor /var/lib/tor/webserver
sudo chmod 700 /var/lib/tor/webserver
sudo systemctl restart tor