Skip to content

Mosquitto MQTT broker setup — TLS, auth, and persistence on Ubuntu

Standing up Mosquitto as an MQTT broker on Ubuntu — listeners, password auth, TLS via Let's Encrypt, persistent sessions, and the operational defaults an MSP applies.

A fresh Ubuntu box and a directive to “stand up an MQTT broker for the device fleet.” This article is the Mosquitto install we run in production: the package from the Ubuntu archive, password authentication enabled from the first start, TLS on port 8883 with a Let’s Encrypt certificate, persistent sessions on a sane disk path, and the firewall locked to what devices actually need. Mosquitto is the canonical broker for small-to-medium IoT footprints; this is how an MSP makes it operable.

How to verify

Confirm what is running on the standard MQTT ports and what Mosquitto packaging is available:

ss -ltnp | grep -E ':(1883|8883|9001)\b'
apt-cache policy mosquitto mosquitto-clients
dpkg -l | grep -E 'mosquitto'
systemctl status mosquitto 2>/dev/null || echo "no mosquitto unit yet"

MQTT uses 1883 for plaintext, 8883 for TLS, and conventionally 9001 for WebSocket transport. Plaintext on 1883 is fine on a closed segment; 8883 is mandatory across any untrusted network.

What’s happening

MQTT is a publish-subscribe protocol designed for unreliable networks and constrained devices. A client connects to the broker, subscribes to topics (sensors/floor1/temp), publishes to topics, and receives messages routed to its subscriptions. Quality of Service has three levels: 0 (fire and forget), 1 (at least once, with PUBACK), 2 (exactly once, with a four-step handshake). Sessions can be persistent — the broker holds undelivered QoS 1/2 messages for a disconnected client until it reconnects.

Mosquitto is the reference open-source broker. Single binary, written in C, runs everywhere, scales to tens of thousands of concurrent clients on modest hardware. The Ubuntu archive has a reasonably current package; for the latest, the Mosquitto PPA (ppa:mosquitto-dev/mosquitto-ppa) tracks upstream more closely.

Authentication out of the box has three modes:

  • Anonymous — the default before you change anything. Never acceptable in production.
  • Password file — mosquitto_passwd-managed file with bcrypt hashes. Suitable for fleets up to a few thousand devices.
  • External auth plugins — mosquitto-auth-plugin, dynsec, JWT, x509 client certificates. Required at higher scale or for multi-tenant deployments.

Persistence (persistence true) means the broker writes its state — retained messages, subscriptions, in-flight QoS 1/2 messages — to disk on a configured interval. Without persistence, a restart loses retained messages and forces every client to resubscribe.

The procedure

  1. Install Mosquitto and the client tools:

    sudo apt update
    sudo apt install -y mosquitto mosquitto-clients
    mosquitto --help | head -3
    systemctl status mosquitto --no-pager

    The package starts the broker on port 1883, accepting anonymous connections from localhost only. Good for the install moment, not for one minute longer.

  2. Disable anonymous, create a password file:

    sudo mosquitto_passwd -c /etc/mosquitto/passwd app-publisher
    # prompts for password — use `openssl rand -base64 24` and paste
    sudo mosquitto_passwd /etc/mosquitto/passwd ops-subscriber
    sudo chown root:mosquitto /etc/mosquitto/passwd
    sudo chmod 0640 /etc/mosquitto/passwd
  3. Write the main config at /etc/mosquitto/conf.d/01-main.conf:

    persistence true
    persistence_location /var/lib/mosquitto/
    persistence_file mosquitto.db
    autosave_interval 60
    
    log_dest file /var/log/mosquitto/mosquitto.log
    log_dest topic
    log_type error
    log_type warning
    log_type notice
    log_timestamp true
    log_timestamp_format %Y-%m-%dT%H:%M:%S
    
    # plaintext listener — bind to localhost only
    listener 1883 127.0.0.1
    protocol mqtt
    allow_anonymous false
    password_file /etc/mosquitto/passwd
    
    # TLS listener — public
    listener 8883
    protocol mqtt
    allow_anonymous false
    password_file /etc/mosquitto/passwd
    cafile /etc/letsencrypt/live/mqtt.example.net/chain.pem
    certfile /etc/letsencrypt/live/mqtt.example.net/cert.pem
    keyfile /etc/letsencrypt/live/mqtt.example.net/privkey.pem
    tls_version tlsv1.2
  4. Provision the Let’s Encrypt certificate. The standalone challenge needs port 80 free, which usually is not the case on a server hosting other things; use DNS-01 instead:

    sudo apt install -y certbot
    sudo certbot certonly --dns-rfc2136 \
      --dns-rfc2136-credentials /etc/letsencrypt/rfc2136.ini \
      -d mqtt.example.net

    Mosquitto’s user (mosquitto) needs read access to the cert files; the certbot defaults are too restrictive. Either chown the live directory or use the deploy-hook to copy the certs to a directory Mosquitto can read.

  5. Set up cert-renewal reload. Add a deploy hook so Mosquitto picks up renewed certs:

    sudo tee /etc/letsencrypt/renewal-hooks/deploy/mosquitto-reload.sh >/dev/null <<'EOF'
    #!/bin/sh
    systemctl reload mosquitto
    EOF
    sudo chmod +x /etc/letsencrypt/renewal-hooks/deploy/mosquitto-reload.sh
  6. Restart and verify:

    sudo systemctl restart mosquitto
    sudo systemctl status mosquitto --no-pager
    sudo journalctl -u mosquitto -n 30 --no-pager
    ss -ltnp | grep mosquitto

    You should see Mosquitto bound to 127.0.0.1:1883 and 0.0.0.0:8883.

  7. Round-trip a message over TLS:

    # subscribe in one terminal
    mosquitto_sub --capath /etc/ssl/certs/ \
      -h mqtt.example.net -p 8883 -u ops-subscriber -P "PASS" \
      -t 'sensors/#' -v
    
    # publish in another
    mosquitto_pub --capath /etc/ssl/certs/ \
      -h mqtt.example.net -p 8883 -u app-publisher -P "PASS" \
      -t 'sensors/floor1/temp' -m '21.4' -q 1 -r

    The -r flag retains the message; a new subscriber to sensors/floor1/temp immediately receives 21.4 even if it connects after the publish.

  8. Lock the firewall:

    sudo ufw allow 8883/tcp comment 'mqtt-tls'
    sudo ufw deny 1883/tcp comment 'mqtt-plaintext-localhost-only'
    sudo ufw reload

Common pitfalls

  • The Ubuntu package starts Mosquitto with allow_anonymous true on a localhost listener; if you bind to 0.0.0.0 without disabling anonymous, the broker accepts unauthenticated connections from the internet for as long as it takes you to notice.
  • Persistence writes are not immediate; autosave_interval 60 means up to 60 seconds of state can be lost on a crash. For QoS 2 critical-data fleets, lower it.
  • The cafile for Let’s Encrypt is chain.pem, not fullchain.pem — Mosquitto wants only the intermediate certificate, not the leaf again.
  • Client certificate auth (require_certificate true) is dramatically stronger than passwords for IoT fleets, but every device needs an enrollment process; do not adopt it without that infrastructure.
  • WebSocket transport on 9001 (protocol websockets) is a separate listener stanza; do not try to add it to the existing MQTT listener.

Stack Harbor stands up Mosquitto brokers as the MQTT tier for clients with IoT or device-fleet workloads — installs scripted, TLS automated end-to-end, password files under config management, persistence sized to the QoS profile of the traffic, and dashboards that show subscriber counts and message rates per topic prefix. If your MQTT broker has grown from a quick install on a developer box into infrastructure your devices depend on, this is the work covered by our Managed Operations practice.