How to install Mosquitto MQTT on Ubuntu 20.04
How to install Mosquitto MQTT on Ubuntu 20.04

How To Install Mosquitto MQTT On Ubuntu 20.04

On this short article we will learn how to install the latest version of Mosquitto MQTT (MQTT v.2) on Ubuntu 20.04 LTS.

Introduction

The Message Queuing Telemetry Transport (MQTT) is a lightweight machine-to-machine messaging protocol, designed to provide lightweight publish/subscribe communication between devices. The protocol usually runs over TCP/IP. It is designed for connections with remote locations where the network bandwidth is limited. The protocol is an open OASIS standard and an ISO recommendation (ISO/IEC 20922). Mosquitto is an open source message broker (or server) that implements MQTT protocols. Mosquitto has good community support, documentation, and ease of installation it has become one of the most popular MQTT brokers.

On this tutorial, we’ll install Mosquitto and set up our broker to use SSL to secure our password-protected MQTT communications.

Mosquitto MQTT Installation On Ubuntu 20.04

On this tutorial, we will use Mosquitto MQTT (MQTT v.2) source to be installed on Ubuntu 20.04 LTS server. For this purpose we need several dependencies package. The installation will be arranged as following steps :

Prerequisites

A. Mosquitto MQTT Installation
1. Update Ubuntu Repository  and Get MQTT dependencies
3. Add mosquitto user
4. Download Mosquitto source code and unpack it
5. Compile and install MQTT software

B. Configuring MQTT Server
1. Create permission and password for new user
2. Create configuration file for Mosquitto MQTT Server
3. Add systemd unit file
4. Start and check the MQTT service

C. Testing

Prerequisites

Before we are going to the install MQTT application and test it, we have to prepare the environment first as mentioned below :

  • Ubuntu 20.04 System with updated repository
  • a user with sudo privilege
  • Sufficient disk space and good internet access

A. Mosquitto MQTT Installation

On this stage we will install Mosquitto application from the scratch. We will download MQTT source file, make and install it on the system rather than using apt repository.

1. Update Ubuntu Repository  and Get MQTT dependencies

We will update our Ubuntu system first and getting MQTT dependencies packages.

$sudo apt update
$ sudo apt -y install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc

The output :

ramans@otodiginet:~$ sudo apt -y install build-essential libwrap0-dev libssl-dev libc-ares-dev uuid-dev xsltproc
Reading package lists... Done
Building dependency tree 
Reading state information... Done
build-essential is already the newest version (12.8ubuntu1.1).
build-essential set to manually installed.
The following additional packages will be installed:
libc-ares2 libssl1.1
Suggested packages:
libssl-doc
The following NEW packages will be installed:
libc-ares-dev libc-ares2 libssl-dev libwrap0-dev uuid-dev xsltproc
The following packages will be upgraded:
libssl1.1
1 upgraded, 6 newly installed, 0 to remove and 167 not upgraded.
Need to get 3,123 kB of archives.
After this operation, 8,872 kB of additional disk space will be used.
Get:1 http://mirror.telkomuniversity.ac.id/ubuntu focal-updates/main amd64 libssl1.1 amd64 1.1.1f-1ubuntu2.4 [1,319 kB]
Get:2 http://mirror.telkomuniversity.ac.id/ubuntu focal-updates/main amd64 libssl-dev amd64 1.1.1f-1ubuntu2.4 [1,583 kB]
Get:3 http://mirror.telkomuniversity.ac.id/ubuntu focal/main amd64 libwrap0-dev amd64 7.6.q-30 [21.7 kB] 
Get:4 http://mirror.telkomuniversity.ac.id/ubuntu focal/main amd64 xsltproc amd64 1.1.34-4 [14.3 kB] 
Get:5 http://mirror.telkomuniversity.ac.id/ubuntu focal/main amd64 libc-ares2 amd64 1.15.0-1build1 [37.8 kB] 
Get:6 http://mirror.telkomuniversity.ac.id/ubuntu focal/main amd64 libc-ares-dev amd64 1.15.0-1build1 [114 kB] 
Get:7 http://mirror.telkomuniversity.ac.id/ubuntu focal-updates/main amd64 uuid-dev amd64 2.34-0.1ubuntu9.1 [33.6 kB] 
Fetched 3,123 kB in 9s (367 kB/s) 
Preconfiguring packages 

...

Setting up libwrap0-dev:amd64 (7.6.q-30) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...

2. Add mosquitto user

We will create a group and user system called as mosquitto to dedicated holding Mosquitto MQTT application. For this purpose, we will do the following tasks :

$ sudo adduser mosquitto
$ sudo usermod -aG sudo mosquitto
$ sudo su - mosquitto

The output :

ramans@otodiginet:~$ sudo adduser mosquitto
Adding user `mosquitto' ...
Adding new group `mosquitto' (1001) ...
Adding new user `mosquitto' (1001) with group `mosquitto' ...
Creating home directory `/home/mosquitto' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
passwd: password updated successfully
Changing the user information for mosquitto
Enter the new value, or press ENTER for the default
Full Name []: 
Room Number []: 
Work Phone []: 
Home Phone []: 
Other []: 
Is the information correct? [Y/n] Y
ramans@otodiginet:~$ sudo usermod -aG sudo mosquitto
ramans@otodiginet:~$ sudo su - mosquitto
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

3. Download Mosquitto MQTT Archive File and Unpack

By using mosquitto system user which was already created we will use wget command line, we will download Mosquitto MQTT source file and by using tar command line we will unpack the file. Please find the following task below :

$ wget https://mosquitto.org/files/source/mosquitto-2.0.11.tar.gz
$ tar zxvf mosquitto-2.0.11.tar.gz

The output will be :

mosquitto@otodiginet:~$ wget https://mosquitto.org/files/source/mosquitto-2.0.11.tar.gz
--2021-06-27 01:43:36-- https://mosquitto.org/files/source/mosquitto-2.0.11.tar.gz
Resolving mosquitto.org (mosquitto.org)... 85.119.83.194, 2001:ba8:1f1:f271::2
Connecting to mosquitto.org (mosquitto.org)|85.119.83.194|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 760325 (743K) [application/octet-stream]
Saving to: ‘mosquitto-2.0.11.tar.gz’

mosquitto-2.0.11.tar.gz 100%[====================================================>] 742.50K 683KB/s in 1.1s

2021-06-27 01:43:39 (683 KB/s) - ‘mosquitto-2.0.11.tar.gz’ saved [760325/760325]
mosquitto@otodiginet:~$ tar zxvf mosquitto-2.0.11.tar.gz 
mosquitto-2.0.11/
mosquitto-2.0.11/pskfile.example
mosquitto-2.0.11/security/
mosquitto-2.0.11/security/mosquitto.apparmor
mosquitto-2.0.11/service/
mosquitto-2.0.11/service/upstart/
mosquitto-2.0.11/service/upstart/mosquitto.conf
...
mosquitto-2.0.11/NOTICE.md
mosquitto-2.0.11/README-compiling.md
mosquitto-2.0.11/Makefile
mosquitto-2.0.11/edl-v10

Until this step, we have an extracted MQTT source file and ready to compile.

4. Compile and Install MQTT Application

On this stage we will make Mosquitto MQTT application and install it. To do this task, we have to use mosquitto user.

~/mosquitto-2.0.11$ make && sudo make install

The output will be shown below :

mosquitto@otodiginet:~/mosquitto-2.0.11$ make && sudo make install
set -e; for d in lib apps client plugins src; do make -C ${d}; done
make[1]: Entering directory '/home/mosquitto/mosquitto-2.0.11/lib'
make -C cpp
make[2]: Entering directory '/home/mosquitto/mosquitto-2.0.11/lib/cpp'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/mosquitto/mosquitto-2.0.11/lib/cpp'
make[1]: Leaving directory '/home/mosquitto/mosquitto-2.0.11/lib'
make[1]: Entering directory '/home/mosquitto/mosquitto-2.0.11/apps'
set -e; for d in db_dump mosquitto_ctrl mosquitto_passwd; do make -C ${d}; done
make[2]: Entering directory '/home/mosquitto/mosquitto-2.0.11/apps/db_dump'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/mosquitto/mosquitto-2.0.11/apps/db_dump'
make[2]: Entering directory '/home/mosquitto/mosquitto-2.0.11/apps/mosquitto_ctrl'

B. Configuring MQTT Server

After Mosquitto MQTT application was installed successfully, then we will configure it to work properly. On our scenario, there will be a server which provide a service and a client which sends the request. Please follow the following steps to complete the MQTT configuration.

1. Create permission and password for new user

On this stage, we will create a premission and setting a passwod. For this task we will do the following steps:

$ ~/mosquitto-2.0.11$ sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt-spy
$ ~/mosquitto-2.0.11$ sudo mkdir /var/lib/mosquitto/
$ ~/mosquitto-2.0.11$ sudo chown -R mosquitto:mosquitto /var/lib/mosquitto/

The output will be :

mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt-spy
Password: 
Reenter password: 
mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo mkdir /var/lib/mosquitto/
mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo chown -R mosquitto:mosquitto /var/lib/mosquitto/

2. Create configuration file for Mosquitto MQTT Server

Then we will create a new configuration file for MQTT server as shown below :

mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo vi /etc/mosquitto/mosquitto.conf

persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db
log_dest syslog
log_dest stdout
log_dest topic
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
allow_anonymous true
password_file /etc/mosquitto/pwfile
mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo ldconfig

3. Add Systemd Unit File

To manage Mosquitto MQTT service, we have to create a systemd file.  For this purpose we will create a new file called as /etc/systemd/system/mosquitto.service.

mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo vi /etc/systemd/system/mosquitto.service

[Unit]
Description=Insite MQTT Broker

[Service]
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Restart=always

[Install]
WantedBy=multi-user.target

~ 
~ 
"/etc/systemd/system/mosquitto.service" [New File]

4. Starting Mosquitto MQTT service

After systemd file was already created, the next step is to start it service and check the status, is it running normally or not? It will be explained in the following steps.

$ sudo systemctl start mosquitto.service
$ sudo systemctl enable mosquitto.service
$ sudo systemctl status mosquitto.service

The output will be shown below :

mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo vi /etc/systemd/system/mosquitto.service
mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo systemctl start mosquitto.service
mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo systemctl enable mosquitto.service
Created symlink /etc/systemd/system/multi-user.target.wants/mosquitto.service → /etc/systemd/system/mosquitto.service.
mosquitto@otodiginet:~/mosquitto-2.0.11$ sudo systemctl status mosquitto.service
● mosquitto.service - Insite MQTT Broker
Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-06-27 02:02:34 PDT; 19s ago
Main PID: 35674 (mosquitto)
Tasks: 1 (limit: 9448)
Memory: 992.0K
CGroup: /system.slice/mosquitto.service
└─35674 /usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

Jun 27 02:02:34 otodiginet systemd[1]: Started Insite MQTT Broker.
Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: mosquitto version 2.0.11 starting
Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Config loaded from /etc/mosquitto/mosquitto.conf.
Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Starting in local only mode. Connections will only b>
Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Create a configuration file which defines a listener>
Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: For more details see https://mosquitto.org/documenta>
Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Opening ipv4 listen socket on port 1883.
Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: Opening ipv6 listen socket on port 1883.
Jun 27 02:02:34 otodiginet mosquitto[35674]: 1624784554: mosquitto version 2.0.11 running

C. MQTT Testing

At this stage, we will do a simple test to show how Mosquitto MQTT works. On the MQQT protocol, we will get acquainted with ‘topic’ term. Topic is a string that the server/broker uses to filter messages for the connected clients. To subscribe to a topic, we havet to submit the mosquitto_sub -t command line followed by the name of the topic that you want to subscribe to.

1. Server
On the server we will create a topic by using command line : mosquitto_sub -v -t 'otodiginet/test' -u mqtt-spy -P <password>

2. Client
Sending message with the same topic as server has. Submitting command line :  mosquitto_pub -t ‘otodiginet/test’ -u mqtt-spy -P <password>

Mosquitto MQTT Testing

Conclusion

On this short tutorial, we have shown you how to install Mosuitto MQTT message broker application version 2.0 by using source file from Mosquitto MQTT official website, and taking a simple testing to show, who MQTT protocl work. I hope this article will be helpful.

(Visited 651 times, 1 visits today)

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *