Skip to content

How to Set Up Mosquitto on a Linux Server – Your Own MQTT Broker for Smart Home and IoT

Set up Mosquitto on a Linux VPS: use the One-Click App with a password and wss://, or install it with Docker Compose, users, TLS, and Home Assistant.

1 December 202612 min read1 Views0 helpful

Introduction

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.

When it makes sense to run Mosquitto on a VPS

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.

The easy way: Mosquitto as a One-Click App

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.

Requirements

  • 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.

Install

  1. Open your VPS in the ZapX dashboard and go to the One-Click Apps tab.
  2. Search the app catalog for Mosquitto and click Install app.
  3. 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.
  4. Under App configuration, you’ll see three values that you can leave as they are to start with: MQTT_PORT (default 1883) is the port where the broker is accessible at your VPS IP address. ALLOW_ANONYMOUS is set to false, so only clients with a username and password can log in. You only need REQUIRE_CERTIFICATE (default false) for client certificates; see below.
  5. 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.

Find your credentials

  1. Click the gear icon on the app card and go to App environment.
  2. Under Automatically generated values, you’ll find SERVICE_USER_MOSQUITTO (username) and SERVICE_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 see ALLOW_ANONYMOUS, MQTT_PORT, and REQUIRE_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.

Test the connection

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/:

Code
mosquitto_sub -h SERVER_IP -p 1883 -u BENUTZER -P 'PASSWORT' -t 'test/#' -v

In a second terminal, send a message:

Code
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.

Connect securely

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:

Code
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.

Info: Client certificates with REQUIRE_CERTIFICATE

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.

Updates and backups

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.

Manual installation

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.

Requirements

  • 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 --version and docker 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.

HardwareMinimumRecommended
CPU1 vCPU core1 vCPU core
RAM512 MB (entire server)1 GB (entire server)
Storage1 GB free5 GB free

Prepare the firewall

Mosquitto uses three ports, which you only need to open if required:

PortPurpose
1883MQTT without encryption
8883MQTT with TLS
9001MQTT over WebSockets (e.g., for web dashboards)

First, use ufw to allow SSH only. Open the MQTT ports only after creating users and passwords:

Code
ufw allow OpenSSH
ufw enable
Warning: Docker bypasses ufw

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.

Install with Docker Compose

Create a folder with subfolders for configuration, data, and logs:

Code
mkdir -p /opt/mosquitto/config /opt/mosquitto/data /opt/mosquitto/log
cd /opt/mosquitto

Create the configuration file config/mosquitto.conf:

Code
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 true stores retained messages and open sessions in /mosquitto/data so they persist through a restart.
  • allow_anonymous false and password_file ensure that only users with a password can connect.
  • acl_file specifies which users can read and write to which topics.
  • The second listener provides MQTT over WebSockets on port 9001. 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/:

Code
user homeassistant
topic readwrite #

user sensor1
topic write sensors/#

Create the docker-compose.yml file:

Code
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
  • image specifies 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.
  • ports lists the ports that are accessible from outside. For access only from the same server, use 127.0.0.1:1883:1883.

Create users and passwords

Create the first user with -c. You’ll be prompted for the password twice:

Code
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:

Code
docker run --rm -it -v /opt/mosquitto/config:/mosquitto/config eclipse-mosquitto:2.1.2-alpine mosquitto_passwd /mosquitto/config/passwd sensor1
Tip: “File exists” error with mosquitto_passwd

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:

Code
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.

Test the connection

In a terminal, subscribe to all topics under sensors/:

Code
docker compose exec mosquitto mosquitto_sub -h localhost -u homeassistant -P 'PASSWORT' -t 'sensors/#' -v

In a second terminal, send a message:

Code
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.

Encrypted connection with TLS

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:

Code
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:

Code
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.

Info: Copy the certificate again after renewal

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.

Back up your data

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:

Code
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.

Update Mosquitto

Set the new version under image in docker-compose.yml and restart the container:

Code
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.

Connect Home Assistant and your devices

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.

Home Assistant

  1. In Home Assistant, open Settings > Devices & services and click Add integration.
  2. Search for MQTT and select the integration.
  3. Enter your VPS address under Broker, 1883 or 8883 under Port, and your Username and Password.
  4. 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 /mqtt path.

If Home Assistant itself is running on a VPS, this guide can help: Install Home Assistant with Docker.

ESPHome and other devices

  • ESPHome: Add a mqtt: block with broker, port, username, and password to 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 as mqtt://SERVER_IP:1883, for an encrypted One-Click App connection as wss://mqtt.example.com:443/mqtt, or for a manual installation with TLS as mqtts://mqtt.example.com:8883, along with user and password.
  • 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 port 9001.

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.

Limitations

  • Mosquitto has no web interface. You manage users, permissions, and settings in files, and inspect messages with mosquitto_sub or 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 connection entries (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.

Conclusion

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.

Free tools

Related tools for servers and developers

View all

FAQ

Frequently asked questions

The One-Click App takes just a few minutes to install: a username and password are generated automatically, anonymous connections are disabled, stored messages persist through restarts, and you can connect securely over wss:// on port 443 using the app’s domain. It’s a good fit if your clients can share one account. A manual Docker Compose installation is useful if you need multiple users with their own topic permissions or your own Let's Encrypt certificate for MQTT on port 8883.
By default, port 1883 is for MQTT without encryption, port 8883 is for MQTT with TLS, and port 9001 is often used for MQTT over WebSockets. The One-Click App publishes MQTT on port 1883 (changeable with MQTT_PORT) and provides an encrypted WebSocket connection through the app’s domain on port 443. Only open the ports you actually use, and use an encrypted connection over the internet whenever possible, since otherwise passwords and messages are sent in plain text.
Mosquitto is rejecting the login. Usually, the username or password is incorrect, or the client isn’t sending credentials even though anonymous access is disabled. Check the values. For the One-Click App, find them under App environment > Automatically generated values (SERVICE_USER_MOSQUITTO and SERVICE_PASSWORD_MOSQUITTO). For a manual installation, check the password file and reload it after changes with docker compose kill -s SIGHUP mosquitto.

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 people found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP-Hosting support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.