Install Mosquitto MQTT Broker on Ubuntu 24.04

By Anurag Singh

Updated on Dec 09, 2024

Install Mosquitto MQTT Broker on Ubuntu 24.04

In this tutorial, we'll explain how to install Mosquitto MQTT Broker on Ubuntu 24.04. 

Mosquitto is a lightweight and open-source MQTT (Message Queuing Telemetry Transport) broker designed to implement the MQTT protocol for machine-to-machine communication. It is particularly popular in IoT (Internet of Things) applications where devices need to exchange data reliably and efficiently over constrained networks.

In this tutorial, we will walk through the process of installing and configuring the Mosquitto MQTT broker on Ubuntu 24.04, ensuring a secure and functional setup.

Prerequisites

Before getting started, ensure you have the following:

  • A KVM VPS or dedicated server with Ubuntu 24.04.
  • A non-root user with sudo privileges.
  • Basic knowledge of Linux commands.

How to Install Mosquitto MQTT Broker on Ubuntu 24.04

Step 1: Update the System

To begin, update your package lists and upgrade the installed packages to ensure your system is up-to-date.

sudo apt update && sudo apt upgrade -y

This ensures you have the latest security patches and software versions.

Step 2: Install Mosquitto and Mosquitto Clients

Mosquitto is available in the default Ubuntu repositories, making the installation process straightforward. Run the following command:

sudo apt install mosquitto mosquitto-clients -y
  • Mosquitto: The broker software that facilitates message exchanges.
  • Mosquitto-clients: Includes tools like mosquitto_pub and mosquitto_sub for publishing and subscribing to MQTT topics.

Once the installation is complete, verify the Mosquitto service status:

sudo systemctl status mosquitto

You should see that the service is active and running.

Step 3: Configure Mosquitto

Mosquitto is installed with a default configuration, which may not suit production use. Let’s customize it for better functionality and security.

3.1 Create a Custom Configuration File

The default configuration file is located at /etc/mosquitto/mosquitto.conf. Instead of modifying this file directly, we will create a custom configuration file.

sudo nano /etc/mosquitto/conf.d/custom.conf

Add the following configuration:

listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd
log_type all
log_dest syslog
log_dest stdout
  • listener 1883: Specifies the port on which Mosquitto listens for incoming connections.
  • allow_anonymous false: Disables anonymous connections, requiring clients to authenticate.
  • password_file: Specifies the file containing usernames and passwords.
  • log_type: Defines the types of events logged (all events in this case).
  • log_dest: Sets log output destinations.

Step 4: Set Up User Authentication

To enable secure access, we’ll configure user authentication.

4.1 Create a Password File

Run the following command to create a new password file and add a user:

sudo mosquitto_passwd -c /etc/mosquitto/passwd <username>

Replace <username> with your desired username. You will be prompted to enter a password for the user.

4.2 Add More Users (Optional)

To add more users, run:

sudo mosquitto_passwd /etc/mosquitto/passwd <another_username>

4.3 Change the permission

We need to adjust the /etc/mosquitto/passwd file permission. Execute following command:

sudo chown mosquitto:mosquitto /etc/mosquitto/passwd

Step 5: Restart Mosquitto

After making changes to the configuration, restart the Mosquitto service to apply the updates:

sudo systemctl restart mosquitto

Check the status to ensure it restarted correctly:

sudo systemctl status mosquitto

Step 6: Test the MQTT Broker

Use the Mosquitto clients (mosquitto_pub and mosquitto_sub) to test the broker.

6.1 Subscribe to a Topic

Open a terminal and run:

mosquitto_sub -h localhost -t test/topic -u <username> -P <password>

Replace <username> and <password> with the credentials you set up earlier.

6.2 Publish to the Same Topic

Open another terminal and run:

mosquitto_pub -h localhost -t test/topic -m "Hello, MQTT!" -u <username> -P <password>

If everything is configured correctly, the message will appear in the first terminal.

Conclusion

You have successfully installed and configured the Mosquitto MQTT broker on Ubuntu 24.04. With the broker running, you can now connect IoT devices or applications to exchange messages securely and efficiently. For production environments, always enforce SSL/TLS and monitor the server logs to ensure reliability.