Installing secure Mosquitto broker on Raspberry Pi with Raspbian Jessie or Stretch along with systemd start up

Eclipse Mosquitto™ is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. MQTT provides a lightweight method of carrying out messaging using a publish/subscribe model. This makes it suitable for “Internet of Things” messaging such as with low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino.

This is enough for the introduction. In this tutorial we will learn “How to install mosquitto broker on Raspberry Pi. So Let’s get started!!  😎

Installation:

Open up your favourite terminal program such as putty and connect to Raspberry Pi through ssh. Type the following commands one by one for successful installation of Mosquitto broker in your Raspberry Pi.

For Raspbian Jessie:

For Raspbian Stretch:

Due to possible dependencies issue, there is no user friendly way to install latest mosquitto version on Raspbian Stretch. However, one can install the aptitude version as following;

Using systemd start up for Mosquitto:

Mosquitto broker does not using modern system of systemd for start up. In order to move old init system, following the procedure below.

After removing old init.d script for Mosquitto broker, Create systemd unit file using Nano editor;

Paste the following contents in the Nano editor using Right mouse key.

Save the file by pressing “CTRL” and “X” key. Editor will ask for the confirmation to save the file. Press “Y” key and hit “Enter”.

Now reload systemd configuration using the following command.

Enable Mosquitto service to start at boot;

Starting Mosquitto service;

Password protecting MQTT Server:

In order to protect our Mosquitto MQTT Server, we need to create a user by issuing following command.

“pi” is the name of user which will be used in authentication process in future. By issuing the command above, system will ask to enter a password for MQTT broker. Enter the password and press Enter key, confirm it by retyping again and you have successfully completed securing your MQTT server with a password.

Mosquitto configuration:

We also have to make certain adjustment further. Create a new configuration file with the help of nano editor by using the command below.

Copy the contents of mosquitto configuration file and paste the contents into the nano editor the by clicking Right mouse button and save the file using “CTRL” and “X” key followed by “Y” and then hit enter (“Return Key”).

After adding configuration file, restart mosquitto service by issuing following commands;

Thats all, We have successfully configured our Mosquitto broker. Have fun 😎 .

6 thoughts on “Installing secure Mosquitto broker on Raspberry Pi with Raspbian Jessie or Stretch along with systemd start up

  1. Davo says:

    By Mosquitto configuration: you say:
    Paste the following contents to the mosquitto configuration file by clicking Right mouse button and save the file using “CTRL” and “” key followed by “Y” and then hit enter (“Return Key”).

    But there is no contents!

  2. Danie says:

    MQTT does not start automatically after reboot. Starting manually with “mosquitto” or “mosquitto -v” works, but no auto start after reboot. How can this be corrected?

    I am using stretch.

    • Waqas Ahmed says:

      Have you enabled the service? sudo systemctl enable mosquitto Send us your system log file (can be found under /var/log folder) at support [ @ ] hobbytronics.pk.

Leave a Reply

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

For better user experience, please clear your browser cookies and cache at least for once. Dismiss