MQTT broker installation: Mosquitto

What is Mqtt?#


Messaging Queuing Telemetry Transport, or Mqtt, was created by Andy Stanford-Clark (IBM) and Arlen Nipper (EuroTech).

The initial objective was to interconnect remote sites via low bandwidth, to send, receive and process telemetry data.

Today, it is widely used in the IoT world. This is the context in which we’ll be using it.

This service operates via a publish/subscribe mechanism, with an agent providing the service: the broker

Objectives#

Cover the Mosquitto broker installation process and provide the following services:

  • Publish the service on TCP port 1883
  • Service accessible via user & password.

Prerequisites#

A platform running OpenBSD, here our RockPro64

Installation#

Install the package:

doas pkg_add mosquitto

Manage service at startup:

doas rcctl enable mosquitto

Configuration#

Uncomment and configure the following two lines:

code: /etc/mosquitto.conf

# =================================================================
# Default listener
# =================================================================
# IP address/hostname to bind the default listener to. If not
# given, the default listener will not be bound to a specific
# address and so will be accessible to all network interfaces.
# bind_address ip-address/host name
# bind_address 127.0.0.1
bind_address mon.ip.domain.local

# Port to use for the default listener.
port 1883

Revoke anonymous access; uncomment the following two lines (allow_anonymous & password_file):

code: /etc/mosquitto.conf

# =================================================================
# Security
# =================================================================
(...)
# Defaults to true if no other security options are set. If any other
# authentication options are set, then allow_anonymous defaults to false.
#
allow_anonymous false
(...)
# See the TLS client require_certificate and use_identity_as_username options
# for alternative authentication options. If an auth_plugin is used as well as
# password_file, the auth_plugin check will be made first.
password_file /etc/mosquittp/pwd.txt
Mosquitto supports authentication using ssl certificates. These features will not be implemented in our case.

Application user management#

doas touch /etc/mosquitto/pwd.txt
doas echo "foo:aCrazyPassword" >> /etc/pwd.txt
doas mosquitto_passwd -U /etc/mosquitto/pwd.txt

cat /etc/mosquitto/pwd.txt
foo:$6$QfkyvDluHIPNxHZ3$86S/YJq4xHejjfBRmSO9nR5ko2U0N6DRvLWH28Z6Ajrg7uv2haVPzYKMO24VfbiiuCBrTyYQmVZn5xZH7ODP+Q==

If necessary, add a second user:

doas echo "toto:totoPassword" >> /etc/mosquitto/pwd.txt
doas mosquitto_passwd -U /etc/mosquitto/pwd.txt

doas cat /etc/mosquitto/pwd.txt
foo:$6$QfkyvDluHIPNxHZ3$86S/YJq4xHejjfBRmSO9nR5ko2U0N6DRvLWH28Z6Ajrg7uv2haVPzYKMO24VfbiiuCBrTyYQmVZn5xZH7ODP+Q==
foo:$6$sfEnzZJZjlegrkLL$pzkn5uNykUSzLFkvFoN5aTvMenY8GLMc7ASaHNPlB0n0q2SCesOYopZdWMM1S+NpP8Ql9x6vnmME0kyx/XeYQw==

Service startup#

doas rcctl start mosquitto

Hello, can you hear me?#

Open 2 terminals. One for publication, the other for subscription. Let’s start with subscription.

Terminal #2 - subscribe to: install/mosquitto

mosquitto_sub -h mon.ip.domain.local -p 1883 -u foo -P aCrazyPassword -t install/mosquitto

Terminal #1 - publish to: install/mosquitto

mosquitto_pub -h mon.ip.domain.local -p 1883 -u foo -P aCrazyPassword -t install/mosquitto -m "Oye, oye, the mosquito installation is finished, what a success!"
mosquitto_pub -h mon.ip.domain.local -p 1883 -u foo -P aCrazyPassword -t install/mosquitto -m "Thanks, and see you soon for new topics."

Message received on terminal 2:

Oye, oye, the mosquito installation is finished, what a success!
Thanks, and see you soon for new topics.

References#