Mosquitto MQTT is an open-source message broker that implements the MQTT protocol. It is a lightweight, publish-subscribe network protocol designed for low-bandwidth, high-latency networks. It facilitates efficient communication between devices, making it ideal for Internet of Things (IoT) applications and other scenarios requiring reliable message delivery with minimal overhead. Mosquitto supports various security features, such as TLS encryption and authentication, ensuring secure data transmission. Its ability to handle thousands of concurrent connections makes it suitable for large-scale deployments.
In this tutorial, we will walk you through the process of installing the Mosquitto MQTT Server on Ubuntu 22.04.
Step 1 – Installing Required Packages
First, we need to update our package list and install some essential packages required for the installation process. Open your terminal and run the following command:
apt-get update
apt-get install curl gnupg2 wget git apt-transport-https ca-certificates -y
Step 2 – Adding the Mosquitto PPA
Next, we need to add the Mosquitto PPA (Personal Package Archive) to our system. The PPA provides the latest version of Mosquitto. Run the following command:
add-apt-repository ppa:mosquitto-dev/mosquitto-ppa -y
This command adds the Mosquitto PPA to your system’s software sources.
Step 3 – Installing Mosquitto and Mosquitto Clients
Now that the PPA has been added, we can install Mosquitto and its clients. Execute the following command:
apt install mosquitto mosquitto-clients -y
This command installs the Mosquitto server and the Mosquitto client utilities (mosquitto_sub and mosquitto_pub).
To verify that Mosquitto has been installed correctly and is running, use the systemctl command to check its status:
systemctl status mosquitto
You should see an output similar to this:
● mosquitto.service - Mosquitto MQTT Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2024-06-26 13:49:53 UTC; 6s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 97745 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 97746 ExecStartPre=/bin/chown mosquitto:mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 97747 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 97748 ExecStartPre=/bin/chown mosquitto:mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
Main PID: 97749 (mosquitto)
Tasks: 1 (limit: 4579)
Memory: 1.3M
CPU: 32ms
CGroup: /system.slice/mosquitto.service
└─97749 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
The output indicates that the Mosquitto service is active and running.
Step 4 – Configuring Mosquitto
By default, Mosquitto is configured to allow anonymous connections. For security reasons, it is recommended to set up a password for your Mosquitto broker.
Run the following command to create a password file and add a user:
mosquitto_passwd -c /etc/mosquitto/passwd hjethva
You will be prompted to enter and confirm a password for the user you just created (hjethva).
Password:
Reenter password:
Set the correct ownership for the password file:
chown mosquitto:mosquitto /etc/mosquitto/passwd
Next, create a configuration file to specify the listener and password file. Open the configuration file with nano:
nano /etc/mosquitto/conf.d/default.conf
Add the following lines:
listener 1883
password_file /etc/mosquitto/passwd
These lines configure Mosquitto to listen on port 1883 and use the specified password file for authentication.
After making these changes, restart the Mosquitto service to apply the new configuration:
systemctl restart mosquitto
Step 5 – Testing Mosquitto
To ensure everything is working correctly, we will test Mosquitto using the mosquitto_pub and mosquitto_sub commands.
Open a terminal window and subscribe to a topic:
mosquitto_sub -u hjethva -P password -v -t "hello/topic"
In another terminal window, publish a message on the same topic:
mosquitto_pub -u hjethva -P password -t 'hello/topic' -m 'hello MQTT'
You should see the following output in the subscriber terminal:
hello/topic hello MQTT
This confirms that the Mosquitto server is working correctly.
Conclusion
In this tutorial, we have covered the installation and configuration of the Mosquitto MQTT server on Ubuntu 22.04. We also demonstrated how to secure the server with a password and tested the setup using Mosquitto client tools. Mosquitto is a powerful and lightweight MQTT broker that is widely used in IoT and messaging applications. With this setup, you can now start building your own MQTT-based solutions. You can try deploying Mosquitto MQTT on dedicated server hosting from Atlantic.Net!