Eclipse Mosquitto is a lightweight, open-source broker for MQTT. MQTT is a messaging protocol that allows sensors, microcontrollers, smart home hubs, and custom programs to exchange data: a device publishes a message under a topic such as wohnzimmer/temperatur, and every client subscribed to that topic receives it immediately. The broker in the middle accepts all messages and distributes them. Mosquitto is one of the best-known MQTT brokers, requires very few resources, and is supported by Home Assistant, ESPHome, Tasmota, Shelly, and Zigbee2MQTT, among others. In this guide, we’ll show you two ways to get started: the easy way with the One-Click App and manual installation with Docker Compose. Afterward, you’ll connect Home Assistant and your devices.
An MQTT broker often runs on a Raspberry Pi on your home network. Mosquitto is a good fit on a VPS if:
- Devices at multiple locations need to send data to a shared broker, for example, sensors at your vacation home, office, and home.
- You already run Home Assistant on a VPS and want to run the broker alongside it instead of exposing it from your home network.
- You develop your own projects, for example, with ESP32, Python, or Node.js, and need a broker that is available around the clock at a fixed address.
- You don’t want to set up port forwarding on your router. The devices connect to the VPS from inside your network, so you don’t need to open access to your home network.
An internet-hosted broker is less suitable if all your devices are on the same home network and the network should not connect to the outside world. In that case, a broker on the local network is simpler and faster. A broker on a VPS is accessible from the internet, so it must always use a password and, ideally, encryption. If you don’t need device messaging but need queues for background tasks, RabbitMQ is a better choice.
You can install Mosquitto in a few clicks through the One-Click Apps on your VPS. A username and password for the broker are generated automatically, anonymous connections are disabled, and stored messages persist through a restart. Your devices can reach the broker in two ways: via MQTT at your VPS IP address on port 1883, and via encrypted MQTT over WebSocket (wss://) using the app’s domain on port 443.
- A Linux VPS (Debian or Ubuntu) with One-Click Apps set up. For instructions, see Set up One-Click Apps.
- Mosquitto itself needs only a few megabytes of RAM. A small VPS is enough, even if other apps are already running on it.
- Open your VPS in the ZapX dashboard and go to the One-Click Apps tab.
- Search the app catalog for Mosquitto and click Install app.
- Use a custom subdomain or zap.cloud is already selected. Leave it enabled and enter a name, for example,
mqtt. This domain will be used for the encrypted WebSocket connection. - Under App configuration, you’ll see three values that you can leave as they are to start with:
MQTT_PORT(default1883) is the port where the broker is accessible at your VPS IP address.ALLOW_ANONYMOUSis set tofalse, so only clients with a username and password can log in. You only needREQUIRE_CERTIFICATE(defaultfalse) for client certificates; see below. - Click Queue installation.
Installation takes just a moment. The app card will then show Online and the address SERVER_IP:1883 under Connection. The Connection and login note on the card summarizes the two connection methods and where to find the credentials. The Access link doesn’t open a web interface, because Mosquitto doesn’t have one. The WebSocket connection for MQTT clients listens behind the domain, so you’ll only see an error page in your browser.
- Click the gear icon on the app card and go to App environment.
- Under Automatically generated values, you’ll find
SERVICE_USER_MOSQUITTO(username) andSERVICE_PASSWORD_MOSQUITTO(password). Click the eye icon to show a value, or click the icon next to it to copy it. Above these, you’ll seeALLOW_ANONYMOUS,MQTT_PORT, andREQUIRE_CERTIFICATE.
Whenever Mosquitto starts, it writes this user to its password file again. You can’t create additional users with their own permissions in the One-Click App; all clients use this single account.
You can test the connection from a computer with the Mosquitto clients installed (apt install mosquitto-clients). In a terminal, subscribe to all topics under test/:
mosquitto_sub -h SERVER_IP -p 1883 -u BENUTZER -P 'PASSWORT' -t 'test/#' -v
In a second terminal, send a message:
mosquitto_pub -h SERVER_IP -p 1883 -u BENUTZER -P 'PASSWORT' -t test/hallo -m 'es klappt'
Without a username or with an incorrect password, the broker rejects the connection with “not authorised.” If the Network firewall is enabled for your VPS in the dashboard, allow port 1883 (TCP) or your MQTT_PORT value there.
On port 1883, the username, password, and messages are sent over the internet without encryption. For external connections, the secure option is the WebSocket connection through the app’s domain: One-Click Apps provide a valid certificate for this, and you connect using wss:// on port 443. Many clients support MQTT over WebSocket, including MQTT Explorer, Zigbee2MQTT, Home Assistant, and libraries such as Paho and MQTT.js. The address looks like this:
wss://mqtt.example.com:443/mqtt
Replace mqtt.example.com with your app’s domain. Many clients add the /mqtt path automatically. The broker also accepts the connection without a path. The username and password are the same as for port 1883.
If you set REQUIRE_CERTIFICATE to true, the broker will listen on MQTT_PORT using TLS only, and every client must present a client certificate from your own certificate authority (CA), in addition to a username and password. The WebSocket connection through the domain will no longer be available. Before doing this, use SSH to place the ca.crt, server.crt, and server.key files in the app’s certs folder under /data/coolify/services/. This is only worthwhile if your devices support client certificates. For most use cases, wss:// through the domain is sufficient.
Update Mosquitto in the app’s settings (gear icon), under App update. Click Check for updates and install a new version with Apply update. If it says the app is up to date, there isn’t a newer version in the app catalog yet.
The app rewrites the configuration every time it starts. Retained messages and open sessions are stored in the app’s data volume and are preserved across restarts and updates. Back up the VPS before an update, for example, using Backups in the dashboard. If you use client certificates, also back up the certs folder containing your CA.
The rest of this guide describes manual installation of Mosquitto with Docker Compose. This is useful, for example, if you need multiple users with their own permissions per topic, your own Let's Encrypt certificate for MQTT on port 8883, or full control over the configuration.
- A Linux VPS running Debian or Ubuntu, with SSH access; see First SSH access.
- Docker and Docker Compose (Compose v2); see Install Docker. Check the installation with
docker --versionanddocker compose version. - For TLS with a Let's Encrypt certificate, a domain or subdomain whose A record points to your VPS IP address (e.g.,
mqtt.example.com).
Mosquitto is very lightweight. Even with hundreds of devices, resource requirements remain low; you mainly need storage for logs and stored messages.
| Hardware | Minimum | Recommended |
|---|---|---|
| CPU | 1 vCPU core | 1 vCPU core |
| RAM | 512 MB (entire server) | 1 GB (entire server) |
| Storage | 1 GB free | 5 GB free |
Mosquitto uses three ports, which you only need to open if required:
| Port | Purpose |
|---|---|
1883 | MQTT without encryption |
8883 | MQTT with TLS |
9001 | MQTT over WebSockets (e.g., for web dashboards) |
First, use ufw to allow SSH only. Open the MQTT ports only after creating users and passwords:
ufw allow OpenSSH
ufw enable
Ports published by Docker with ports: are accessible from outside even if ufw blocks them. Docker adds its own rules before those of ufw. Therefore, publish only the ports you actually need, and never start Mosquitto with allow_anonymous true. For more about the firewall, see Managing the firewall on Linux.
Create a folder with subfolders for configuration, data, and logs:
mkdir -p /opt/mosquitto/config /opt/mosquitto/data /opt/mosquitto/log
cd /opt/mosquitto
Create the configuration file config/mosquitto.conf:
persistence true
persistence_location /mosquitto/data/
log_dest stdout
allow_anonymous false
password_file /mosquitto/config/passwd
acl_file /mosquitto/config/acl
listener 1883
protocol mqtt
listener 9001
protocol websockets
persistence truestores retained messages and open sessions in/mosquitto/dataso they persist through a restart.allow_anonymous falseandpassword_fileensure that only users with a password can connect.acl_filespecifies which users can read and write to which topics.- The second
listenerprovides MQTT over WebSockets on port9001. If you don’t need this, omit those two lines.
Create the permissions file config/acl. In this example, homeassistant can read and write everything, while the sensor1 sensor can only publish under sensors/:
user homeassistant
topic readwrite #
user sensor1
topic write sensors/#
Create the docker-compose.yml file:
services:
mosquitto:
image: eclipse-mosquitto:2.1.2-alpine
container_name: mosquitto
restart: unless-stopped
user: "1883:1883"
ports:
- "1883:1883"
- "9001:9001"
volumes:
- ./config:/mosquitto/config
- ./data:/mosquitto/data
- ./log:/mosquitto/log
imagespecifies the version. You can find the latest version on Docker Hub.user: "1883:1883"starts Mosquitto with its own user from the image instead of as root.portslists the ports that are accessible from outside. For access only from the same server, use127.0.0.1:1883:1883.
Create the first user with -c. You’ll be prompted for the password twice:
docker run --rm -it -v /opt/mosquitto/config:/mosquitto/config eclipse-mosquitto:2.1.2-alpine mosquitto_passwd -c /mosquitto/config/passwd homeassistant
Create each additional user without -c:
docker run --rm -it -v /opt/mosquitto/config:/mosquitto/config eclipse-mosquitto:2.1.2-alpine mosquitto_passwd /mosquitto/config/passwd sensor1
Starting with Mosquitto 2.1, -c no longer overwrites an existing file and exits with “File exists.” Use -c only for the very first file.
Assign the files to the Mosquitto user, start the broker, and only then open the ports in the firewall:
chown -R 1883:1883 /opt/mosquitto
chmod 0700 /opt/mosquitto/config/passwd /opt/mosquitto/config/acl
docker compose up -d
docker compose logs mosquitto
ufw allow 1883/tcp
ufw allow 9001/tcp
In the log, you’ll see “Opening ipv4 listen socket on port 1883” and “mosquitto version … running.” If you later change users or permissions, Mosquitto reloads the files with docker compose kill -s SIGHUP mosquitto without disconnecting clients.
In a terminal, subscribe to all topics under sensors/:
docker compose exec mosquitto mosquitto_sub -h localhost -u homeassistant -P 'PASSWORT' -t 'sensors/#' -v
In a second terminal, send a message:
docker compose exec mosquitto mosquitto_pub -h localhost -u sensor1 -P 'PASSWORT' -t sensors/temperatur -m 21.5
The first terminal will display sensors/temperatur 21.5. An attempt without a username will end with “Connection Refused: not authorised.” This verifies that anonymous access is actually blocked. To test from another computer, use the same commands but replace localhost with -h SERVER_IP.
Without TLS, usernames, passwords, and messages are sent over the internet in plain text. For long-term use, you should therefore use port 8883 with a Let's Encrypt certificate. For instructions on creating a certificate for mqtt.example.com, see Create an SSL certificate. Then copy the certificate and key to the Mosquitto folder so the Mosquitto user can read them:
mkdir -p /opt/mosquitto/certs
cp /etc/letsencrypt/live/mqtt.example.com/fullchain.pem /opt/mosquitto/certs/
cp /etc/letsencrypt/live/mqtt.example.com/privkey.pem /opt/mosquitto/certs/
chown -R 1883:1883 /opt/mosquitto/certs && chmod 600 /opt/mosquitto/certs/privkey.pem
Add a third listener to config/mosquitto.conf:
listener 8883
protocol mqtt
certfile /mosquitto/certs/fullchain.pem
keyfile /mosquitto/certs/privkey.pem
In docker-compose.yml, add the folder under volumes with ./certs:/mosquitto/certs:ro, add the line "8883:8883" under ports, and restart with docker compose up -d. Open port 8883 with ufw allow 8883/tcp. If everything is running over TLS, remove the entry for port 1883 from ports and close it with ufw delete allow 1883/tcp. Clients can then connect using mqtt.example.com and port 8883. Since Let's Encrypt is a well-known CA, most clients don’t need their own CA certificate.
Let's Encrypt certificates are renewed every 90 days. Copy them again afterward and reload Mosquitto with docker compose kill -s SIGHUP mosquitto, ideally automatically using a certbot deploy hook.
Everything important is in /opt/mosquitto: configuration, password and permissions files, certificates, and the data/mosquitto.db file containing retained messages. It’s quick to create a backup:
cd /opt && tar czf mosquitto-backup-$(date +%F).tar.gz mosquitto
Then copy the archive to another computer. The password file contains only hashes, but the backup should still be kept secure.
Set the new version under image in docker-compose.yml and restart the container:
cd /opt/mosquitto
docker compose pull
docker compose up -d
docker image prune -f
Before upgrading to a new major version, read the version changes. Clients will usually reconnect automatically after the restart.
Once the broker is running, connect your clients. They all need the same details: address (IP or domain), port, username, password, and, if using TLS, the CA certificate.
- In Home Assistant, open Settings > Devices & services and click Add integration.
- Search for MQTT and select the integration.
- Enter your VPS address under Broker,
1883or8883under Port, and your Username and Password. - For TLS or WebSocket, you’ll need the advanced options. These appear when Advanced mode is enabled in your Home Assistant profile. There, enable certificate verification. For the One-Click App, select WebSockets as the transport and enter the app’s domain, port
443, and the/mqttpath.
If Home Assistant itself is running on a VPS, this guide can help: Install Home Assistant with Docker.
- ESPHome: Add a
mqtt:block withbroker,port,username, andpasswordto your device’s YAML file. For instructions on running ESPHome on a server, see Set up ESPHome. - Tasmota and Shelly: In the device’s web interface, enter the host, port, username, and password under the MQTT settings, and enable MQTT.
- Zigbee2MQTT: In the configuration under
mqtt, enter the address asmqtt://SERVER_IP:1883, for an encrypted One-Click App connection aswss://mqtt.example.com:443/mqtt, or for a manual installation with TLS asmqtts://mqtt.example.com:8883, along withuserandpassword. - Custom programs: Libraries such as Paho (Python, Java, C) and MQTT.js (Node.js, browser) communicate directly over MQTT. In a browser, use MQTT over WebSockets: with the One-Click App, use
wss://and the app’s domain; with a manual installation, use port9001.
For a manual installation, it’s best to create a separate user for each device or device group, with appropriate permissions in config/acl. If a device is stolen or compromised, you only need to delete that one user.
- Mosquitto has no web interface. You manage users, permissions, and settings in files, and inspect messages with
mosquitto_subor a client such as MQTT Explorer. - Mosquitto runs as a single broker. The open-source version doesn’t provide a cluster across multiple servers, but you can connect brokers using
connectionentries (a bridge). - Devices that only support unencrypted MQTT send their password in plain text. Use a separate user with limited permissions for such devices, or connect them through a VPN.
With Mosquitto, you get a lightweight MQTT broker on your VPS that lets sensors, your smart home hub, and custom programs exchange messages, including across multiple locations. The quickest way to get started is the easy option, using the One-Click App: the password and data volume are already set up, and you can connect securely via wss:// through the app’s domain. If you need multiple users with their own permissions or your own TLS certificate on port 8883, manual installation with Docker Compose gives you more flexibility. In either case, make sure anonymous access remains disabled.
