# Keep a tunnel running

> A tunnel closes when its SSH connection does: Wi-Fi drops, your laptop sleeps, or a free tunnel reaches its time limit. Connect as `stable@` and reconnect automatically, and your URL comes back on its own, the same as before. This guide covers a one-line loop, `autossh`, and running the tunnel as a background service on macOS and Linux.

## 1. Reconnect in a loop

The simplest way: run the command again whenever it ends. On macOS and Linux:

```bash
while true; do
  ssh -t -o ServerAliveInterval=30 -o ServerAliveCountMax=3 \
    -o ExitOnForwardFailure=yes \
    -R 80:localhost:3000 stable@proxy.tunnl.gg
  sleep 5
done
```

On Windows, in PowerShell:

```powershell
while ($true) {
  ssh -t -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -R 80:localhost:3000 stable@proxy.tunnl.gg
  Start-Sleep 5
}
```

Because you connect as `stable@`, every reconnect gets the same URL, tied to your SSH key. To stop, press Ctrl+C to close the tunnel, then Ctrl+C again within 5 seconds to stop the loop.

What the options do:

| Option | Why |
|--------|-----|
| `ServerAliveInterval=30`, `ServerAliveCountMax=3` | Checks the connection every 30 seconds and gives up after 3 missed replies, so a dead connection (after sleep, or a network change) is noticed in about 90 seconds instead of hanging. |
| `ExitOnForwardFailure=yes` | Exits if the tunnel can't be set up, so the loop tries again instead of sitting on a connection without a tunnel. |
| `sleep 5` | Waits a little between attempts. Each IP can open 10 connections a minute, and a loop without a pause would hit that. |

## 2. Or use autossh

[autossh](https://www.harding.motd.ca/autossh/) restarts SSH for you. Install it with `brew install autossh` or `sudo apt install autossh`, then:

```bash
autossh -M 0 -t -o ServerAliveInterval=30 -o ServerAliveCountMax=3 \
  -o ExitOnForwardFailure=yes \
  -R 80:localhost:3000 stable@proxy.tunnl.gg
```

`-M 0` turns off autossh's own monitoring port and relies on the `ServerAlive` options instead.

## 3. Run it in the background

To keep a tunnel up without a terminal open, for a home server, a Raspberry Pi or a [Home Assistant](/guides/home-assistant-remote-access) box, run it as a service. Use `-T` instead of `-t` there: there's no terminal to attach to.

Before you set up a service, connect once by hand, so SSH asks you to trust the tunnl.gg host key and saves it. A service can't answer that question. You can also add the [tunnl.gg host key](/docs#verify-the-host-key) to `~/.ssh/known_hosts` yourself.

The service also needs a key it can use without you: one without a passphrase, or one already loaded in your SSH agent. Your URL comes from the key, so the service gets the same URL as your terminal only if it uses the same key.

### macOS: launchd

Save this as `gg.tunnl.tunnel.plist` in `~/Library/LaunchAgents`, with your user name in the paths:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>gg.tunnl.tunnel</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/bin/ssh</string>
    <string>-T</string>
    <string>-o</string><string>ServerAliveInterval=30</string>
    <string>-o</string><string>ServerAliveCountMax=3</string>
    <string>-o</string><string>ExitOnForwardFailure=yes</string>
    <string>-R</string><string>80:localhost:3000</string>
    <string>stable@proxy.tunnl.gg</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <true/>
  <key>StandardOutPath</key>
  <string>/Users/you/Library/Logs/tunnl.log</string>
  <key>StandardErrorPath</key>
  <string>/Users/you/Library/Logs/tunnl.log</string>
</dict>
</plist>
```

Start it now and at every login:

```bash
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/gg.tunnl.tunnel.plist
```

The URL is in the log: `grep URL ~/Library/Logs/tunnl.log`. To stop it, run `launchctl bootout gui/$(id -u)/gg.tunnl.tunnel`.

### Linux: systemd

Save this as `tunnl.service` in `~/.config/systemd/user`:

```ini
[Unit]
Description=tunnl.gg tunnel

[Service]
ExecStart=/usr/bin/ssh -T -o ServerAliveInterval=30 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -R 80:localhost:3000 stable@proxy.tunnl.gg
Restart=always
RestartSec=5

[Install]
WantedBy=default.target
```

Start it now and at every login:

```bash
systemctl --user daemon-reload
systemctl --user enable --now tunnl
```

The URL is in the log: `journalctl --user -u tunnl | grep URL`. User services stop when you log out; to keep this one running on a server where nobody is logged in, run `sudo loginctl enable-linger $USER` once.

## Things to know

- **Free tunnels have time limits.** A free tunnel closes after 2 hours without traffic, or after 24 hours at most. With any of the setups above it comes straight back, with the same URL. Reconnecting doesn't reset the daily transfer of 5 GB, though: past it, requests get HTTP 429 until 00:00 UTC. [Pro](/pro) tunnels have no time limits, and can use a name you choose, like `myapp.tunnl.gg`.
- **One live tunnel per key.** A new connection with the same key replaces the old one, which is what makes reconnecting after sleep work right away. It also means two machines running a loop with the same key take the URL from each other. Give each machine its own key, registered to your account: see [More than one stable URL](/stable-urls#more-than-one-stable-url).
- **Requests fail while it's reconnecting.** For the few seconds between connections, visitors and webhooks get an error. Most webhook providers retry, and their retries reach you once the tunnel is back. See [Test webhooks locally](/guides/test-webhooks-locally).
- **Options still work.** Add `host=localhost` and other [options](/docs) after the host in any of these commands.

## Related

- [Stable URLs](/stable-urls)
- [Test webhooks locally](/guides/test-webhooks-locally)
- [Share a local dev server](/guides/share-localhost-dev-server)
- [tunnl.gg documentation](/docs)
