How to Install Mosquitto MQTT Broker on Ubuntu 24.04

Updated on November 15, 2024
How to Install Mosquitto MQTT Broker on Ubuntu 24.04 header image

Introduction

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.

Prerequisites

Before you begin:

Install Mosquitto MQTT Broker on Ubuntu 24.04

Mosquitto is available in the default package repositories on Ubuntu 24.04. Follow the steps below to install MQTT Broker on Ubuntu 24.04.

  1. Install the Mosquitto package.

    console
    $ sudo apt install -y mosquitto
    
  2. Verify the new Mosquitto version.

    console
    $ mosquitto -v
    

    Output:

    1730087147: mosquitto version 2.0.18 starting
    1730087147: Using default config.
    1730087147: Starting in local only mode. Connections will only be possible from clients running on this machine.
    1730087147: Create a configuration file which defines a listener to allow remote access.

Manage the Mosquitto System Service

Mosquitto runs as a system service under mosquitto. Follow the steps below to manage the service using the systemctl command.

  1. View the mosquitto service status and verify it's active.

    console
    $ sudo systemctl status mosquitto
    

    Output:

    ● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; enabled; preset: enabl>
     Active: active (running) since Fri 2024-10-25 06:51:37 UTC; 12min ago
       Docs: man:mosquitto.conf(5)
          man:mosquitto(8)
    ...
  2. Stop the mosquitto service.

    console
    $ sudo systemctl stop mosquitto
    
  3. Start the mosquitto service.

    console
    $ sudo systemctl start mosquitto
    
  4. Restart the mosquitto service.

    console
    $ sudo systemctl restart mosquitto
    

Install and Test the Mosquitto Clients

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.

  1. Install the mosquitto-clients package.

    console
    $ sudo apt install -y mosquitto-clients
    
  2. Subscribe to a topic, such as home/sensor/temperature by running the following command as a background process.

    console
    $ mosquitto_sub -t "home/sensor/temperature" &
    

    Output:

    23.5
  3. Publish a new message to the home/sensor/temperature topic and set the value to 30.5.

    console
    $ mosquitto_pub -t "home/sensor/temperature" -m "30.5" -q 1 -r
    

    Output:

    30.5
    30.5
  4. Publish another message and set the value to 45.2.

    console
    $ mosquitto_pub -t "home/sensor/temperature" -m "45.2" -q 1 -r
    

    Output:

    45.2
    45.2
  5. View all active background jobs and note the mosquitto_sub job ID.

    console
    $ jobs
    

    Output:

    [1]   Running                 mosquitto_sub -t "home/lights/sitting_room" &
  6. Stop the job by specifying the ID.

    console
    $ kill %1
    

    Output:

    [1]   Done                    mosquitto_sub -t "home/lights/sitting_room" &

Secure the Mosquitto Server

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.

  1. Create a new default.conf configuration in the /etc/mosquitto/conf.d directory.

    console
    $ sudo nano /etc/mosquitto/conf.d/default.conf
    
  2. 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.

    ini
    allow_anonymous false
    password_file /etc/mosquitto/passwd
    

    Save and close the file.

  3. Open the /etc/mosquitto/passwd authentication file.

    console
    $ sudo nano /etc/mosquitto/passwd
    
  4. Add the following user credentials to the file. Replace EXAMPLE_PASSWORD and STRONG_PASSWORD with your desired values.

    ini
    john_doe:EXAMPLE_PASSWORD
    mary_smith:STRONG_PASSWORD
    

    Save and close the file.

  5. Encrypt the plain users' passwords using the mosquitto_passwd utility.

    console
    $ sudo mosquitto_passwd -U /etc/mosquitto/passwd
    
  6. View the /etc/mosquitto/passwd file to verify you've encrypted the passwords.

    console
    $ sudo cat /etc/mosquitto/passwd
    

    Output:

    john_doe:$6$TSzNycsj...5Qyvgd4g==
    mary_smith:$6$DtlKf1lG.../rLHIL0Q==
  7. Restart the mosquitto service to apply the configuration changes.

    console
    $ sudo systemctl restart mosquitto
    
  8. Subscribe to the /home/sensor/temperature topic using a valid username and password. For example, john_doe and EXAMPLE_PASSWORD.

    console
    $ mosquitto_sub -u john_doe -P EXAMPLE_PASSWORD -t "/home/sensor/temperature"
    
  9. Publish a new message to the /home/sensor/temperature topic.

    console
    $ mosquitto_pub -u john_doe -P EXAMPLE_PASSWORD -t "home/sensor/temperature" -m "22.5" -q 1 -r
    

    Output:

    22.5
  10. View all active background jobs and note the mosquitto_sub job ID.

    console
    $ jobs
    

    Output:

    [1]   Running                 mosquitto_sub -u john_doe -P EXAMPLE_PASSWORD -t "home/lights/sitting_room" &
  11. Stop the topic by specifying the job ID.

    console
    $ kill %1
    

    Output:

    [1]   Done                    mosquitto_sub -u john_doe -P EXAMPLE_PASSWORD -t "home/lights/sitting_room" &
  12. Try subscribing to the /home/sensor/temperature topic using incorrect credentials

    console
    $ mosquitto_pub -u john_doe -P VERY_WRONG_PASSWORD -t "home/sensor/temperature" -m "22.5" -q 1 -r
    

    Output:

    Connection error: Connection Refused: not authorised.
    Error: The connection was refused.

    The above output shows the connection fails because Mosquitto can't authorize the user.

Conclusion

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.