You are on page 1of 10

7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.

com

NEWS Introducing Vultr Managed Databases for MySQL. Toil not


Categories Linux Guides Install Mosquitto MQTT Br…
included.

Install Mosquitto MQTT


Broker On Ubuntu 20.04
Server
Author: Francis Ndungu

Last Updated: Fri, Oct 15, 2021


Linux Guides System Admin Ubuntu

Using a Different System?

Introduction

Mosquitto is an open-source message broker that uses


the Message Queuing Telemetry Transport (MQTT)
Protocol. MQTT runs on top of the TCP/IP model and is
the standard messaging platform for the Internet of
Things (IoT).

Since the MQQT protocol is extremely lightweight, its


small code footprint allows you to create applications for
devices with minimal resources such as short battery life,

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 1/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

limited network bandwidth, and unreliable internet


Categories Linux Guides Install Mosquitto MQTT Br…
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 guide, you'll install and configure the Mosquitto


application and learn how the event-driven MQQT
protocol works with IoT applications.

Prerequisites

To follow along with this guide, you need:

An Ubuntu 20.04 server.


A non-root user with sudo rights.

1. Install the Mosquitto Server

You'll pull the mosquitto package from Ubuntu's


software repository by executing the following steps.

1. SSH to your server and update the package information


index.

$ sudo apt update

2. Install the mosquitto package.

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 2/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

Categories Linux$Guides
sudo apt install
Install -yMQTT
Mosquitto mosquitto

Br…

3. The mosquitto package should now load on your


server. Confirm the status of the mosquitto service.

$ sudo systemctl status mosquitto

Ensure the package is loaded and active .

● mosquitto.service - Mosquitto MQTT v3.1/


Loaded: loaded (/lib/systemd/system/m
Active: active (running) since Fri 20
Docs: man:mosquitto.conf(5)

man:mosquitto(8)

...

4. Once running, you can manage the mosquitto


services by executing the following commands.

Stop the mosquitto service:

$ sudo systemctl stop mosquitto

Start the mosquitto service:

$ sudo systemctl start mosquitto

Restart the mosquitto service:

$ sudo systemctl restart mosquitto

2. Install and Test the Mosquitto Clients

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 3/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

When using an MQTT client, you connect to the


Categories Linux Guides Install Mosquitto MQTT Br…
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.

1. The Mosquitto package ships with a command-line client


that allows you to test the server functionalities. Install the
client.

$ sudo apt install -y mosquitto-clients

2. Next, you'll subscribe to a topic. In the MQQT 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.

home/lights/sitting_room
home/lights/kitchen
home/lights/master_bedroom
home/lights/kids_bedroom
3. 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.

$ mosquitto_sub -t "home/lights/sitting_ro

Please note that the above command has a blocking


function and will put your shell terminal in a listening
state.

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

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 4/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

Categories
$ mosquitto_pub
Linux Guides
-m "ON" -t "home/lights/si
Install Mosquitto MQTT Br…

5. You should now receive the ON payload in the first


window.

ON

6. Next, publish an OFF message still on the same


home/lights/sitting_room topic on your second
terminal.

$ mosquitto_pub -m "OFF" -t "home/lights/s

7. Your broker should display the new message as well.

ON

OFF

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

Monitoring patients' heartbeats and sending them


to a central server for monitoring by doctors. This
avoids heavy transport costs that the patients
could incur by traveling to the hospital.
In the gas and the oil industry, MQQT devices
monitor different parameters and send the data to a
central broker. Usually, this involves thousands of
sensors in remote locations that collect and send
data through satellite links that are billed per data
usage. Luckily, the MQQT topology keeps the
transmission minimal and only pushes data to the
server when necessary.

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 5/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

In the transport industry, MQQT devices monitor the


Categories Linux Guides Install Mosquitto MQTT Br…
location of trains in real-time and send data to the
companies' headquarters in order to provide better
insights to customers who want to travel without
any delays.
Also, the Mosquitto broker can be used as a middle
layer in a chat application to refresh the online
status of users and pass messages between end-
users.
Another common scenario where the Mosquitto
server can be a good fit is in decoupled systems.
Clients can send data to the broker, which then
sends the data to a database for permanent
storage.
9. 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.

3. Secure the Mosquitto Server

By default, the Mosquitto server is not secured. However,


you can make some configuration settings to secure it
with usernames and passwords.

1. Mosquitto reads configuration information from the


following location.

/etc/mosquitto/conf.d

2. Create a default.conf under the directory.

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

3. Paste the information below to disable anonymous


connections and allow Mosquitto to read valid credentials

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 6/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

from the /etc/mosquitto/passwd file.


Categories Linux Guides Install Mosquitto MQTT Br…

allow_anonymous false

password_file /etc/mosquitto/passwd

4. Save and close the file.

5. Open the /etc/mosquitto/passwd file with nano .

$ sudo nano /etc/mosquitto/passwd

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

john_doe:EXAMPLE_PASSWORD

mary_smith:EXAMPLE_PASSWORD_2

7. Save and close the file.

8. Next, use the mosquitto_passwd utility to encrypt the


passwords.

$ sudo mosquitto_passwd -U /etc/mosquitto/

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

$ sudo cat /etc/mosquitto/passwd

Output.

john_doe:$6$TSzNycsj...5Qyvgd4g==
mary_smith:$6$DtlKf1lG.../rLHIL0Q==

10. Restart the mosquitto service to load the new


changes.

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 7/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

Categories
$ sudo systemctl
Linux Guides
restart mosquitto

Install Mosquitto MQTT Br…

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

$ mosquitto_sub -u john_doe -P EXAMPLE_PAS


$ mosquitto_pub -u john_doe -P EXAMPLE_PAS

Unauthenticated commands or connections with incorrect


credentials should now fail.

$ mosquitto_pub -m "ON" -t "home/lights/si


$ mosquitto_sub -t "home/lights/sitting_ro
$ mosquitto_sub -u john_doe -P WRONG_PASSW
$ mosquitto_pub -u john_doe -P WRONG_PASSW

Output.

...

Connection error: Connection Refused: not

Conclusion

In this tutorial, you've installed and configured the


Mosquitto server on your Ubuntu 20.04 server. You've
also used the command-line interface to subscribe and
publish messages to a sample topic. Consider using the
Mosquitto server next time when deploying an IoT
application.

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 8/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

Categories Linux Guides Install Mosquitto MQTT Br…


Want to contribute?
You could earn up to $600 by adding new articles.

Submit your article

Suggest an update

Request an article

Products

Features

Use Cases

Marketplace

Resources

Company

Over 45,000,000 Cloud Servers Launched

Terms of Service

AUP/DMCA

Privacy Policy

Cookie Policy
https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 9/10
7/19/22, 7:22 PM Install Mosquitto MQTT Broker On Ubuntu 20.04 Server - Vultr.com

Categories Linux Guides Install Mosquitto MQTT Br…


© Vultr 2022
VULTR is a registered trademark of The Constant Company, LLC.

https://www.vultr.com/docs/install-mosquitto-mqtt-broker-on-ubuntu-20-04-server/ 10/10

You might also like