Skip to content

Installing NATS server — single-node baseline with sane defaults

A clean NATS install on Ubuntu — binary deployment, systemd unit, monitoring port, authentication file, and the operational defaults an MSP runs on day one.

A NATS server is a single static binary, three TCP ports, and a config file. This article is the install we run for a single-node baseline before the topology grows to a cluster or a JetStream-backed stream — get the binary on the box, run it under systemd as an unprivileged user, expose the monitoring port for Prometheus, and gate client access with at least token authentication. The goal is a server that an application team can start using immediately and that an MSP can monitor without surprises.

How to verify

Before installing, confirm the box is clean and you know what is binding to the standard NATS ports:

ss -ltnp | grep -E ':(4222|6222|8222)\b'
which nats-server || true
id nats 2>/dev/null || echo "no nats user yet"
systemctl status nats 2>/dev/null || echo "no nats unit yet"

NATS uses 4222 for client connections, 6222 for the cluster route protocol, and 8222 for the HTTP monitoring endpoint. None should be in use before install.

What’s happening

NATS is a publish-subscribe messaging system written in Go. The server (nats-server) is a single binary with no external dependencies — no JVM, no Erlang, no database. Subjects are dot-delimited routing keys (orders.eu.fr.paris); publishers send to a subject and any subscriber matching the subject (with wildcards * for one token and > for tail) receives the message. By default messages are fire-and-forget — no persistence, no delivery guarantees beyond “best effort while subscriber is connected.”

Persistence and at-least-once delivery come from JetStream, which is an add-on built into the same binary but disabled until you turn it on. For this article we are installing the core server only; JetStream gets its own article (NATS JetStream streams).

Authentication has three modes:

  • No auth — fine for a localhost dev server, never for production.
  • Token or user/password auth — simple, in the config file, fine for internal-only deployments.
  • NKeys and JWT — the cryptographic credential system NATS recommends for multi-tenant deployments. Out of scope for “install basics” but on the roadmap once you have more than one application team.

The monitoring port (8222) exposes a read-only JSON API: /varz for server stats, /connz for connections, /subsz for subscriptions, /healthz for liveness. Wire Prometheus at this port via the NATS exporter and you have queue-style observability for a pub-sub system.

The procedure

  1. Create the service user and directories. Never run NATS as root or as a personal user:

    sudo useradd --system --shell /usr/sbin/nologin --home /var/lib/nats nats
    sudo mkdir -p /etc/nats /var/lib/nats /var/log/nats
    sudo chown -R nats:nats /var/lib/nats /var/log/nats
  2. Install the binary from the official release. NATS publishes signed tarballs on GitHub; pin a specific version, do not use latest:

    NATS_VER=2.10.20
    curl -fsSL "https://github.com/nats-io/nats-server/releases/download/v${NATS_VER}/nats-server-v${NATS_VER}-linux-amd64.tar.gz" \
      | sudo tar -xz -C /tmp
    sudo install -m 0755 /tmp/nats-server-v${NATS_VER}-linux-amd64/nats-server /usr/local/bin/nats-server
    nats-server --version
  3. Write the server config at /etc/nats/nats-server.conf:

    server_name: "nats-01"
    listen: "0.0.0.0:4222"
    
    http: "0.0.0.0:8222"
    
    log_file: "/var/log/nats/nats-server.log"
    logtime: true
    debug: false
    trace: false
    
    max_payload: 8MB
    max_connections: 4096
    
    authorization {
      users = [
        { user: "app", password: "REPLACE_WITH_STRONG_PASS",
          permissions = { publish = ">", subscribe = ">" } }
        { user: "ops", password: "REPLACE_WITH_STRONG_PASS",
          permissions = { publish = ["_INBOX.>"], subscribe = [">"] } }
      ]
    }

    The app user can publish and subscribe to anything; the ops user can subscribe to anything (for monitoring tools) but only publish to inbox replies. Replace passwords with output from openssl rand -base64 24.

  4. Set ownership and permissions on the config:

    sudo chown root:nats /etc/nats/nats-server.conf
    sudo chmod 0640 /etc/nats/nats-server.conf
  5. Write the systemd unit at /etc/systemd/system/nats.service:

    [Unit]
    Description=NATS server
    Documentation=https://docs.nats.io
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=simple
    User=nats
    Group=nats
    ExecStart=/usr/local/bin/nats-server -c /etc/nats/nats-server.conf
    ExecReload=/bin/kill -HUP $MAINPID
    Restart=on-failure
    RestartSec=5s
    LimitNOFILE=65536
    ProtectSystem=full
    ProtectHome=true
    NoNewPrivileges=true
    
    [Install]
    WantedBy=multi-user.target
  6. Enable and start the service:

    sudo systemctl daemon-reload
    sudo systemctl enable --now nats
    sudo systemctl status nats --no-pager
    journalctl -u nats -n 50 --no-pager
  7. Smoke-test pub-sub with the nats CLI. Install it once (brew install nats-io/nats-tools/nats or grab the static binary):

    # in one terminal
    nats --server nats://app:PASS@127.0.0.1:4222 sub "orders.>"
    
    # in another terminal
    nats --server nats://app:PASS@127.0.0.1:4222 pub orders.eu.fr "hello"

    The subscriber should print the message immediately. If not, the auth user has wrong permissions, or you have a firewall blocking 4222.

  8. Hit the monitoring port to confirm it is up:

    curl -s http://127.0.0.1:8222/varz | head -c 400
    curl -s http://127.0.0.1:8222/healthz

    healthz returns {"status":"ok"} when the server is ready. This is the endpoint your load balancer or platform should health-check against.

Common pitfalls

  • The monitoring port (8222) is unauthenticated by design; never expose it to the internet. Bind it to localhost or to a management VLAN, and put a reverse proxy with auth in front if you need remote access.
  • max_payload defaults to 1 MiB; if your application sends larger messages, increase it on the server and set the matching client option, otherwise the broker silently drops them.
  • The nats CLI is not the server; treat them as two separate releases. The CLI talks any 2.x server but its features track its own version.
  • Without auth configured, NATS happily accepts any TCP connection on 4222 — every operator’s first incident is exactly this. The authorization block is not optional in production.
  • A kill -HUP reloads the config file but does not restart the server; auth users update without disconnect. Use this for routine credential rotation.

Stack Harbor stands up NATS as a low-overhead messaging fabric for clients building event-driven services — single-binary deploys, monitoring wired into the same Prometheus stack as the rest of the platform, JetStream turned on only when an application team actually needs persistence. If your NATS install grew from a quick docker-compose to something the business depends on, this is the work covered by our Managed Operations practice.