With IoT becoming a leading name in the market, businesses are keen to import it into their strategy of changing their market by the use of new technologies to expand their business in the market. Due to the increasing demand, many people seek to learn the use of Mosquitto MQTT broker Linux to pump up their IoT productivity, and Shivalik College of Engineering helps its students in getting the right knowledge to inculcate the learning of such new technologies to help them make a successful career in the industry.
MQTT, being lightweight and effective with battery-powered applications, has dragged a lot of limelight towards itself.
So, here’s presenting you a tutorial on how to install an MQTT broker for a Linux (Ubuntu) machine.
Must have Pre-Requisites
Installation Process of Mosquitto Broker on Linux (Ubuntu) Platform
Step 1: The Installation of the broker
You need to update the package list of Ubuntu. Then, you must install the recent version of Mosquitto broker available in it.
The use of the below commands will start the installation process:
Step 2: Installation of the clients
Mosquitto clients aid in testing MQTT via a command-line utility. You need to make use of two separate command windows. Use the first one to subscribe to a topic called “test”. And on the other window, you only need to publish a message.
Use the following mosquitto command to install clients for MQTT brokers.
Subscription to “test” Topic
The next command to execute is mosquitto_sub -t “test”.
Here “mosquitto_sub” is a subscribe client that we got installed from the previous command. “-t” is trailed by the topic name “test”.
Publishing a Message to “test” Topic
In the second terminal window, you need to publish a message to the topic “test”.
The following command can do this:
Note the additional parameter “-m”. It is followed by the message to be published.
Hit the “Enter Key”. You will then find that the message you entered is displayed on the first terminal window, where the mosquitto_sub client has been running.
Step 3: Securing with a password
MQTT possesses a utility for generating a password file. You need to type the command: “mosquitto_passwd“.
Now, create a configuration file that will point to the created password file.
To do this, you have to follow the command mentioned below:
It will lead you to an empty file, where you need to paste the following:
Now, Save and Exit with “Ctrl+O,” “Enter,” and “Ctrl+X“.
Testing the Changes
Exit the subscribe client window by pressing “Ctrl+C”. And then, restart with the given mosquitto command:
Try to publish a message in the client window without using the password.
If the below message shows, it indicates successful implementation of the changes.
Now, try publishing the message with a username and password-
It will display a message in the subscribe client window as in Step 2.
Testing the Installation
The testing process in Linux remains the same as in Windows.
Open terminals and type the below command.
The output should show that the MQTT broker is running on port 1883.
Multiple MQTT Brokers
You can configure the MQTT broker in such a way that it can run on many ports. It requires the creation of multiple instances of Mosquitto. However, you can find several tutorials for the same on the internet for your reference.
With all these said, you can now set up a password-protected Mosquitto MQTT server for your IoT tasks. You can also use the Public IP of your Ubuntu server for MQTT broker Linux-based projects.
Written By:- SHASHANK MISHRA