An ordinary website has an IP address, and anyone who connects to it learns where it lives. An onion service flips that around. The server never accepts a direct connection from anyone. It only makes outbound connections into the Tor network, and clients reach it through a relay that both sides agree to meet at. The visitor doesn't learn the server's IP, the server doesn't learn the visitor's, and no relay in the middle sees both.
The old name was "hidden service," and you'll still see it in the config options: HiddenServiceDir, HiddenServicePort and friends. The Tor Project moved to "onion service" because "hidden" made people think the sites were secret or unlisted. Plenty of them are public. What's hidden is the location of the machine.
The address is the key
A v3 onion address is not a name that points at something, the way a DNS name points at an IP. It is the service's ed25519 public key, plus a checksum and a version byte, encoded in base32. That has a nice consequence: if you have the right address, you can cryptographically verify you've reached the holder of the matching private key. No certificate authority, no registrar, no DNS to poison. The downside is that the address is 56 characters of noise that nobody can remember, which is why phishing copies are such a problem. The structure is covered byte by byte in Anatomy of a v3 .onion Address.
What happens when the service starts
When tor starts with an onion service configured, it does roughly this:
- Picks a few relays to act as introduction points (three by default) and builds a full three-hop circuit to each one. It asks each to accept introduction requests on its behalf.
- Writes a descriptor: a signed document listing those introduction points along with the keys a client needs to talk to them.
- Uploads the descriptor to a set of relays with the HSDir flag, the distributed hash table that clients will query.
The descriptor upload is where v3 got clever. In v2, the HSDirs could see which onion address a descriptor belonged to, and a relay that wanted to harvest addresses just had to run as an HSDir and collect them. Researchers did exactly that. In v3, the service signs descriptors with a blinded key derived from its identity key and the current time period (a day, by default). The HSDir stores a blob indexed by that blinded key and can't work backwards to the onion address. A client who already knows the address can compute the same blinded key and ask for it. The descriptor body is also encrypted with a key derived from the address, so HSDirs can't read the introduction point list either.
Which HSDirs hold a given descriptor also rotates with the time period, so no single relay gets to sit on a particular service for long.
What happens when you visit
You paste the address into Tor Browser, and a lot happens before the page appears.
First, your client derives the blinded key for the current period, works out which HSDirs should hold the descriptor, and fetches it over a Tor circuit. It decrypts it using the address and gets the introduction point list.
Second, it picks a random relay to be the rendezvous point, builds a circuit there, and hands the relay a one-time cookie. The rendezvous relay now waits.
Third, your client builds a separate circuit to one of the introduction points and sends an INTRODUCE1 cell. Inside, encrypted to the service's key, is the rendezvous relay's identity, the cookie, and the first half of a key exchange. The introduction point passes it along the service's existing circuit. It can't read it.
Fourth, the service decides whether to answer. If it does, it builds its own three-hop circuit to the rendezvous point, presents the cookie and the second half of the handshake. The rendezvous relay matches the cookies and splices the two circuits together. From here on, your traffic goes through your three hops, across the splice, and through the service's three hops. End-to-end encrypted between you and the service, with the rendezvous relay just shoveling cells it can't read.
That's six relays between you and the server in the usual case. It's why onion sites feel slower to load the first time: you're waiting for a descriptor fetch plus two circuit builds plus the service building a third. Once the connection is up, it's often perfectly snappy.
Who knows what
| Party | Learns client IP? | Learns service IP? | Learns onion address? |
|---|---|---|---|
| HSDir | No | No | No (v3) |
| Introduction point | No | No | No |
| Rendezvous point | No | No | No |
| Client's guard | Yes | No | No |
| Service's guard | No | Yes | No |
The guards are the sensitive spots, as they are for ordinary Tor use. A service's guard knows it is talking to something running an onion service (the traffic pattern gives that away), even if it doesn't know which one. That's why attackers try to work out which guard a service uses, and why vanguards exist.
Why bother, if you're not hiding anything
Most people think of onion services as a way for a server to hide. That's one use. The others are less dramatic and arguably more common now:
Traffic never leaves Tor, so there's no exit relay to snoop or inject. Exit relays are the part of Tor that has historically been abused, with operators caught stripping TLS or rewriting Bitcoin addresses. An onion connection sidesteps all of that.
The address authenticates the server. If you've got the correct address, you are talking to the right machine. That property is why SecureDrop runs exclusively as an onion service, and why large sites like Facebook and the BBC run onion mirrors.
NAT and firewalls don't matter. Since the service only makes outbound connections, you can host something on a Raspberry Pi behind carrier-grade NAT with no port forwarding at all. OnionShare relies on exactly this to let you share files from a laptop on hotel Wi-Fi.
Running one
The minimal config is two lines in /etc/tor/torrc:
HiddenServiceDir /var/lib/tor/mysite/
HiddenServicePort 80 127.0.0.1:8080
Reload tor, and it creates the directory, generates keys, and writes your address into /var/lib/tor/mysite/hostname. Everything else (the web server, hardening, not leaking your real IP through some badly configured side channel) is on you. We have a walkthrough for hosting an onion site with Nginx, and a separate piece on how onion services leak their real IP, which is worth reading before you put anything sensitive up.
If you want the formal version of all this, the rendezvous protocol is specified in the rend-spec on spec.torproject.org. It's dense but readable, and it's the only place where every edge case is spelled out.