
Mosquitto is an open-source message broker that uses the Message Queuing Telemetry Transport (MQTT) Protocol. To install MQTT Broker on Ubuntu 20.04, Mosquitto offers compatibility with the TCP/IP model and serves as the standard messaging platform for the Internet of Things (IoT).
Since the MQTT protocol is extremely lightweight, its small code footprint allows you to create applications for devices with minimal resources such as short battery life, limited network bandwidth, and unreliable internet connections.
The Mosquitto application supports the publisher/subscriber topology. In this model, clients connect to the Mosquitto server, which acts as a broker to distribute information to other clients subscribed or sending messages to a channel.
In this article, you'll install MQTT Broker on Ubuntu 20.04 and configure the Mosquitto application to understand how the event-driven MQTT protocol supports IoT applications. If you're working with a newer version, check out our article on How to Install Mosquitto MQTT Broker on Ubuntu 24.04.
To follow along with this guide, you need:
sudo rights.You'll pull the mosquitto package from Ubuntu's software repository by executing the following steps, which are essential to install an MQTT broker on Ubuntu and demonstrate how to install Mosquitto in Ubuntu.
SSH to your server and update the package information index.
Install the mosquitto package.
The mosquitto package should now load on your server. Confirm the status of the mosquitto service.
Ensure the package is loaded and active.
Once running, you can manage the mosquitto services by executing the following commands.
Stop the mosquitto service:
Start the mosquitto service:
Restart the mosquitto service:
When using an MQTT client, you connect to the Mosquitto broker to send and receive messages on different topics depending on the application's use case. A client can either be a publisher, a subscriber, or both. To test this functionality, you can install the Mosquitto client on Ubuntu.
The Mosquitto package ships with a command-line client that allows you to test the server functionalities. Install the client.
Next, you'll subscribe to a topic. In the MQTT protocol, a topic is a string that the server/broker uses to filter messages for the connected clients. For instance, here are some sample topics that you can use when using the Mosquitto broker in a home automation application.
To subscribe to a topic, execute the mosquitto_sub -t command followed by the name of the topic that you want to subscribe to. For example, to subscribe to the home/lights/sitting_room topic, execute.
Please note that the above command has a blocking function and will put your shell terminal in a listening state.
Open a second terminal window and don't close the first one. This time around, publish an "ON" message to the topic home/lights/sitting_room topic using the mosquitto_pub -m command.
You should now receive the ON payload in the first window.
Next, publish an OFF message still on the same home/lights/sitting_room topic on your second terminal.
Your broker should display the new message as well.
In this guide, you're manually subscribing and publishing messages using the Mosquitto Clients for demonstration purposes. In real-life applications, you should program small microchip devices that support the TCP/IP layer like the ESP8266 to push a message to a broker to control or even monitor devices. Here are some common use-cases where the Mosquitto package is used in real life.
In addition to the above use-cases, there are dozens of libraries that you can use to connect to the Mosquitto server using your favorite programming language, including PHP, Python, Golang, and more.
By default, the Mosquitto server is not secured. However, you can make some configuration settings to secure it with usernames and passwords.
Mosquitto reads configuration information from the following location.
Create a default.conf under the directory.
Paste the information below to disable anonymous connections and allow Mosquitto to read valid credentials from the /etc/mosquitto/passwd file.
Save and close the file.
Open the /etc/mosquitto/passwd file with nano.
Then, populate the file with the account details for the users that you want to connect to the Mosquitto server. Replace EXAMPLE_PASSWORD and EXAMPLE_PASSWORD_2 with strong values.
Save and close the file.
Next, use the mosquitto_passwd utility to encrypt the passwords.
Your passwords are now encrypted in a format that only the Mosquitto server can decrypt. Use the Linux cat command to confirm the encryption process.
Output.
Restart the mosquitto service to load the new changes.
From this point forward, you should execute any pub/sub command using the syntax below. Remember to replace john_doe and EXAMPLE_PASSWORD with the credentials that you defined in the password file.
Unauthenticated commands or connections with incorrect credentials should now fail.
Output.
In this tutorial, you've installed MQTT Broker on Ubuntu 20.04 and configured the Mosquitto server. You also used the command-line interface to subscribe and publish messages to a sample topic. Consider using the Mosquitto server when deploying your next IoT application.
0 Comments
Be the first to comment and share your perspective with the community.