Install a NATS Server on Debian/Ubuntu

refer to: Installing, running a NATS Server

Create the user nats

sudo useradd nats -r -s /sbin/nologin


Download NATS binary

curl -fsSL https://binaries.nats.dev/nats-io/nats-server/v2@v2.11.6 | sh


Add NATS to /usr/local/sbin

sudo mv nats-server /usr/local/sbin/


Add NATS to systemd

  1. Create /etc/nats-server.conf. No need to add content to it, since it is only there for customization and overriding the default behavior.
  2. Create /etc/systemd/system/nats-server.service and add the following content to it.
[Unit]
Description=NATS Server
After=network-online.target ntp.service

[Service]
PrivateTmp=true
Type=simple
ExecStart=/usr/local/sbin/nats-server -c /etc/nats-server.conf
ExecReload=/bin/kill -s HUP $MAINPID

# The nats-server uses SIGUSR2 to trigger Lame Duck Mode (LDM) shutdown
# https://docs.nats.io/running-a-nats-service/nats_admin/lame_duck_mode
ExecStop=/bin/kill -s SIGUSR2 $MAINPID
TimeoutStopSec=150

Restart=on-failure

User=nats
Group=nats

[Install]
WantedBy=multi-user.target


Run the NATS Server

sudo systemctl daemon-reload
sudo systemctl start nats-server


Clustering

Clustering Configuration

Node1

# Client Connection Authentication
authorization {
    user:     nats
    password: natsClientPassw01
    timeout:  1
}

# Cluster definition
cluster {
    name: "nats-cluster"
    listen: 0.0.0.0:6222

    # Route Connection Authentication
    authorization {
        user:     route_user
        password: route_pass
        timeout:  1
    }

    routes = [
        nats-route://route_user:route_pass@node2:6222,
        nats-route://route_user:route_pass@node3:6222
    ]    
}

Node2

# ...

# Cluster definition
cluster {
    # ...

    routes = [
        nats-route://route_user:route_pass@node1:6222,
        nats-route://route_user:route_pass@node3:6222
    ]    
}

Node3

# ...

# Cluster definition
cluster {
    # ...

    routes = [
        nats-route://route_user:route_pass@node1:6222,
        nats-route://route_user:route_pass@node2:6222
    ]    
}
Last modified: February 6, 2026

Comments

Write a Reply or Comment

Your email address will not be published.