With IoT becoming a leading name in the market, businesses are keen to import it in their strategy. Due to the increasing demand, many people seek to learn the use of Mosquitto MQTT broker Linux to pump up their IoT productivity.
Eclipse Mosquitto is an open-source message broker (EPL/EDL licensed) that supports MQTT versions 5.0, 3.1.1, and 3.1. The MQTT protocol uses a publish/subscribe method to deliver a lightweight messaging method. This makes it outstanding for Internet of Things (IoT) communications, as with low-power sensors or mobile devices like phones, embedded computers, and microcontrollers. Mosquitto is a lightweight operating system that may be employed on many devices, from low-power single-board computers to full-fledged servers. This tutorial guides you on how to install MQTT Mosquitto Broker version 2.0, MQTT version v5.0, on your linux machine.
Refer this article for the introduction of MQTT Mosquitto Broker.
So, here’s a tutorial on how to install an MQTT broker for a Linux (Ubuntu) machine.
Pre-Requisites
- Ubuntu 16.04 server with root access
- TCP: 1883 open port on Firewall
Installation of Mosquitto Broker on Linux (Ubuntu) Platform
Step 1: The Installation of the broker
Firstly, you need to update the package list of Ubuntu. Then, you must install the recent version of Mosquitto broker available in it.
Run the following commands,
sudo apt-get update
sudo apt-get install mosquitto
Step 2: Installation of the clients
Mosquitto clients helps in testing MQTT via a command-line utility. You should install publisher and subscriber clients which sends and receives the payload/messages. You need to make use of two separate terminals. The first terminal is to subscribe to a topic called “test”. The second terminal is for publishing a message. Use the following mosquitto command to install clients for MQTT brokers.
sudo apt-get install mosquitto-clients
Note: First subscribe to the topic, before publishing the messages on the topic.
Subscription to Topic “Test”
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”.
If you want to view the mosquitto process on the background, add -d flag to the command.
Publishing a Message to the topic “Test”
In the second terminal window, you need to publish a message to the topic “Test”.
The following command can do this:
mosquitto_pub -m “hello world” -t “Test”
Note the additional flag “-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.
Mosquitto Version
You can also check the version of the mosquitto with the following command,
sudo mosquitto –v
Step 3 (Optional) : Securing with a password
MQTT possesses a utility for generating a password file. You need to type the command: “mosquitto_passwd“.
mosquitto_passwd is a tool for managing password files for mosquitto. The flag -c tends to create a new password file, and this will overwrite the existing files. You have to create a new when you want to setup a password, so in the command below, the new username is “user”. As soon as you create a new username, you will be prompted to create a password for this username.
sudo mosquitto_passwd -c /etc /mosquitto / passwd user
Password: ***********
Now, create a configuration file that will point to the created password file.
sudo nano /etc/mosquitto/conf.d/default.conf
Now you have created a file default.conf, add the following lines to the file.
allow_anonymous false
password_file /etc/mosquitto/passwd
Now, Save and Exit with “Ctrl+O,” “Enter,” and “Ctrl+X“.
Another method: You can also directly add these lines in the main mosquitto.conf file in the mosquitto folder, just remove the last line include_dir /etc/mosquitto/conf.d and add the above lines in the configuration file.
Testing the Changes
Restart mosquitto:
sudo systemctl restart mosquitto
Exit the subscribe client window by pressing “Ctrl+C”. And then, restart with the given mosquitto command:
mosquitto_sub -t “test” -u “<username>” -P “<Password>”
Try to publish a message in the client window without using the password.
mosquitto_pub -t “Test” -m “hello world”
If the below message shows, it indicates successful implementation of the changes.
- Connection Refused: not authorized.
- Error: The connection was refused.
Now, try publishing the message with a username and password-
mosquitto_pub -t “test” -m “hello world” -u “<username>” -P “<Password>”
It will display a message in the subscribe client window as in Step 2.
Without Password : Publisher and Subscriber
Testing the Installation
The testing process in Linux remains the same as in Windows.
Open terminals and type the below command.
netstat –at
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 MQTTserver for your IoT tasks. You can also use the Public IP of your Ubuntu server for MQTT broker Linux based projects.