Mosquitto
Overview
This image runs Eclipse Mosquitto, the open source MQTT broker, installed and configured from the official Mosquitto PPA. The broker runs as a single node with password authentication turned on, so it rejects anonymous MQTT connections from the moment it starts.
Mosquitto speaks MQTT over TCP only, so the image also runs an nginx healthcheck shim on port 80. The shim returns a static OK response, which gives the AWS Marketplace scanner and your own load balancer an HTTP endpoint to probe. MQTT itself stays on port 1883.
What you get:
- Eclipse Mosquitto installed from the official Mosquitto PPA, running as a single node broker
- Password authentication on, with anonymous connections rejected from the start
- A strong MQTT password for the SecureAnyCloud user, created by a one-shot service on the first boot of every instance, so it is unique to that instance
- The password applied to the broker with mosquitto_passwd and written to /root/mosquitto-credentials.txt (mode 0600, root only)
- No shared or default MQTT credentials in the image
- The mosquitto_pub and mosquitto_sub command line tools from the mosquitto-clients package, for publishing and subscribing from the instance or from remote machines
- An nginx healthcheck shim on port 80 that returns a static OK response
Prerequisites
Before you deploy this image, make sure you have:
- An Amazon Web Services account where you can launch EC2 instances
- IAM permissions to launch instances, create security groups, and subscribe to AWS Marketplace products
- An EC2 key pair in the target Region, for SSH access to the instance
- A VPC and subnet in the target Region, with a security group that allows inbound port 22 from your management network and inbound port 1883 from any clients that will publish or subscribe
- The AWS CLI version 2 installed locally, if you plan to deploy from the command line
Launch the Instance from the AWS Marketplace
Sign in to the AWS Management Console and open the EC2 service. Select Launch instance.
Under Application and OS Images, choose AWS Marketplace AMIs and search for Eclipse Mosquitto. Select the SecureAnyCloud listing, choose Select, then choose Continue on the subscription summary.
Choose an instance type of m5.large or larger. This is a balanced default, so size it to the publish and subscribe load you expect. Choose your EC2 key pair under Key pair (login).
Under Network settings, select your VPC and subnet. Create or select a security group that allows inbound port 22 from your management network and inbound port 1883 from the clients that will connect to the broker. Leave the root volume at the default size or larger.
Select Launch instance. After the instance state becomes Running and the status checks pass, first boot takes only a few seconds. The firstboot service generates the per-instance MQTT password and starts the broker.
Launch the Instance from the AWS CLI
Run the following command to launch an instance from the SecureAnyCloud Eclipse Mosquitto Marketplace AMI into an existing subnet and security group. Replace these values:
- <ami-id> with the AMI ID shown on the Marketplace listing
- <key-name> with your EC2 key pair name
- <subnet-id> with your subnet ID
- <security-group-id> with a security group that opens port 22 from your management network and port 1883 from your client networks
aws ec2 run-instances \
--image-id <ami-id> \
--instance-type m5.large \
--key-name <key-name> \
--subnet-id <subnet-id> \
--security-group-ids <security-group-id> \
--block-device-mappings '{"DeviceName":"/dev/sda1","Ebs":{"VolumeSize":20,"VolumeType":"gp3"}}' \
--metadata-options 'HttpTokens=required,HttpEndpoint=enabled' \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=mosquitto-broker}]'Connect to the Instance over SSH
Connect with SSH as the default login user for the operating system variant you launched. The login user is ubuntu on Ubuntu 24.04.
Replace <key-file> with the path to your private key file and <instance-public-ip> with the public IP address or DNS name of the instance.
ssh -i <key-file> ubuntu@<instance-public-ip>Retrieve the Per-Instance MQTT Password
The first boot service writes the generated MQTT password to /root/mosquitto-credentials.txt. The file is mode 0600 and owned by root, so read it with sudo. It also records the MQTT user name and port for reference.
sudo cat /root/mosquitto-credentials.txtThe MOSQUITTO_PASSWORD line holds the password generated for this instance. Keep it safe. By default, it is the only credential that can connect to the broker.
Confirm the Broker
Check that the broker and the healthcheck shim are running and that the MQTT listener is bound.
systemctl is-active mosquitto.service nginx.service ss -tln | grep -E ':(1883|80) 'Check the installed version of Mosquitto.
dpkg-query -W -f='${Package} ${Version}\n' mosquittoThe HTTP healthcheck shim serves a static OK on port 80. The Marketplace scanner uses this endpoint. To verify it yourself, run:
curl -fsS http://127.0.0.1/Publish and Subscribe Locally
The mosquitto-clients package provides mosquitto_sub for subscribing and mosquitto_pub for publishing. Read the password into a shell variable, then run a subscriber in one terminal and a publisher in another. This confirms the broker is delivering messages.
Subscribe to a topic. This command blocks and waits for messages.
PASS=$(sudo grep '^MOSQUITTO_PASSWORD=' /root/mosquitto-credentials.txt | cut -d= -f2-) mosquitto_sub -h 127.0.0.1 -u sac -P "$PASS" -t 'hello/#' -vIn a second SSH session on the same instance, read the password the same way and publish a message.
PASS=$(sudo grep '^MOSQUITTO_PASSWORD=' /root/mosquitto-credentials.txt | cut -d= -f2-) mosquitto_pub -h 127.0.0.1 -u sac -P "$PASS" -t hello/world -m 'first message from mosquitto'The subscriber prints hello/world first message from mosquitto. The # wildcard subscribes to every topic under hello/, so messages on any other hello/... topic are delivered too.
If no subscriber is attached when the publisher runs, the message is dropped on delivery (MQTT QoS 0). To retain a message for new subscribers, add -r to the publish command. Mosquitto stores the last retained message on the topic and delivers it immediately to every subsequent subscriber.
To stop the subscriber, press Ctrl+C.
Publish and Subscribe from a Remote Client
The MQTT listener binds 0.0.0.0:1883, so any host that can reach the instance on port 1883 can connect with the same credentials.
Install the mosquitto-clients package on the remote host (for example, sudo apt-get install -y mosquitto-clients on Ubuntu). Then connect to the instance's public IP address or DNS name.
mosquitto_sub -h <instance-public-ip> -u sac -P '<password>' -t 'hello/#' -vmosquitto_pub -h <instance-public-ip> -u sac -P '<password>' -t hello/world -m 'message from a remote client'Make sure your security group allows inbound TCP 1883 from the source network. For production deployments, restrict port 1883 to known client networks, or front the broker with TLS on port 8883 (see Step 9).
Manage MQTT Users
The image ships with one MQTT user, SecureAnyCloud. To add another user, run mosquitto_passwd against the broker's password file. Then reload the service so the broker picks up the change.
sudo mosquitto_passwd /etc/mosquitto/passwd <new-username>
sudo systemctl reload mosquitto.servicemosquitto_passwd prompts twice for the new user's password and appends the user to the file. To delete a user, edit /etc/mosquitto/passwd as root, remove the line for that user, and reload the service.
Add a TLS Listener on Port 8883
Plain MQTT on port 1883 is the default. For deployments exposed to the internet, add a TLS listener on the standard MQTTS port 8883.
Place a certificate and private key on the instance (for example, under /etc/mosquitto/certs/). Then create a drop-in configuration file and reload the broker.
sudo install -d -o root -g mosquitto -m 0750 /etc/mosquitto/certs
# Copy your certificate, private key and CA bundle into /etc/mosquitto/certs/
# fullchain.pem (certificate + intermediates)
# privkey.pem (private key, mode 0640 root:mosquitto)
# ca.pem (your CA chain, optional for client cert auth)
sudo tee /etc/mosquitto/conf.d/tls.conf >/dev/null <<'CONF' listener 8883 0.0.0.0 certfile /etc/mosquitto/certs/fullchain.pem keyfile /etc/mosquitto/certs/privkey.pem
# Uncomment to require client certificates:
# cafile /etc/mosquitto/certs/ca.pem
# require_certificate true CONF
sudo systemctl reload mosquitto.serviceOpen inbound TCP 8883 on the security group from your client networks. Then connect with the mqtts:// or --cafile form of your MQTT client.
Persistence and Broker Tuning
Mosquitto persists retained messages, queued messages for offline subscribers, and subscription state under /var/lib/mosquitto/. The default /etc/mosquitto/mosquitto.conf sets persistence true and persistence_location /var/lib/mosquitto/. The SecureAnyCloud overlay in /etc/mosquitto/conf.d/sac.conf does not change those values.
For high message rate workloads, review and tune the broker for your deployment. The full list of broker options is documented at https://mosquitto.org/man/mosquitto-conf-5.html.
Place additional configuration in a new file under /etc/mosquitto/conf.d/, then reload the broker.
sudo systemctl reload mosquitto.serviceMaintenance
Keep the operating system patched with the standard package manager. The official Mosquitto PPA is already configured on the image, so broker upgrades arrive through the normal package update process.
sudo apt-get update
sudo apt-get -y upgradeAfter a Mosquitto package upgrade, restart the broker if the package post-installation step does not do so automatically.
sudo systemctl restart mosquitto.serviceTo diagnose connection or routing issues, inspect the broker logs with journalctl.
sudo journalctl -u mosquitto.service -n 200 --no-pagerSupport
SecureAnyCloud provides this Amazon Machine Image with 24/7 technical support by email and chat. Contact SecureAnyCloud for help with MQTT broker deployment, authentication and access control, TLS configuration, bridging multiple brokers, and broker tuning.
Eclipse Mosquitto is a trademark of the Eclipse Foundation. All other product and company names are trademarks or registered trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.
Deploy on AWS
Launch Eclipse Mosquitto on AWS with 24/7 support from SecureAnyCloud.
View on Marketplace
Need Help?
Our support team is available 24/7.
sales@secureanycloud.com