
Mosquitto is an open-source message broker implementing the Message Queuing Telemetry Transport (MQTT) protocol. To install MQTT Broker on Ubuntu 24.04, you can take advantage of MQTT’s lightweight TCP/IP messaging platform, which is designed for Internet of Things (IoT) devices with minimal resources, such as low bandwidth or short battery life. Mosquitto relies on a publish-subscribe (pub/sub) topology, where publishers send messages to the broker and subscribers receive messages through a channel.
This article explains how to install MQTT Broker on Ubuntu 24.04. You'll also manage topics and secure the Mosquitto server with a password.
Before you begin:
Mosquitto is available in the default package repositories on Ubuntu 24.04. Follow the steps below to install MQTT Broker on Ubuntu 24.04.
Install the Mosquitto package.
Verify the new Mosquitto version.
Output:
Mosquitto runs as a system service under mosquitto. Follow the steps below to manage the service using the systemctl command.
View the mosquitto service status and verify it's active.
Output:
Stop the mosquitto service.
Start the mosquitto service.
Restart the mosquitto service.
Publishers and subscribers can connect to a Mosquitto server and exchange messages using the Mosquitto command-line interface. Follow the steps below to install the package.
Install the mosquitto-clients package.
Subscribe to a topic, such as home/sensor/temperature by running the following command as a background process.
Output:
Publish a new message to the home/sensor/temperature topic and set the value to 30.5.
Output:
Publish another message and set the value to 45.2.
Output:
View all active background jobs and note the mosquitto_sub job ID.
Output:
Stop the job by specifying the ID.
Output:
By default, Mosquitto allows clients to connect without any form of authentication. Follow the steps below to enable password-based authentication that requires clients to connect using a username and a password.
Create a new default.conf configuration in the /etc/mosquitto/conf.d directory.
Add the following configurations to the /etc/mosquitto/conf.d/default.conf file to disable anonymous connections and allow Mosquitto to read the /etc/mosquitto/passwd credentials file.
Save and close the file.
Open the /etc/mosquitto/passwd authentication file.
Add the following user credentials to the file. Replace EXAMPLE_PASSWORD and STRONG_PASSWORD with your desired values.
Save and close the file.
Encrypt the plain users' passwords using the mosquitto_passwd utility.
View the /etc/mosquitto/passwd file to verify you've encrypted the passwords.
Output:
Restart the mosquitto service to apply the configuration changes.
Subscribe to the /home/sensor/temperature topic using a valid username and password. For example, john_doe and EXAMPLE_PASSWORD.
Publish a new message to the /home/sensor/temperature topic.
Output:
View all active background jobs and note the mosquitto_sub job ID.
Output:
Stop the topic by specifying the job ID.
Output:
Try subscribing to the /home/sensor/temperature topic using incorrect credentials
Output:
The above output shows the connection fails because Mosquitto can't authorize the user.
You have installed Mosquitto MQTT Broker on Ubuntu 24.04, enabled password authentication, and run pub/sub samples to the broker. You can integrate Mosquitto with modern programming languages like Python, PHP, Ruby, and Golang to create IoT applications.
0 Comments
Be the first to comment and share your perspective with the community.