How to Install The Mosquitto MQTT Broker on Linux

In this tutorial we will look at how you install and run the mosquitto MQTT broker on a Linux server running for example Raspberry Pi or Ubuntu.

Here are the steps I used on Ubuntu.

  • sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
  • sudo apt-get update
  • sudo apt-get install mosquitto
  • sudo apt-get install mosquitto-clients
  • sudo apt clean

For Raspberry Pi the instructions are taken from  here:

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
sudo apt-key add mosquitto-repo.gpg.key

Then make the repository available :

cd /etc/apt/sources.list.d/

Then , depending on which version of debian you are using:

sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-buster.list

Then update apt information:

sudo apt-get update
sudo apt-get install mosquitto
sudo apt-get install mosquitto-clients

Upgrading on Pi

Running

sudo apt-get install mosquitto

should update the existing installation but it may not upgrade as it is dependent on the repository which is different for the version of PI you are Running.

Stopping and Starting

Mosquitto is installed as a service and should start automatically after install

To Stop and start the service I needed to use

  • sudo service  mosquitto stop
  • sudo service mosquitto start #see note later

Most sites I discovered where using the format.

  • sudo /etc/init.d/mosquitto stop

However it seems newer installations use upstart initialization system instead of the init.d initialization system.

The stop/start scripts start the mosquitto broker in the background and also use the default mosquitto.conf file in the /etc/mosquitto/ folder.

mosquitto-folder-linux

The mosquitto binary is located in the /usr/sbin folder

If you want to see the control messages on the console then you need to start the mosquitto broker manually from a command line.

You first need to to stop the broker from running, and then type:

mosquitto -v

start-mosquitt-linux

Testing The Install

To test it is running use command:

  • netstat –at

netstamqtt-broker

You should see the Mosquitto broker running on port 1883 as shown in the screen shot above.

Below is a screen shot showing testing using the mqtt-ping command

mosquitto-broker-test

Testing Using Mosquitto

If you are doing tests with mosquito then I recommend you stop the installed service and run mosquito manually from the command line.

If you do it this way you can create various configuration files for testing.

In addition, I would recommend you place the test configuration files and other files like password files in the local use folder as it avoids any permission errors, and they are easily edited without requiring root permissions.

Starting Mosquitto Using a Configuration file

The configuration file (mosquitto.conf) that comes with the install is mainly completely commented out as mosquitto doesn’t need a configuration file to start.

This file is loaded by the mosquitto when starting as a service.

Before you edit the default configuration file it is a good idea to create a copy of this file.

However I wouldn’t use this file as a basis for you own as if you do it is difficult to find any of you changes that a buried inside this file.

If you have done this then you might find my python configuration file tool useful.

To start mosquitto using a configuration file use the following command:

  • mosquitto -c filename

You can find the mosquitto.conf template file in the /etc/mosquitto/ folder.

It is a good idea to create a copy of this file before editing it.

Note: For testing it is easier to use a configuration file in your home directory rather than the /etc/mosquitto folder as you need root permissions to edit files in this folder.

I use: /home/steve/mos/mosquitto.conf

Verbose Mode

This is useful for troubleshooting use

mosquitto -v

Running Multiple Mosquitto Brokers

You can configure a broker to listen on several ports, but to create multiple brokers with their own configurations then you will need to start multiple instances of mosquitto.

See Quick Guide to the Mosquitto.conf file and this video on running multiple brokers.

Examples:

Start mosquitto and listen on port 1883

mosquitto -p 1883

Start mosquitto as a daemon and listen on port 1884

mosquitto -p 1884 -d

Start mosquitto as a daemon and use the mosquitti-2.conf file.

mosquitto -c /etc/mosquitto/mosquitto-2.conf -d

Useful Linux Commands

To stop Mosquitto when running as a daemon:

ps -aux | grep mosquitto
pgrep mosquitto
kill -9 PID (that you get from above command)

Mosquitto Client Scripts

On Linux they need to be installed using

  • sudo apt-get install mosquitto-clients

There is a simple subscriber client

mosquitto_sub

and a publisher client

mosquitto_pub

Use

mosquitto_sub –help

They are useful for some quick tests and troubleshooting.

See Using The Mosquitto_pub and Mosquitto_sub Client Tools- Examples

Other Tools

MQTTlensMQTTlens is also very useful for troubleshooting and quick testing. It is an add-on for the chrome browser.

It lets you publish and subscribe to topics using a web interface, and is much easier to use than the command line clients.

mqtt-ping-icon MQTT-Ping -Python Tool written by me that works like the traditional IP ping command tool.

Note: Starting will mosquitto version 1.6 mosquitto now supports MQTT v5.



Mosquitto Configuration Tutorials

Other Related Articles and Resources:

Please rate? And use Comments to let me know more
[Total: 5   Average: 5/5]

8 comments

  1. Hi Steve

    ‘Thanks for your guide, when I enter mosquitto -v it returns ‘command not found’, however if I put sudo in front of it, it works ?

    What have I done wrong, I was sure I followed your instructions to the letter ?

    1. Hi
      You shouldn’t need sudo. Try the install again. Also try whereis mosquitto it maybe that you don’t have permissions in the folder where it has been installed.
      Rgds
      Steve

  2. Thanks for this it was very useful. I think it should be “sudo service mosquitto start” instead of “sudo service start mosquitto”, as service puts the object before the command.

  3. Thanks so much for this article, every other install guide misses completely the step to import the repos, which is baffling because otherwise nothing works. Thanks for being thorough in your explanation.

Leave a Reply

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