How to Install Mosquitto MQTT Broker on Ubuntu 24
Mosquitto is a lightweight, open-source MQTT (Message Queuing Telemetry Transport) broker, ideal for IoT (Internet of Things) devices to communicate in real-time. In this guide, we’ll walk through the installation of Mosquitto on Ubuntu 24.
What is Mosquitto?
Mosquitto enables real-time communication between devices using the publish-subscribe model. It’s widely used in IoT projects like smart homes and industrial automation due to its efficiency and scalability.
Step 1: Update Your System
Before installing Mosquitto, ensure that your system is up to date. Open a terminal and run:
sudo apt updatesudo apt upgrade -y
This ensures all available updates are installed.
Step 2: Add the Mosquitto Repository
You need to add the Mosquitto PPA repository to get the latest version:
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
This adds the Mosquitto development PPA to your system.
Step 3: Install Mosquitto
Next, install the Mosquitto broker and client tools:
sudo apt updatesudo apt install mosquitto mosquitto-clients -y
mosquitto
is the MQTT broker.mosquitto-clients
includes the command-line tools for testing MQTT.
Step 4: Enable and Start the Mosquitto Service
Enable and start the Mosquitto service to run it automatically on system boot:
sudo systemctl enable mosquitto
sudo systemctl start mosquitto
To verify that Mosquitto is running, check the status:
sudo systemctl status mosquitto
The service should be marked as active and running.
Step 5: Configure Mosquitto (Optional)
The default configuration works fine for most cases, but you can edit Mosquitto’s configuration file located at /etc/mosquitto/mosquitto.conf
if needed.
For example, you can change the listener port, set up authentication, or configure Access Control Lists (ACLs). After making any changes, restart Mosquitto:
sudo systemctl restart mosquitto
Step 6: Test Mosquitto MQTT Broker
Use the Mosquitto client tools to test the broker. Open two terminals:
In the first terminal, subscribe to a topic:
mosquitto_sub -t "test/topic"
In the second terminal, publish a message to the same topic:
mosquitto_pub -t "test/topic" -m "Hello MQTT"
If everything is set up correctly, the first terminal will display the message “Hello MQTT”.
Step 7: Secure Mosquitto (Recommended)
To enhance security, you can enable username/password authentication and TLS encryption.
To create a password file:
sudo mosquitto_passwd -c /etc/mosquitto/passwd yourusername
Then, configure Mosquitto to use this file by editing the configuration file:
sudo nano /etc/mosquitto/conf.d/default.conf
Add the following lines:
allow_anonymous false
password_file /etc/mosquitto/passwd
Finally, restart Mosquitto to apply the changes:
sudo systemctl restart mosquitto
Conclusion
Installing Mosquitto MQTT Broker on Ubuntu 24 is a straightforward process. You can quickly set up a message broker for IoT communication by following these steps. Don’t forget to secure your installation using authentication and encryption for production environments.