ssl-guides 2026-12-02 ⏱ 4 min read

Configure SSL/TLS for MQTT with Mosquitto

Configure SSL/TLS for MQTT with Mosquitto

MQTT by default runs on TCP port 1883 with no encryption. Usernames, passwords and message payloads cross the network readable. MQTT over TLS conventionally uses port 8883. The broker presents a certificate, clients verify it, and optionally the broker also verifies a certificate from each client. This guide uses Mosquitto, but the concepts apply to other brokers; check their docs for exact option names.

Pick the right kind of certificate

ScenarioServer certificateClient verification
Devices connect over the internet to a public hostnamePublic CA (for example Let's Encrypt)Username and password, or client certificates
Closed fleet of devicesPrivate CA you controlClient certificates from the same CA
Local labSelf-signed, pinned in each clientOptional

A public CA certificate only works if the broker has a real DNS name that clients use. IP-only brokers and internal names generally need a private CA. For the public case see getting a free certificate with Certbot.

Step 1: configure the MQTT TLS listener in Mosquitto

Edit /etc/mosquitto/conf.d/tls.conf:

listener 8883
cafile   /etc/mosquitto/certs/ca.pem
certfile /etc/mosquitto/certs/server.pem
keyfile  /etc/mosquitto/certs/server.key
tls_version tlsv1.2

allow_anonymous false
password_file /etc/mosquitto/passwd

Here certfile should contain the server certificate followed by any intermediates, and cafile holds the CA that signed it. If you use a Let's Encrypt certificate, cafile can point at the system bundle. Mosquitto starts as root and then drops privileges, but it must be able to read the key file after dropping, so set ownership and mode deliberately: chown mosquitto:mosquitto server.key and chmod 600 server.key. Check your version's documentation for the newest TLS version options, as newer releases support TLS 1.3 settings.

Step 2: Require client certificates (optional)

For device fleets, mutual TLS is stronger than shared passwords. Add:

require_certificate true
use_identity_as_username true

With these options the broker rejects clients that do not present a certificate signed by the CA in cafile, and uses the certificate common name as the MQTT username. The same idea applies to HTTP APIs; see What Is Public Key Infrastructure (PKI)? for how a private CA is created.

Step 3: Restart and test

sudo systemctl restart mosquitto

mosquitto_sub -h broker.example.com -p 8883 \
  --cafile /etc/mosquitto/certs/ca.pem \
  -u device1 -P "$MQTT_PASSWORD" -t "sensors/#" -v

mosquitto_pub -h broker.example.com -p 8883 \
  --cafile /etc/mosquitto/certs/ca.pem \
  -u device1 -P "$MQTT_PASSWORD" -t sensors/temp -m 21.5

For a broker using a public CA, you can often use --capath /etc/ssl/certs instead of a file. With client certificates add --cert client.pem --key client.key. Always connect using the hostname that is in the certificate; otherwise hostname verification fails, and turning it off with an insecure option defeats the point.

Step 4: Inspect the handshake

openssl s_client -connect broker.example.com:8883 \
  -servername broker.example.com -brief

This shows the protocol version, the cipher and whether the chain verified. The OpenSSL testing guide explains how to read it.

Step 5: Close the plaintext port

Once all clients use 8883, remove the port 1883 listener, or bind it to localhost only with listener 1883 127.0.0.1. Leaving it open to the internet lets any client or attacker bypass the work you just did. Also review ACLs so a compromised device can only publish to its own topics.

MQTT TLS renewals and embedded devices

Small devices often have a fixed CA bundle and cannot update easily. If you use a public CA, remember that CA roots and chains change over time, so test with real firmware before you rely on it. A private CA gives you control over the chain and lifetime, but you must manage expiry and revocation yourself. When a public certificate renews, reload Mosquitto with systemctl reload mosquitto so the new files are used; see automating renewal with Certbot for deploy hooks.

Common mistakes

  • Connecting by IP address when the certificate carries a hostname.
  • Leaving allow_anonymous true on a broker reachable from the internet.
  • Forgetting that the key file must be readable by the broker user after it drops privileges.
  • Using a long CA chain on devices that only trust one root.

If a device cannot connect, test from a laptop with mosquitto_sub first. That separates broker problems from firmware problems quickly.

Related reading: fixing certificate chain errors and free SSL for a reverse proxy.

Frequently Asked Questions

Which port does MQTT use with TLS?

8883 by convention. Port 1883 stays plaintext, so close it or bind it to localhost once clients have moved.

Can I use a free certificate for MQTT?

Yes, if the broker has a public DNS name and clients use that name. Let's Encrypt certificates work. A broker reachable only by IP needs a private CA.

Do clients need their own certificates?

Only for mutual TLS. With require_certificate on, the broker rejects clients that do not present a certificate from your CA.

Why does a client fail hostname verification?

It connected by IP address or by a different name than the certificate carries. Connect with the hostname in the certificate.

🔒 Test Your Website SSL Certificate Now
Run Free SSL Check →

✅ Post-Installation Verification & SEO Checklist

After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:

  • Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
  • Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
  • Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
  • Confirm both root and www versions resolve cleanly to one canonical HTTPS URL.
  • Verify your HTTPS property and sitemap.xml inside Google Search Console and Bing Webmaster Tools.
Technically Reviewed by TryFreeSSL Security Team
Last updated: 2026-12-05
About Us →

Free SSL Certificates, Live SSL Checker & CSR Generator

Issue trusted Let's Encrypt SSL certificates in 60 seconds with zero server setup.

Related Articles

🤖 TryFreeSSL AI Assistant