How To Create Secure MQTT Broker
Introduction
Eclipse Mosquitto is an open-source MQTT (Message Queuing Telemetry Transport) broker, licensed under EPL/EDL. It supports MQTT versions 3.1, 3.1.1, and 5.0, making it a reliable choice for IoT (Internet of Things) applications. Mosquitto is lightweight, making it suitable for everything from low-power embedded devices to high-performance servers.
This article provides an updated guide on Mosquitto, including its installation, security features, and robust deployment on AWS Ubuntu (2025 update).
1. Installing Mosquitto on AWS Ubuntu
Step 1: Update System Packages
sudo apt-get update
Step 2: Install Mosquitto Broker & Clients
sudo apt-get install mosquitto mosquitto-clients -y
Step 3: Start and Enable Mosquitto
sudo systemctl start mosquitto
sudo systemctl enable mosquitto
Step 4: Test Mosquitto Locally
Subscribe:
mosquitto_sub -h localhost -t mytopic
Publish:
mosquitto_pub -h localhost -t mytopic -m "Hello World"
2. Enabling Remote Access
By default, Mosquitto allows only local connections. To enable remote access, follow these steps:
Step 1: Open Port 1883 on AWS Security Group
- In the AWS console, navigate to Security Groups.
- Open port 1883 to allow MQTT connections.
Step 2: Modify Mosquitto Configuration
sudo nano /etc/mosquitto/conf.d/default.conf
Add:
listener 1883
allow_anonymous true
Save and exit.
Step 3: Restart Mosquitto
sudo systemctl restart mosquitto
Your broker is now accessible remotely on port 1883.
3. Ensuring Robust MQTT Broker Performance
To auto-restart Mosquitto in case of failure, create a monitoring script.
Step 1: Create a Restart Script
echo '#!/bin/bash
if ! pgrep -x "mosquitto" > /dev/null; then
echo "Mosquitto not running, restarting..." >> /var/log/mosquitto.log
systemctl restart mosquitto
fi' > /home/ubuntu/mosquitto_restart.sh
Step 2: Make It Executable
chmod +x /home/ubuntu/mosquitto_restart.sh
Step 3: Schedule with Cron
sudo crontab -e
Add this line:
*/5 * * * * /home/ubuntu/mosquitto_restart.sh
This ensures Mosquitto restarts automatically if it crashes.
4. Securing Mosquitto with SSL Encryption
We use Let’s Encrypt to secure MQTT communication.
Step 1: Install Certbot
sudo apt-get install certbot -y
Step 2: Generate SSL Certificates
Ensure you have a domain/subdomain pointing to your server.
sudo certbot certonly --standalone -d mqtt.example.com
Step 3: Modify Mosquitto Configuration
sudo nano /etc/mosquitto/conf.d/default.conf
Add:
listener 8883
certfile /etc/letsencrypt/live/mqtt.example.com/fullchain.pem
keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
Save and restart Mosquitto.
sudo systemctl restart mosquitto
Now MQTT secure communication (MQTTS) is enabled on port 8883.
5. Configuring WebSockets for Web Apps
For Angular, React, or JavaScript applications, enable MQTT over WebSockets.
Step 1: Modify Mosquitto Configuration
sudo nano /etc/mosquitto/conf.d/default.conf
Add:
listener 8083
protocol websockets
certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
Save and restart Mosquitto.
sudo systemctl restart mosquitto
Now, secure WebSockets (WSS) are enabled on port 8083.
6. Adding Password Protection
To secure MQTT, create a username and password.
Step 1: Create Password File
sudo mosquitto_passwd -c /etc/mosquitto/passwd user1
Enter a password when prompted.
Step 2: Modify Configuration
sudo nano /etc/mosquitto/conf.d/default.conf
Add:
allow_anonymous false
password_file /etc/mosquitto/passwd
Save and restart Mosquitto.
sudo systemctl restart mosquitto
Step 3: Use Credentials to Publish & Subscribe
Subscribe:
mosquitto_sub -h localhost -t test -u "user1" -P "password123"
Publish:
mosquitto_pub -h localhost -t "test" -m "Hello Secure MQTT" -u "user1" -P "password123"
7. Complete Mosquitto Configuration File (Reference)
allow_anonymous false
password_file /etc/mosquitto/passwd
listener 1883
listener 8883
certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
listener 8083
protocol websockets
listener 8084
protocol websockets
certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
Conclusion
Mosquitto remains a powerful, lightweight, and secure MQTT broker for IoT applications. In 2025, security, scalability, and robust monitoring are crucial for MQTT deployments.
With the above installation, security, and automation steps, you can deploy a secure and stable Mosquitto MQTT broker on AWS Ubuntu, suitable for both IoT devices and web applications.
Read This: MQTT on Mobile platforms | MQTT on Android | MQTT on iOS
great stuff..:-)
thank you
Pingback: IoT vs M2M | Difference between M2M and IoT - IoTbyHVM
Pingback: Apache Kafka - A distributed Streaming Platform - IoTbyHVM
Pingback: IoT Communication APIs - IoTbyHVM
Pingback: How to Send e-mail using NodeJS - How To - IoTbyHVM
Pingback: Node.js for Microcontrollers - Explainer - IoTbyHVM
Pingback: GitHub Pages - Websites for you and your projects - IoTbyHVM
Pingback: MQTT Tools - Web, Mobile platforms, Desktop tools, Gateways
Pingback: What is DTMF - Dual-tone multi-frequency - Top X IoTbyHVM
Pingback: MQTT Tools – Web, Mobile platforms, Desktop tools, Gateways - TechIoT
Pingback: How To Use Raspberry pi in a truely headless mode -
Pingback: IoT Protocols - IoTbyHVM - Explore tech bytes
Pingback: Visuino - Alternative of Node-Red - IoTbyHVM - Explore tech bytes
Pingback: Slax OS - Your pocket operating system - IoTbyHVM - Explore tech bytes
Pingback: Termux- A Linux environment android app - IoTbyHVM - Explore tech bytes
Pingback: MQTT Public Brokers List - IoT - IoTbyHVM - Explore tech bytes
Pingback: Stringify - Automation service for the Internet of Things (IoT)
Pingback: Interfacing a light Sensor (LDR) with Raspberry Pi - Raspberry Pi
Pingback: How To Setup Static IP Address on Raspberry Pi - Raspberry Pi
Pingback: Controlling LED with Raspberry Pi - IoTbyHVM - Explore TechBytes
Pingback: Best CDN For WordPress Websites - IoTbyHVM - Explore TechBytes
Pingback: MicroPython : optimized to run on a microcontroller
Pingback: RabbitMQ - message-oriented middleware - IoT Software&Tools
Pingback: A Simple Chat Server with NodeJS - IoT Config - IoTbyHVM - Explore TechBytes
Pingback: Setting up Authentication in Mosquitto MQTT Broker - IoTbyHVM - Explore TechBytes
Pingback: Portable OS - Your pocket operating systems - TopX
Pingback: Smick: A Smart Brick for IoT - IoTbyHVM - Explore TechBytes
Pingback: MQTT Servers/Brokers - IoTbyHVM - Explore TechBytes
Pingback: Message Brokers : An introduction - Explainer - IoTbyHVM - Explore TechBytes
Pingback: DHT11 vs DHT22: Overview - IoTbyHVM - Explore TechBytes
Pingback: Redis : What and Why? - IoTbyHVM - Explore TechBytes
Pingback: VerneMQ - Clustering MQTT for high availability and scalability
Pingback: How To Install VerneMQ on UbunTu, RHEL, Docker, Debian and CentOS
Pingback: Setting up SPI on Raspberry Pi - IoTbyHVM - Explore TechBytes
Pingback: Kaa - An Enterprise-Grade IoT Platform - IoTbyHVM - Explore TechBytes
Pingback: Mosquitto - An open source MQTT broker | Create Broker | Setting Up
Pingback: Industrial IoT | Industry 4.0 | IIoT | Industrial Internet of Things - IoTbyHVM - Explore TechBytes
Pingback: Dynamic WLAN configuration for ESP32 Board | AutoConnect
Pingback: What Is Chatbot ? - IoTbyHVM - Explore TechBytes
Pingback: Using Node js and Arduino with LED Blinking Program
Pingback: Termux Tutorials - Linux Environment Android app - Explainer
Pingback: Porteus OS : Portable Linux - IoTbyHVM - Explore TechBytes
Pingback: Popular Development Boards for IoT - IoTbyHVM - Explore TechBytes
Pingback: Nano RK : A Wireless Sensor Networking Real-Time Operating System
Pingback: HTML 5 | HTML vs HTML 5 - IoTbyHVM - Bits & Bytes of IoT
Pingback: Content Delivery Network : CDN | CDN for WordPress
Pingback: Mosquitto MQTT broker | Install Broker in AWS | Setting Up
Pingback: ThingSpeak IoT Platform Introduction - IoTbyHVM - Bits & Bytes of IoT
Pingback: ThingSpeak IoT Platform - IoTbyHVM - Bits & Bytes of IoT
Pingback: Adafruit IO ? | Adafruit IO Arduino Library | Adafruit MQTT
Pingback: MQTT General Questions - IoTbyHVM - Bits & Bytes of IoT
Pingback: Using Mq135 Sensor with InfluxDB - IoTbyHVM - Bits & Bytes of IoT
Pingback: MQTT protocol | Specification | Clarifications - CompileIoT
Pingback: PICO-WHU4 : Powerful Raspberry Pi-alternative with Core i7 available
Pingback: UDOO SBCs (single board computers) - CompileIoT
Pingback: ArduPy | What is ArduPy ? - CompileIoT -Explore IoT
Pingback: The Much Needed Cloud Application Security Checklist
Pingback: Termux- A Linux environment android app - CoolDigiBytes
Pingback: ExpressJS – Web framework for Node.js - apalgorithm.com