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.

Hardware Note:

This was pointed out in the comments and not being really too much into hardware I was aware of but think it important to point out sp here is the comment.

No support in repo for arm, arm64 or aarch64?  So only armhf is supported?  You should call this out somewhere in your article.

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

Mosquitto 2..0.2 and above

Mosquitto v2 introduced some important changes that affect first time users in particular.

By default it requires authentication and doesn’t listen on a network address.

The following simple configuration file will make mosquitto start like previous versions:

listener 1883
allow_anonymous true

Connection Limits

You can limit the number of client connections in the mosquitto.conf file but the default configuration doesn’t impose limits.

However the OS does and it is 1024 for each user. This limit is the max number of open files allowed.

This can be changed for each user by editing the limits.conf file (/etc/security/limits.conf).

limits-file

Note: You will need to restart the system for the setting to take affect

You can check the limits by using the ulimit -Sn (soft) or ulimit -Hn (hard command as shown above.

However this only affects mosquitto started from the command line. If you start mosquitto as a service then you need to edit the /lib/systemd/system/mosquitto.service file.

and adding the line

LimitNOFILE=4000 #(example limit)

mosquitto-service

Note: You will need to restart the system for the setting to take affect

If you the go to the command line you can check the limits by getting the process id and using the command:

cat /proc/<pid>/limits

as shown below:

show-limts-running process

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.


coffeeIf you found this guide useful then perhaps you would like to Buy Me a Coffee

Mosquitto Configuration Tutorials

Other Related Articles and Resources:

Please rate? And use Comments to let me know more

39 comments

  1. Hi Steve! First of all, thank you very much for the excellent material. Helps many here.

    I’m trying to compile Mosquitto from the source code (which is in the oficial github repository). After resolving some dependencies, make install finishes well. When testing mosquitto_pub, I ran into the problem of missing libmosquitto.so.1, which was not in /var/lib. I copied it from the source code folder and managed to publish a message to the hivemq broker. However, I cannot publish to the local broker (connection refused). I can’t find the log (it’s not in /var/log) and mosquitto.conf is only in the folder I downloaded from the repository. I’ve already tried changing allow_anonymous in it and recompiling later, but the same problem remains.

    When trying to check the service status, the following lines are displayed:
    “`
    ● mosquitto.service – Mosquitto MQTT Broker
    Loaded: loaded (/etc/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
    Active: activating (auto-restart) (Result: exit-code) since Wed 2023-10-25 10:46:51 BST; 7s ago
    Process: 11395 ExecStart=/usr/local/bin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited, status=203/EXEC)
    Main PID: 11395 (code=exited, status=203/EXEC)
    CPU: 577us
    “`
    Any idea where I might be going wrong?

    1. Hi
      Been a very long time since I compiled mosquitto. I would try creating a new config file with allow anonymous and the default listener and run it using
      mosquitto -c myconf.conf -v

      and see if it works
      rgds
      steve

  2. No support in repo for arm, arm64 or aarch64? So only armhf is supported? You should call this out somewhere in your article.

  3. 2023-02-19 RaspberryPi ZeroW, Buster …
    Have Node-RED installed and working on Pi and Ubuntu laptop. Client on a Pico works with Ubuntu.
    Having trouble getting Mosquitto installed on PiZeroW i.e.
    wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key,
    gives ERROR certificate of ‘repo.mosquito.org’ is not trusted, has expired
    which is likely why
    sudo apt-key add mosquitto-repo.gpg.key,
    gives mktemp: failed to create directory via template …
    Mosquitto.org had these lines listed in 2013 and 2018 but maybe 2023 is a tad beyond the fringe!

  4. Hi Steve,

    thanks for the great article.
    I have followed all the steps to install mosquitto and clients on raspberry-PI.

    when tried with local-host for pub and sub on the raspberry-PI its working.
    But i try replacing with the Raspberry-PI’s IP address instead of local-host in below command it doesn’t work. can you please provide any valuable insights:

    pi@raspberrypi:~ $ mosquitto_pub -h 192.168.146.20 -t testing -m “hellofromPI” -p 1883
    Error: Connection refused
    pi@raspberrypi:~ $

    ***********************************************************************************
    please find below additional details:

    pi@raspberrypi:~ $ mosquitto -v
    1661826620: mosquitto version 2.0.11 starting
    1661826620: Using default config.
    1661826620: Starting in local only mode. Connections will only be possible from clients running on this machine.
    1661826620: Create a configuration file which defines a listener to allow remote access.
    1661826620: For more details see https://mosquitto.org/documentation/authentication-methods/
    1661826620: Opening ipv4 listen socket on port 1883.
    1661826620: Opening ipv6 listen socket on port 1883.
    1661826620: mosquitto version 2.0.11 running

    pi@raspberrypi:~ $ mosquitto -version
    Error: Unknown option ‘-version’.
    mosquitto version 2.0.11

    mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker.

    Usage: mosquitto [-c config_file] [-d] [-h] [-p port]

    -c : specify the broker config file.
    -d : put the broker into the background after starting.
    -h : display this help.
    -p : start the broker listening on the specified port.
    Not recommended in conjunction with the -c option.
    -v : verbose mode – enable all logging types. This overrides
    any logging options given in the config file.

    See https://mosquitto.org/ for more information.

    pi@raspberrypi:~ $

    pi@raspberrypi:~ $ uname -a
    Linux raspberrypi 5.15.32-v7+ #1538 SMP Thu Mar 31 19:38:48 BST 2022 armv7l GNU/Linux
    pi@raspberrypi:~ $

    ****************************************************************************************

  5. Hi!
    I have ubuntu 20.04 installed on my laptop (windows 11 home). im using the latest version of mosquitto too.
    when i add lines for port 1883 and 9001 (protocol websockets) and allow_anonymous true to a test conf in my home directory, i see the lines that is is opening the ports but when i go to test it, it doesnt actually work. using netstat, i see that the ports are bound to localhost, however i have my firewalls open for both port 1883/9001. i really dont know what im doing wrong… ive done all the correct steps and it seems to show that its understoood my conf file (as it says its opening the ipv4/6 sockets) but its not actually applying the changes

    1. how are you testing are you using mosquitto_pub? If so use the ip address and not 127.0.0.1 if that is what you are using.
      Rgds
      Steve

      1. hello! i am using mosquitto_sub -h xxxxxxxx -t “mia/home/#” -p 1883
        where xxxxxx is my IP address
        when i do this, it says connection refused so i cant even try publishing….

        1. Connection refused is either it can’t find the machine or the port .If the coonsoles displays the ports as open then I would suspect firewall settings. When you say ubuntu and windows is ubuntu in a virtual machine.
          Rgds
          Steve

          1. i dont think it is, when you open file explorer, its under a “Linux” tab – its WSL i guess?

  6. I am trying to install mosquitto. I get the following error :

    Process: 22646 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
    Process: 22651 ExecStartPre=/bin/chown mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
    Process: 22655 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
    Process: 22660 ExecStartPre=/bin/chown mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
    Process: 22666 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=killed, signal=ILL) <————————————————————————————— error error error
    Main PID: 22666 (code=killed, signal=ILL)

    Feb 11 09:54:24 raspberrypi systemd[1]: mosquitto.service: Service RestartSec=100ms expired, scheduling restart.
    Feb 11 09:54:24 raspberrypi systemd[1]: mosquitto.service: Scheduled restart job, restart counter is at 1.
    Feb 11 09:54:24 raspberrypi systemd[1]: Stopped Mosquitto MQTT Broker.
    dpkg: error processing package mosquitto (–configure):
    installed mosquitto package post-installation script subprocess returned error exit status 1
    Processing triggers for systemd (241-7~deb10u8+rpi1) …
    Processing triggers for man-db (2.8.5-2) …
    Processing triggers for libc-bin (2.28-10+rpi1) …
    Errors were encountered while processing:
    mosquitto
    E: Sub-process /usr/bin/dpkg returned an error code (1)

    1. It looks like it installed but can’t start. I can only suggest you try and install it again and see if that changes anything. Did you use sudo to do the install?
      rgds
      steve

      1. Also tryed to install Mosquitto on Bullseye and failed with the same error
        > dpkg: Fehler beim Bearbeiten des Paketes mosquitto (–configure):
        > »installiertes mosquitto-Skript des Paketes post-installation«-Unterprozess gab den Fehlerwert 1 zurück
        >Fehler traten auf beim Bearbeiten von:
        > mosquitto
        > E: Sub-process /usr/bin/dpkg returned an error code (1)
        Previously installing on Buster works with the same procedure!
        Only difference SDcard has 16GB instead of 32Gb with Buster … but don’t think that’s the problem.
        (Multiple attempts to install/remove/install didn’t solve the situation)

  7. I experienced tons of trouble connecting. Then I found Steves hint to add the repo to my Ubuntu (halve dozen machines) “sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa” Suddenly it works! The new 2.x version solves the endless trouble with pseudo-occupied ports and unsuccessful CONNECT attempts. THANK YOU SO MUCH!! Most of Steves stuff should appear in the official howtos and manuals (poorly maintained anyways). Great job, Steve!

  8. Hi Steves.
    Now able to communicate between pi and ubuntu. pi a broker and ubuntu as client. SSL communication
    pi (running broker)
    1607426140: New connection from 10.42.0.1 on port 8883.
    1607426140: Client disconnected due to protocol error.

    ubuntu
    abhishek@dell-Inspiron-3584:~$ mosquitto_pub –cafile /home/abhishek/ssl_certificates/certs/ca/ca.crt -h 10.42.0.118 -t “topic” -m “Abhishek” -p 8883
    Error: A TLS error occurred.

    Same message getting while subscribing mosquitto_pub.

    Mosquitto Conf file

    port 8883
    require_certificate true

    cafile /home/pi/ssl_certificates/certs/ca/ca.crt
    certfile /home/pi/ssl_certificates/certs/broker/broker.crt
    keyfile /home/pi/ssl_certificates/certs/broker/broker-nopass.key

    pid_file /var/run/mosquitto.pid
    persistence true
    persistence_location /var/lib/mosquitto/
    log_dest file /var/log/mosquitto/mosquitto.log
    include_dir /etc/mosquitto/conf.d

    Please help

  9. Hi Steve,
    I really enjoy reading your explanation of MQTT. Its crystal clear! But I on my netstat it didn’t appear like yours. I wish I can upload pictures here but it’s okay.
    My result didn’t appear port 1883, it appears other numbers than 1883:
    58972, 52620,57972, 57970 and many more
    Did my mosquitto install well? Thank you in advance!

    1. Not sure but if you start mosquitto from a command line using mosquitto -v you either get it running or it complains that the port is already used in which case it is already running

  10. Hi Steve
    i am getting a error
    1601973829: mosquitto version 1.6.10 starting
    1601973829: Using default config.
    1601973829: Opening ipv4 listen socket on port 1883.
    1601973829: Error: Address already in use
    address already in use,please help me how to solve

    1. HI
      You already have mosquitto running. When you install mosquitto on windows or linux it installs as a service and is started automatically. On linux you can stop it using sudo service mosquitto stop on windows you need to open the services console.

  11. 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
      rgds
      steve

      1. I have installed it again..both mosquitto and mosquitto-clients.I am able to publish and subscribe to topics.sudo service mosquitto stop command works fine sometimes it prompts me to enter the password and sometimes works fine.But sometimes throws an error.also i have observed that mosquitto_hub and mosquitto_sub being installed under /usr/bin folder but cannot find mosquitto_passwd.

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

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

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