On this page
- What does Mosquitto need from the server?
- How do you install Mosquitto on Ubuntu or Debian?
- How do you add user accounts and topic rules?
- How do you enable TLS on port 8883?
- Which firewall rules does an MQTT server need?
- How do you test logins and ACLs from outside?
- How do you bridge a local broker to the central one?
- How do you keep the broker healthy over time?
- Questions
What does Mosquitto need from the server?
Very little: Mosquitto is a small C program that handles thousands of idle device connections in a few dozen megabytes of RAM, so the smallest plans are enough for a home or fleet broker. What matters more is a fixed public address and a DNS name such as mqtt.example.net pointing at it, because devices hard-code the host and TLS certificates are issued for the name. Set the A and AAAA records first (the DNS records tutorial shows how) and log in with a sudo user.
Version 2 changed one default that surprises people coming from old guides: without any listener in the configuration, the broker only accepts connections from the local machine. Every remote listener has to be declared, and remote access without a password is refused unless you explicitly allow it.
How do you install Mosquitto on Ubuntu or Debian?
Both distributions carry Mosquitto 2 in their main archive, which is the simplest route and receives security updates through apt.
sudo apt update
sudo apt install -y mosquitto mosquitto-clients
apt policy mosquitto
mosquitto -h | head -n 1
sudo systemctl enable --now mosquitto
sudo ss -ltnp | grep mosquittoAt this point the broker listens on 127.0.0.1 and ::1, port 1883. On Ubuntu you may add the project PPA for a newer 2.x release with sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa followed by an upgrade; on Debian the archive package is the usual choice. A quick local round trip proves the service is alive:
mosquitto_sub -h 127.0.0.1 -t 'check/#' -v -C 1 &
sleep 1
mosquitto_pub -h 127.0.0.1 -t check/hello -m 'ping'How do you add user accounts and topic rules?
Create a password file with mosquitto_passwd, then write an ACL file that says which user may read or write which topics. Passwords are stored as salted hashes, never in clear text.
sudo mosquitto_passwd -c /etc/mosquitto/passwd dashboard
sudo mosquitto_passwd /etc/mosquitto/passwd sensor-kitchen
sudo mosquitto_passwd /etc/mosquitto/passwd sensor-garage
sudo chown mosquitto:mosquitto /etc/mosquitto/passwd
sudo chmod 600 /etc/mosquitto/passwd
sudo tee /etc/mosquitto/acl >/dev/null <<'EOF'
# the dashboard reads everything under home/
user dashboard
topic read home/#
# each sensor writes only below its own name
pattern write home/%u/#
EOF
sudo chown mosquitto:mosquitto /etc/mosquitto/acl
sudo chmod 600 /etc/mosquitto/aclUse -c only for the first user, because it creates a fresh file and would wipe the others. The %u pattern expands to the connecting username, so one rule covers every sensor.
How do you enable TLS on port 8883?
Issue a certificate for the broker name, copy it where the mosquitto user can read it, and declare a TLS listener next to a local-only plain listener. Let's Encrypt works well here; the Certbot guide shows the standalone method, which suits a server that runs no web server.
sudo install -d -o mosquitto -g mosquitto -m 750 /etc/mosquitto/certs
sudo cp /etc/letsencrypt/live/mqtt.example.net/fullchain.pem /etc/mosquitto/certs/
sudo cp /etc/letsencrypt/live/mqtt.example.net/privkey.pem /etc/mosquitto/certs/
sudo chown mosquitto:mosquitto /etc/mosquitto/certs/*.pem
sudo tee /etc/mosquitto/conf.d/broker.conf >/dev/null <<'EOF'
per_listener_settings false
allow_anonymous false
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl
listener 1883 127.0.0.1
listener 8883
certfile /etc/mosquitto/certs/fullchain.pem
keyfile /etc/mosquitto/certs/privkey.pem
tls_version tlsv1.2
EOF
sudo systemctl restart mosquitto
sudo journalctl -u mosquitto -n 20 --no-pagerThe tls_version line sets the minimum, so TLS 1.3 clients still connect. To refresh files after each renewal, save a deploy hook in /etc/letsencrypt/renewal-hooks/deploy/mosquitto.sh that repeats the two copies and the chown, then runs systemctl reload mosquitto.
Which firewall rules does an MQTT server need?
Open SSH and 8883 only. Leave 1883 closed: it is bound to localhost anyway, and plain MQTT would expose passwords on the wire. Details on rule order live in the UFW guide.
sudo ufw allow 22/tcp
sudo ufw allow 8883/tcp
sudo ufw enable
sudo ufw status verboseIf browser dashboards need MQTT over WebSockets, add listener 8083 127.0.0.1 with protocol websockets and publish it through a reverse proxy that terminates HTTPS, such as the setup in the Caddy tutorial, instead of opening another raw port.
How do you test logins and ACLs from outside?
Subscribe as the dashboard user and publish as one sensor from another machine, pointing the clients at the system CA bundle so they verify the certificate.
# terminal 1
mosquitto_sub -h mqtt.example.net -p 8883 --capath /etc/ssl/certs -u dashboard -P 'dash-secret' -t 'home/#' -v
# terminal 2
mosquitto_pub -h mqtt.example.net -p 8883 --capath /etc/ssl/certs -u sensor-kitchen -P 'kitchen-secret' -t home/sensor-kitchen/temp -m 21.5
# this one is accepted by the broker but silently dropped by the ACL
mosquitto_pub -h mqtt.example.net -p 8883 --capath /etc/ssl/certs -u sensor-kitchen -P 'kitchen-secret' -t home/sensor-garage/temp -m 99The subscriber should show the kitchen reading and nothing from the second publish. A wrong password ends with Connection error: Not authorized, and a certificate problem shows up as a TLS handshake error before authentication starts.
How do you bridge a local broker to the central one?
Define a bridge on the edge broker that logs in to the central server over TLS and forwards chosen topics. The central side needs only a normal user with the right ACL; the bridge is just another client.
# /etc/mosquitto/conf.d/bridge.conf on the edge broker
connection edge-to-central
address mqtt.example.net:8883
bridge_capath /etc/ssl/certs
remote_username edge-site1
remote_password Edge-Bridge-Secret
remote_clientid edge-site1
topic home/# out 1
topic commands/site1/# in 1
cleansession falseThe direction words decide the flow: out sends local messages up, in brings remote ones down, and the trailing number is the QoS. On the central broker, add an ACL block for edge-site1 that allows writing home/# and reading commands/site1/#.
How do you keep the broker healthy over time?
Mosquitto updates arrive with the regular system updates, so automatic security updates cover most maintenance. Retained messages and queued QoS 1 or 2 messages for persistent sessions are saved in /var/lib/mosquitto/mosquitto.db; include that file and /etc/mosquitto in your backups.
After adding a user or editing the ACL, run sudo systemctl reload mosquitto: the broker rereads both files without dropping connected devices. The broker publishes its own statistics under $SYS/#; subscribe to it as a trusted local user to see client counts and message rates. If the service refuses to start, sudo mosquitto -c /etc/mosquitto/mosquitto.conf -v runs it in the foreground and prints the exact line it dislikes, and remember to stop that foreground copy before starting the service again.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about Mosquitto MQTT broker
Which MQTT versions does Mosquitto 2 support?
MQTT 3.1, 3.1.1 and 5.0 on the same listener. Clients pick the version when they connect.
How many devices can one small server handle?
It depends on message rates rather than connection count. Idle connections cost little memory, so start small and watch CPU and RAM with the tools in the usage tutorial.
Can devices that cannot verify certificates still connect?
Give them a separate listener only if you must, ideally reachable through a VPN such as WireGuard, and never as plain MQTT on the open internet.
How do I remove a user?
Run sudo mosquitto_passwd -D /etc/mosquitto/passwd sensor-garage, delete any ACL lines for that name and reload the service.
Do retained messages survive a restart?
Yes, when persistence true is set, which the Debian and Ubuntu packages do in the main config file.