Recognized under UGC Section (2f) of the UGC Act 1956

Installing Mosquitto MQTT Broker on Linux (Ubuntu)

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

  1. Ubuntu 16.04 server with root access
  2. TCP: 1883 open port on Firewall

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:

  • sudo apt-get update
  • sudo apt-get install mosquitto

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.

  • sudo apt-get install mosquitto-clients

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:

  • mosquitto_pub -m “message from mosquitto_pub client” -t “test”

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“.

  • sudo mosquitto_passwd -c /etc /mosquitto / passwd dave

Password: password

Now, create a configuration file that will point to the created password file.

To do this, you have to follow the command mentioned below:

  • sudo nano /etc/mosquitto/conf.d/default.conf

It will lead you to an empty file, where you need to paste the following:

  • allow_anonymous false
  • password_file /etc/mosquitto/passwd

Now, Save and Exit with “Ctrl+O,” “Enter,” and “Ctrl+X“.

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 “dave” -P “password”

Try to publish a message in the client window without using the password.

  • mosquitto_pub -t “test” -m “message from mosquitto_pub client”

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 “message from mosquitto_pub client” -u “dave” -P “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.

  • 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 MQTT server for your IoT tasks. You can also use the Public IP of your Ubuntu server for MQTT broker Linux-based projects.