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:

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
sudo wget
sudo wget

Then update apt information:

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

Upgrading on Pi


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.


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


Testing The Install

To test it is running use command:

  • netstat –at


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


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.


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


and a publisher client



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]


  1. I have installed mosquitto broker on ubuntu I used apt-get update,apt-get upgrade followed by installation of mosquitto and mosquitto-clients. I had installed it once and since some commands werent working i removed then using sudo apt-get purge –remove mosquitto* and sudo apt-add-repository –remove ppa:mosquitto-dev/mosquitto-ppa.I am able to publish subscribe to topics but some of the commands like systemctl,mosquitto_passwd and mosquitto -v commands dont seem to work.I get command not found.Have i done anything wrong during installation?Thankx in advance.

    1. Hi
      It looks like you have removed the broker and left the clients. I would install again.
      to start and stop use
      sudo service mosquitto stop

      and not systemctl

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

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

  4. 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 *