Skip to content
Follow
Ubuntu Linux

How to Install Eclipse Mosquitto MQTT on Ubuntu

Richard
Written by
Richard
May 6, 2023 Updated Jul 14, 2026 3 min read
How to Install Nextcloud AIO on Ubuntu Linux
How to Install Nextcloud AIO on Ubuntu Linux

Installing Eclipse Mosquitto MQTT on Ubuntu sets up a message broker, which is like a central post office for your IoT devices to send and receive messages.

Eclipse Mosquitto MQTT is a small, free program that follows the MQTT rules for devices to talk to each other easily.

When you install it on Ubuntu 22.04 LTS, you can securely share data between all your connected gadgets. It’s a straightforward process with just a few commands.

⚡ Quick Answer

Install Mosquitto by adding the official PPA and running sudo apt install mosquitto mosquitto-clients. Then, enable and start the service with sudo systemctl enable mosquitto and sudo systemctl start mosquitto.

Install Mosquitto packages

Install Mosquitto packages. To begin, add the official Mosquitto PPA (Personal Package Archive) to your Ubuntu system. This ensures you get the latest software updates. Afterward, update your system and install both the Mosquitto broker and its client tools.

Execute the following commands to add the PPA and update your system:

sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt update
sudo apt install mosquitto mosquitto-clients

Once installed, verify that the service is running:

sudo systemctl enable mosquitto
sudo systemctl start mosquitto
sudo systemctl status mosquitto

MQTT Explorer interface connected to an Eclipse Mosquitto MQTT broker
MQTT Explorer interface connected to an Eclipse Mosquitto MQTT broker

Setup authentication

Setting up authentication for your Mosquitto MQTT server is crucial. By default, Mosquitto allows open connections, so securing your MQTT server is important. This authentication setup involves creating a password for users who want to connect. You will create an admin user and then tell the Mosquitto MQTT server to use this password for security.

First, create an admin user:

⚠️Warning
sudo mosquitto_passwd -c /etc/mosquitto/.passwd superadmin

Configure Mosquitto to save client data if the server restarts. Open the configuration file, usually found at `/etc/mosquitto/mosquitto.conf`. Add the line `persistence true` to ensure client session information is saved to disk.

sudo nano /etc/mosquitto/mosquitto.conf

Add these lines to the end of the file:

persistence true
persistence_location /var/lib/mosquitto/

Now, create an authentication file at /etc/mosquitto/conf.d/auth.conf and add these lines:

⚠️Warning
listener 1883
allow_anonymous false
password_file /etc/mosquitto/.passwd

To encrypt your network traffic, you’ll need to set up TLS/SSL certificates. Create a directory for your certificates and generate a DH parameter file:

sudo mkdir -p /etc/mosquitto/certs
sudo openssl dhparam -out /etc/mosquitto/certs/dhparam.pem 2048

You can obtain SSL certificates from a provider, such as Let’s Encrypt, or learn how to create self-signed certificates in Ubuntu Linux. Once you have your certificates, add them to your ssl.conf file to ensure all data is encrypted during transit.

Restart the service to apply changes:

sudo systemctl restart mosquitto

Test that your authentication works by sending a message:

sudo mosquitto_pub -h localhost -t "test" -m "Hello" -u "superadmin" -P "your_password"

Conclusion

You now have a secure MQTT broker running on Ubuntu. By using the official PPA, you stay updated. By using persistence and authentication, you keep your data safe and reliable. Your devices can now communicate securely across your network.

How do I run Mosquitto from command line?

Open the command line and use the mosquitto -v -c command to run the broker in a verbose mode, which will allow you to see the debug messages. Using the -c option, we can pass a mosquitto configuration file.

Was this guide helpful?

Was this helpful?
Richard

About the Author

Richard

Tech Writer, IT Professional

Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.

📚 Related Tutorials

How to Install Additional Software on Ubuntu
Ubuntu Linux How to Install Additional Software on Ubuntu
How to Re-enable TLS 1.0 and 1.1 on Windows 11
Windows How to Re-enable TLS 1.0 and 1.1 on Windows 11
How to Delete Let's Encrypt SSL Certificates
Ubuntu Linux How to Delete Let's Encrypt SSL Certificates

No comments yet — be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *