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:

curl -O http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key
rm mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo curl -O http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo apt-get update
sudo apt-get install mosquitto

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;

sudo apt-get install mosquitto

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.

sudo systemctl stop mosquitto
sudo update-rc.d mosquitto remove
sudo rm /etc/init.d/mosquitto

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

sudo nano /etc/systemd/system/mosquitto.service

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

[Unit]
Description=MQTT v3.1 message broker
After=network.target
Requires=network.target

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

[Install]
WantedBy=multi-user.target

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.

sudo systemctl daemon-reload

Enable Mosquitto service to start at boot;

sudo systemctl enable mosquitto

Starting Mosquitto service;

sudo systemctl start mosquitto.service

Password protecting MQTT Server:

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

sudo mosquitto_passwd -c /etc/mosquitto/accesslist.txt pi

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

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

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;

sudo systemctl stop mosquitto
sudo systemctl start mosquitto

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

2.3 3 votes
Article Rating
Subscribe
Notify of
guest

12 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Davo
Davo
6 years ago

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!

Danie
Danie
6 years ago

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.

Danie
Danie
6 years ago

Thank you for your prompt response. I followed all steps above twice already, but mosquitto still does not start on boot?

Danie
Danie
5 years ago

The commands were all executed successfully, but MQTT still does not start after a reboot. Running “sudo chown mosquitto /var/log/mosquitto” again leads to a message “chown: cannot access ‘/var/log/mosquitto’: No such file or directory”. The “mosquito” directory is visible in /var/log, but it is empty

Danie
Danie
5 years ago

Dear Waqas

sudo chown mosquitto:mosquitto /var/lib/mosquitto/ -R did not help.

Commenting out “log_dest file /var/log/mosquitto/mosquitto.log” in “/etc/mosquito/mosquitto.conf” allows MQTT to auto start after Pi reboot.

Directory “/var/log/mosquito” (and therefore also file “mosquitto.log”) gets deleted whenever the Pi is rebooted, which causes MQTT to fail upon Pi reboot.

What is deleting the directory “/var/log/mosquito” upon Pi reboot, can it be “log2ram” causing this?

Francesco
Francesco
5 years ago

The linked config file has everything commented out… What’s the benefit of copying it verbatim if its only comments?

12
0
Would love your thoughts, please comment.x
()
x