Connect Your Servers with Tailscale
Install Tailscale on Debian or Ubuntu, connect several servers in a private network, write access rules and restrict SSH to that network with UFW.
On this page
Your values
Enter them once and the commands in this guide update.
This guide explains how to connect your servers and your workstation with Tailscale. Each machine receives a private 100.x.y.z address and talks to the others through an encrypted WireGuard tunnel, without you having to manage keys or open a port for each link.
By the end of the guide, your servers talk to each other on this private network, called a tailnet, only the access you have defined is allowed, and SSH is no longer reachable from the public address.
Order a Server
This guide applies to servers that you administer yourself:
Prerequisites
- One or more servers running Debian 12 or 13, or Ubuntu 22.04 or 24.04
- SSH access with a user that has
sudo - A Tailscale account
- Tailscale installed on your workstation, from the download page
- A fallback way into the server, such as your VPS's VNC console or rescue mode
The guide names the servers web-1 and db-1.
Ports Used
Tailscale generally requires no inbound port: machines open outbound connections.
| Port | Direction | Purpose |
|---|---|---|
| 443/tcp | Outbound | Coordination service and DERP relays |
| 41641/udp (source) | Outbound | Direct WireGuard tunnels |
| 3478/udp | Outbound | STUN, to discover the public address |
| 41641/udp | Inbound, optional | Helps establish direct connections |
Step 1: Install Tailscale
On each server, add the official repository matching your system, then install the package:
. /etc/os-release
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL "https://pkgs.tailscale.com/stable/${ID}/${VERSION_CODENAME}.noarmor.gpg" | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null
curl -fsSL "https://pkgs.tailscale.com/stable/${ID}/${VERSION_CODENAME}.tailscale-keyring.list" | sudo tee /etc/apt/sources.list.d/tailscale.list
sudo apt-get update
sudo apt-get install -y tailscale
tailscale versionStep 2: Connect the Servers
Start the connection, then open the URL it displays in your browser to authorize the machine:
sudo tailscale upThen give each server a clear name, here web-1 on the first and db-1 on the second:
sudo tailscale set --hostname=web-1Note its Tailscale address and list the machines in the tailnet:
tailscale ip -4
tailscale statusStep 3: Check the Link
From web-1, test db-1:
tailscale ping db-1
ping -c 3 db-1The tailscale ping reply shows the path used. via DERP(...) means traffic goes through a relay, whereas an IP:port address means a direct connection. Short names rely on MagicDNS, enabled by default on recent tailnets. If db-1 does not resolve, use its 100.x.y.z address.
Step 4: Prevent Servers from Expiring
A machine's key expires after 180 days by default. A server whose key expires leaves the tailnet, which is a problem if SSH is no longer reachable any other way.
On the Machines page, open the menu to the right of each server, then choose Disable Key Expiry.
Step 5: Tag the Servers
Tags give servers an identity that is independent of your account. They let you write readable rules.
Open the Access controls page. Add these two sections to the existing file, or complete them if they are already there, without removing anything else. Replace TAILSCALE_EMAIL with the exact address of your account, then save:
"groups": {
"group:admins": ["TAILSCALE_EMAIL"]
},
"tagOwners": {
"tag:server": ["group:admins"]
},Go back to Machines. In each server's menu, choose Edit tags, add tag:server, then save.
Step 6: Limit Access
By default, all machines in the tailnet can reach each other. Replace the contents of the policy file with the following to allow only what is necessary:
{
"groups": {
"group:admins": ["TAILSCALE_EMAIL"]
},
"tagOwners": {
"tag:server": ["group:admins"]
},
"acls": [],
"grants": [
{
"src": ["group:admins"],
"dst": ["tag:server"],
"ip": ["tcp:22", "icmp:*"]
},
{
"src": ["tag:server"],
"dst": ["tag:server"],
"ip": ["tcp:5432", "icmp:*"]
}
],
"tests": [
{
"src": "group:admins",
"proto": "tcp",
"accept": ["tag:server:22"]
},
{
"src": "tag:server",
"proto": "tcp",
"accept": ["tag:server:5432"],
"deny": ["tag:server:22"]
}
]
}| Section | Purpose |
|---|---|
groups |
Groups the authorized accounts |
tagOwners |
Defines who can assign each tag |
acls |
Legacy form of the rules. Empty here, it prevents falling back to the default policy that allows everything |
grants |
Recommended form of access rules. Anything not listed there is denied |
tests |
Checks run on every save. The file is rejected if one of them fails |
With this example, your devices reach the servers over SSH and the servers talk to each other on port 5432, the PostgreSQL port. Replace this port with those of your services. For several ports, write for example "ip": ["tcp:5432", "tcp:6379"].
Then check the result from web-1. The command on port 22 must fail:
sudo apt-get install -y netcat-openbsd
nc -vz -w 3 db-1 5432
nc -vz -w 3 db-1 22The test on port 5432 succeeds only if a service is listening there on an address reachable through Tailscale. From your computer, ssh YOUR_USER@web-1 must work.
Step 7: Restrict SSH to the Private Network
From your computer, first open a session through Tailscale:
ssh YOUR_USER@web-1In this session, allow the Tailscale interface and apply the default policies:
sudo ufw allow in on tailscale0
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw status numberedConfirm that UFW should be enabled by answering y. Filtering between tailnet machines is handled by the rules from step 6. If the server publishes a website, keep or add the 80/tcp and 443/tcp rules.
Then remove the public SSH rule, if it exists:
sudo ufw delete allow 22/tcp
sudo ufw status verboseIf your rule has a different name, such as OpenSSH, use sudo ufw delete allow OpenSSH.
Check the Result
Open a new session through Tailscale. It must succeed:
ssh YOUR_USER@web-1From a machine outside the tailnet, or after stopping Tailscale on your computer, the connection to the public address must time out:
ssh -o ConnectTimeout=5 YOUR_USER@YOUR_SERVER_IPOption: Use Tailscale SSH
With Tailscale SSH, authentication relies on your Tailscale identity rather than SSH keys. OpenSSH and its files are not modified, but Tailscale takes over port 22 on the Tailscale address.
Add this section to the policy file, specifying the authorized Linux account:
"ssh": [
{
"action": "accept",
"src": ["group:admins"],
"dst": ["tag:server"],
"users": ["YOUR_USER"]
}
],The tcp:22 rule from step 6 is still required. Then enable the feature on the server. SSH sessions already open on the Tailscale address will freeze: reconnect afterwards.
sudo tailscale set --sshTo go back, use sudo tailscale set --ssh=false.
Add a Server Without a Browser
An authentication key avoids having to open the URL. Create it on the console's Keys page, preferably single-use and with the tag:server tag, then use it on the new server:
sudo tailscale up --auth-key=TAILSCALE_AUTH_KEYTreat this key like a password and do not leave it in a script or in the shell history. Its validity period is 90 days at most.
Update
Update the package with APT:
sudo apt-get update
sudo apt-get install --only-upgrade -y tailscale
tailscale versionYou can also enable automatic updates:
sudo tailscale set --auto-updateRestarting Tailscale interrupts Tailscale SSH sessions and may disrupt sessions that go through the tailnet. Run the update inside tmux or screen if Tailscale is your only way in.
Back Up
The server keeps its identity in /var/lib/tailscale/tailscaled.state. This file contains keys: it does not need to be backed up, because after a reinstallation it is enough to reconnect the machine with sudo tailscale up. Do not copy it to another machine.
The item to keep is your policy file. Keep an up-to-date copy in a private repository or a document vault.
Troubleshooting
Failed to connect to local Tailscale daemon
The service is not started:
sudo systemctl enable --now tailscaled
sudo journalctl -u tailscaled --no-pager -n 50If the logs mention /dev/net/tun, the system has no access to the TUN device. This mostly affects systems running in a container.
Logged out. or Tailscale is stopped.
The machine is disconnected or its key has expired. Run sudo tailscale up again, then disable expiry as described in step 4.
changing settings via 'tailscale up' requires mentioning all non-default flags
tailscale up expects all the settings you have already customized. To change a single setting, use tailscale set, for example sudo tailscale set --hostname=web-1.
The Connection Goes Through a Relay
In tailscale status, the relay mention indicates that traffic does not go directly. The service remains usable, sometimes with lower performance. Analyze the network, then open the optional port if needed:
tailscale netcheck
sudo ufw allow 41641/udpThe ping Works, but the Service Does Not
- Check that a
grantsrule allows this port between these machines. - Check that the service is not listening only on
127.0.0.1:sudo ss -ltnp. - Check the machine's tag in the console.
The Policy File Is Rejected
Read the message displayed by the console. It points to a syntax error, or to a test in the tests section that does not match the rules. A tag must also be declared in tagOwners before it is used.
The Server's DNS Names No Longer Resolve
Tailscale can manage the machine's DNS. If this interferes with your configuration, disable this support. Short names will no longer work on this server: use the 100.x.y.z addresses instead.
sudo tailscale set --accept-dns=falseYou No Longer Have SSH Access
Connect through your fallback access, then temporarily restore the public rule. Fix Tailscale, then remove this rule:
sudo ufw allow 22/tcp
sudo tailscale status
sudo tailscale up