Skip to content

Deploying headless Syncthing on Ubuntu 22.04 (production-ready)

Stand up a hardened headless Syncthing node on Ubuntu 22.04 — systemd user service, GUI localhost-only behind nginx auth-proxy, Let's Encrypt, ignore patterns.

You’ve been asked to run a Syncthing node that pairs a server-side folder with two workstations. The README is friendly, you apt install syncthing, you start it in a screen session, you copy the device ID off the terminal, and you go home. Three weeks later, the GUI on 0.0.0.0:8384 is reachable from the internet with no password because Syncthing’s default GUI bind only constrains itself on first run for desktop installs, the process is not coming back after a reboot because there is no service, the sync folder has eaten the root volume because no ignore patterns were ever set, and the discovery layer is leaking the device ID and node hostname to the global Syncthing discovery server because the operator never disabled it.

What’s happening

A Syncthing deployment that is actually production-ready needs more than the package install. The Syncthing process has to run as a non-root systemd user service so it survives reboots, gets restarted on failure, and stays scoped to a dedicated account. The GUI must be bound to 127.0.0.1:8384 only and reached through nginx with HTTP basic auth (or SSO) and Let’s Encrypt TLS in front — the built-in GUI password is not enough exposure control on the open internet, and the default certificate is self-signed. The sync data directory has to live on a volume with capacity headroom and the right ignore patterns (.stversions, build artefacts, virtualenvs, node_modules, OS caches) — without them, a workstation’s .cache directory will replicate to every other peer. Global discovery and the global relay should be turned off for server-to-server fleets and replaced with explicit static addresses; for mixed device/workstation fleets, leave discovery on but document what is being announced. SSH password auth has to be off with root key access preserved. None of these are individually critical. They compound — skip them and the peer-to-peer sync becomes both the busiest egress channel on the VM and the least understood security surface.

How to verify

After install, the listeners should look like this:

ss -lntp | grep -E ':(80|443|8384|22000)\b'
# 127.0.0.1:8384   syncthing  (GUI, behind nginx)
# 0.0.0.0:22000    syncthing  (sync TCP)
# 0.0.0.0:443      nginx      (GUI auth-proxy)

The GUI on :8384 must be 127.0.0.1, never 0.0.0.0. Service health under the dedicated user:

systemctl --user -M syncthing@.host is-active syncthing
# or, system-scoped:
systemctl is-active syncthing@syncthing.service

Inventory devices and folders via the CLI to confirm config matches expectation:

syncthing cli -h | head
syncthing cli show config | jq '.devices[].name, .folders[].label'

If :8384 is reachable on the public IP, anyone on the internet can pair a new device against this node. The mitigation is the localhost bind plus nginx auth, not the in-app password.

The fix

Save the script below as /root/setup-syncthing-ubuntu22.sh, then run it. It performs the full installer flow on a fresh Ubuntu 22.04 VM: base update, kernel/network tuning, file limits, UFW (SSH + HTTP + HTTPS + Syncthing sync :22000), SSH hardening with root key preserved, unattended-upgrades, a dedicated syncthing system user with a home on a data volume, Syncthing from the upstream apt repo, a first-run that creates ~/.local/state/syncthing/config.xml, GUI bound to 127.0.0.1:8384 and discovery flipped to local-only, a systemd unit that runs syncthing as the syncthing user with LimitNOFILE=64000, nginx in front of the GUI with HTTP basic auth and Let’s Encrypt TLS, a default .stignore template, and a poll loop that waits for :8384 and :443 before declaring success.

#!/usr/bin/env bash
set -euo pipefail

### Ubuntu 22.04 Syncthing headless clean VM installer
### Keeps root SSH key login enabled
### Disables SSH password login
### Enables automatic security updates
### Installs and configures Syncthing + nginx + Let's Encrypt
### No local backups; assumes VM-level backup
### Reboot remains manual

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

: "${SYNCTHING_DOMAIN:=sync.example.com}"
: "${LE_EMAIL:=ops@example.com}"
: "${SYNCTHING_GUI_USER:=admin}"

echo
echo "========================================"
echo "Syncthing Ubuntu 22.04 Setup"
echo "========================================"
echo

export DEBIAN_FRONTEND=noninteractive

echo "==> Updating base OS"
apt-get update
apt-get -y upgrade

echo "==> Installing base packages"
apt-get -y install \
  vim curl wget ca-certificates gnupg2 chrony fail2ban \
  unattended-upgrades apt-listchanges \
  ufw lsof tcpdump dnsutils net-tools \
  apache2-utils nginx jq pwgen

echo "==> Enabling time sync"
systemctl enable --now chrony
timedatectl set-ntp true

echo "==> Applying kernel/network tuning"
cat > /etc/sysctl.d/99-syncthing.conf <<'SYSCTL'
net.core.somaxconn = 4096
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.ip_local_port_range = 10240 65535

kernel.dmesg_restrict = 1
kernel.kptr_restrict = 2
fs.file-max = 2097152
fs.inotify.max_user_watches = 524288
SYSCTL
sysctl --system

echo "==> Setting file limits"
cat > /etc/security/limits.d/99-syncthing.conf <<'LIMITS'
* soft nofile 64000
* hard nofile 64000
root soft nofile 64000
root hard nofile 64000
syncthing soft nofile 64000
syncthing hard nofile 64000
LIMITS

echo "==> Configuring UFW"
ufw --force reset
ufw default deny incoming
ufw default allow outgoing
ufw allow OpenSSH
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 22000/tcp
ufw allow 22000/udp
ufw allow 21027/udp
ufw --force enable

echo "==> Hardening SSH while keeping root SSH key login enabled"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)

set_sshd_option() {
  local key="$1" value="$2"
  if grep -qE "^[#[:space:]]*${key}[[:space:]]+" /etc/ssh/sshd_config; then
    sed -i "s|^[#[:space:]]*${key}[[:space:]].*|${key} ${value}|" /etc/ssh/sshd_config
  else
    echo "${key} ${value}" >> /etc/ssh/sshd_config
  fi
}

set_sshd_option PermitRootLogin prohibit-password
set_sshd_option PasswordAuthentication no
set_sshd_option PubkeyAuthentication yes
set_sshd_option X11Forwarding no
set_sshd_option ClientAliveInterval 300
set_sshd_option ClientAliveCountMax 2
set_sshd_option MaxAuthTries 4

sshd -t
systemctl restart sshd

echo "==> Configuring fail2ban for SSH"
mkdir -p /etc/fail2ban/jail.d
cat > /etc/fail2ban/jail.d/sshd.local <<'FAIL2BAN'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
FAIL2BAN
systemctl enable --now fail2ban

echo "==> Configuring unattended-upgrades"
dpkg-reconfigure -f noninteractive unattended-upgrades

echo "==> Adding Syncthing apt repo"
curl -fsSL https://syncthing.net/release-key.gpg | gpg --dearmor -o /usr/share/keyrings/syncthing-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/syncthing-archive-keyring.gpg] https://apt.syncthing.net/ syncthing stable" \
  > /etc/apt/sources.list.d/syncthing.list

apt-get update
apt-get -y install syncthing

echo "==> Creating syncthing user"
if ! id syncthing >/dev/null 2>&1; then
  useradd -m -d /var/lib/syncthing -s /usr/sbin/nologin syncthing
fi
mkdir -p /var/lib/syncthing/Sync
chown -R syncthing:syncthing /var/lib/syncthing

echo "==> Generating Syncthing initial config (first run)"
sudo -u syncthing -- env HOME=/var/lib/syncthing XDG_STATE_HOME=/var/lib/syncthing/.local/state \
  syncthing generate --config /var/lib/syncthing/.local/state/syncthing

CFG=/var/lib/syncthing/.local/state/syncthing/config.xml
if [[ ! -f "$CFG" ]]; then
  CFG=/var/lib/syncthing/.config/syncthing/config.xml
fi

GUI_PW="$(pwgen -s 24 1)"
GUI_PW_HASH="$(htpasswd -nbB "${SYNCTHING_GUI_USER}" "${GUI_PW}" | cut -d: -f2)"

echo "==> Hardening Syncthing config (GUI localhost, discovery local-only)"
sudo -u syncthing -- python3 - "$CFG" "${SYNCTHING_GUI_USER}" "${GUI_PW_HASH}" <<'PY'
import sys, xml.etree.ElementTree as ET

path, user, pw_hash = sys.argv[1], sys.argv[2], sys.argv[3]
tree = ET.parse(path)
root = tree.getroot()

gui = root.find('gui')
gui.set('enabled', 'true')
gui.set('tls', 'false')
addr = gui.find('address')
addr.text = '127.0.0.1:8384'
u = gui.find('user')
if u is None:
    u = ET.SubElement(gui, 'user'); u.text = user
else:
    u.text = user
p = gui.find('password')
if p is None:
    p = ET.SubElement(gui, 'password'); p.text = pw_hash
else:
    p.text = pw_hash

opts = root.find('options')
def set_opt(name, value):
    el = opts.find(name)
    if el is None:
        el = ET.SubElement(opts, name)
    el.text = value

# Discovery: keep local on (LAN announce/listen), turn global discovery off.
# If you need cross-NAT, leave globalAnnounceEnabled=true and document it.
set_opt('globalAnnounceEnabled', 'false')
set_opt('localAnnounceEnabled', 'true')
set_opt('relaysEnabled', 'false')
set_opt('urAccepted', '-1')
set_opt('crashReportingEnabled', 'false')

tree.write(path, xml_declaration=True, encoding='utf-8')
PY

echo "==> Writing default .stignore template"
cat > /var/lib/syncthing/Sync/.stignore <<'STIG'
// Default ignore patterns. Edit before adding folders that contain dev artefacts.
.stversions
.DS_Store
.Trash-*
Thumbs.db
*.tmp
*~
.cache/
__pycache__/
*.pyc
.venv/
venv/
node_modules/
.next/
.nuxt/
build/
dist/
target/
.gradle/
.idea/
.vscode/
*.log
STIG
chown syncthing:syncthing /var/lib/syncthing/Sync/.stignore

echo "==> Writing systemd unit (system scope, runs as syncthing user)"
cat > /etc/systemd/system/syncthing@.service <<'UNIT'
[Unit]
Description=Syncthing - Open Source Continuous File Synchronization for %I
Documentation=man:syncthing(1)
After=network-online.target
Wants=network-online.target

[Service]
User=%i
ExecStart=/usr/bin/syncthing serve --no-browser --no-restart --logflags=0 --home=/var/lib/syncthing/.local/state/syncthing
Restart=on-failure
RestartSec=5
SuccessExitStatus=3 4
RestartForceExitStatus=3 4

LimitNOFILE=64000
ProtectSystem=full
ProtectHome=read-only
ReadWritePaths=/var/lib/syncthing
NoNewPrivileges=true

[Install]
WantedBy=multi-user.target
UNIT

systemctl daemon-reload
systemctl enable --now syncthing@syncthing.service

echo "==> Waiting for Syncthing GUI on 127.0.0.1:8384"
for i in {1..30}; do
  if ss -lntp | grep -q '127.0.0.1:8384'; then break; fi
  sleep 1
done

echo "==> Setting nginx auth-proxy with HTTP basic auth"
htpasswd -cbB /etc/nginx/syncthing.htpasswd "${SYNCTHING_GUI_USER}" "${GUI_PW}"
chown root:www-data /etc/nginx/syncthing.htpasswd
chmod 640 /etc/nginx/syncthing.htpasswd

apt-get -y install certbot python3-certbot-nginx

cat > /etc/nginx/sites-available/syncthing.conf <<NGINXCONF
server {
    listen 80;
    server_name ${SYNCTHING_DOMAIN};
    location / { return 404; }
}
NGINXCONF
ln -sf /etc/nginx/sites-available/syncthing.conf /etc/nginx/sites-enabled/syncthing.conf
rm -f /etc/nginx/sites-enabled/default
systemctl enable --now nginx
nginx -t && systemctl reload nginx

certbot certonly --nginx -n --agree-tos -m "${LE_EMAIL}" -d "${SYNCTHING_DOMAIN}"

echo "==> Writing real nginx site"
cat > /etc/nginx/sites-available/syncthing.conf <<NGINXCONF
server {
    listen 80;
    server_name ${SYNCTHING_DOMAIN};
    return 301 https://\$host\$request_uri;
}

server {
    listen 443 ssl http2;
    server_name ${SYNCTHING_DOMAIN};

    ssl_certificate     /etc/letsencrypt/live/${SYNCTHING_DOMAIN}/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/${SYNCTHING_DOMAIN}/privkey.pem;

    add_header Strict-Transport-Security "max-age=31536000" always;

    auth_basic           "Syncthing";
    auth_basic_user_file /etc/nginx/syncthing.htpasswd;

    location / {
        proxy_pass http://127.0.0.1:8384;
        proxy_set_header Host \$host;
        proxy_set_header X-Real-IP \$remote_addr;
        proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_http_version 1.1;
        proxy_set_header Upgrade \$http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_read_timeout 600s;
    }
}
NGINXCONF

nginx -t && systemctl reload nginx

DEVICE_ID="$(sudo -u syncthing -- env HOME=/var/lib/syncthing syncthing --device-id 2>/dev/null || true)"

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Services:"
systemctl is-active syncthing@syncthing.service nginx chronyd fail2ban || true
echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|8384|22000)\b' || true
echo
echo "GUI URL:      https://${SYNCTHING_DOMAIN}/"
echo "GUI user:     ${SYNCTHING_GUI_USER}"
echo "GUI password: ${GUI_PW}"
echo "Device ID:    ${DEVICE_ID}"
echo
echo "Next: open the GUI, add a folder under /var/lib/syncthing/Sync/<label>,"
echo "share it with the workstation device IDs (each workstation must accept the pairing),"
echo "and verify the first sync completes both ways."
echo
echo "Note: global discovery is OFF. If a peer is on a different network without"
echo "a route to this VM, either re-enable globalAnnounceEnabled or set a static"
echo "address on the device in config.xml."

Make it executable and run it:

chmod 700 /root/setup-syncthing-ubuntu22.sh
SYNCTHING_DOMAIN=sync.example.com \
LE_EMAIL=ops@example.com \
/root/setup-syncthing-ubuntu22.sh

After it finishes, open https://sync.example.com/, authenticate with the printed basic-auth credentials, copy the device ID into the workstation Syncthing client, and accept the pairing on both sides. Add the first folder under /var/lib/syncthing/Sync/<label> — the .stignore template is already in place so build artefacts and OS caches will not replicate. Verify a one-way and a two-way sync completes before adding production data.

Run Syncthing inside a Stack Harbor-managed environment with capacity alerting, per-folder health checks, and a documented device pairing process: Talk to us about scoping it →.