How to install Mosquitto on Raspberry Pi (2023)

Updated:November 5, 2022Von Steve

Mosquitto is a very lightweight broker and a Raspberry Pi can easily handle MQTT traffic on clients in smart home networks.

Mosquitto is not installed by default, so you must install it.

installation steps

First get repository key

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.keysudo apt-key agregar mosquitto-repo.gpg.key

Then make the repository available:

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

So, depending on the 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
sudo wget http://repo.mosquitto.org/debian/mosquitto-bullseye.list

To check which version you are using

cat /etc/os-release

Then update the apt information:

sudo apt-get update
sudo apt-get install moskito
sudo apt-get install mosquito clients

For Windows and Linux, seeit's tutorialon my other side

Starting and configuring Mosquitto

Setup installs Mosquitto as a service that automatically starts and tunes to port 1883.

If you need to change the configuration, the configuration file is calledmosquito.confand is in/etc/mosquitomaps

Mosquitto can also be controlled on the Pi
systemctl command. You can modify, start and restart the service with .

sudo systemctl stop mosquitto.servicesudo systemctl start mosquitto.servicesudo systemctl restart mosquitto.service

More details like B. how to run multiple instances, change port, etc. see my other websiteon here.

Run Mosquitto Manually

When testing, you will find it very useful to run mosquitto manually from the command line.

The first step is to stop mosquitoes using:

sudo systemctl stoppt mosquitto.service

Then you can start with:

mosquito -v #start in verbose mode

The -v option displays detailed log information. Other command line options are:

  • -p port number
  • -c config file

examples
To start Mosquitto with a test configuration file, use

(Video) Install Mosquitto Broker on Raspberry Pi and Test it

mosquito -c test.conf

To start Mosquitto with port 1884, use

Mosquito -p 1884

Testing the installation

If you followed the installation instructions, you also installed the Mosquitto trial clientsmosquito_pubymosquito_sub.

You can use these clients to test your Mosquitto installation.

The first step is to subscribe to a topic and then post a message about that topic, which you should see in the subscription client.

So first open two command prompts in one type.

mosquito_sub -h localhost -t teste -d

not second type

mosquito_pub -h localhost -t test -m message

You should see the message in the signature window.

How to install Mosquitto on Raspberry Pi (1)

Related tutorials and resources:

Please let me know if you found it helpful.

[No total:10Average:4.5]

  1. First, thank you for the effort you've put into these guides. You have been a great help. My initial "toy" setups worked fine and easily. Then it's time to add the security and deployment version.
    I have struggled with inscrutable errors, permission errors for pid and log and conf files.
    After a few days of fixing the errors by opening the PID and log files of all users and groups, I noticed that in my actual config file I had specified "user mosquito" in the config file for the first time. When I looked at the permissions on /etc/mosquitto and all its contents, the owner and group was root. The same applies to the default permissions for the PID and log files.
    I find that when installed all Moskito files and directories are rooted to owner and group. At least half a dozen clean reinstalls I've done have this. So, little by little, I fixed the permissions for the "mosquito" user.
    For security reasons, I want the Mosquitto user to run and own the program and its files. Is there a clean, non-bulky way to do this? I would prefer the Mosquitto program's execution and files to be owned by the Mosquitto user, so they don't have to be owned by "pi" or "root" with the higher access levels that go with that.

    answers

    1. Setup must set the correct permissions. I've installed it on multiple Pis and haven't encountered any permission issues.
      You get one when you create a mosquito on the command line. Is that what you do? If so, set the database PID and location to your local folder and it will work fine.
      Owner
      Steve

      answers

      1. I tried installing packages with apt-get and with the Synaptics package manager. Both have the same issues as a manual command line install. They work when I mean originals, they have problems when I start retouching.
        I think I found the root of the problem without fixing anything. I read the mosquitto.conf man page for the 47th time and looked at the section on how to specify a user.
        I set the user line to "mosquito user". This is of course the default, but I have a personal habit of explicitly defining defaults that I will rely on. When I commented out that line, everything was fine and everything worked. Uncommenting to set the user to "mosquito" brought back the permission issues. I haven't looked at the source code to be sure, but I believe from the mosquitto.conf man page that mosquitto performs permissions and user gymnastics whenever there is a setting there.
        I searched extensively for references to this permission issue and none of the references were "Create the files and set the permissions". The real solution for inexperienced users (... like me) is probably NOT changing user parameters in the config file.

        (Video) Pi Guide - Raspberry Pi as a MQTT Broker | Install Mosquitto on Raspberry Pi

        answers

        1. ola
          As I mentioned before, when running as a background service, no permission changes are required, at least I didn't.
          However, changing file locations in the configuration file causes permission issues.
          Rgds
          Steve

          answers

  2. Thanks for this tutorial!
    The only downside I found was the gpg. The command
    sudo apt-key add mosquitto-repo.gpg.key
    generates a warning "apt-key is deprecated. Manage keyring files in Trusted.gpg.d instead."
    I used a solution I found on another site where gpg imported the .key file, then exported the gpg file and put the file in Trusted.gpg.d, but it doesn't seem to be the "correct" way, although it works. 🇧🇷
    It would be great if you could take a look.
    Jeroen

    answers

  3. it worked for me

    update sudo apt
    sudo apt install -y mosquitto mosquitto-clients

    answers

  4. Hello, I have MQTT installed on RPI 4 and 4g, installed on Docker with the allow_anonymous listener they say is running, but there is an error address that is not available. Is there a solution as I can't find one when searching the web for a solution that caused me to switch to eclipse 1.6 image? Is this a solution?
    I hope you can help?

    answers

    1. It looks like a port problem and not a mosquito problem. Make sure no other instances of mosquitto are running.
      Rgds
      Steve

      answers

  5. Hi!
    First thanks for the great guide. There is a lot of information available, but most of it is out of date.
    I have the same problem as John, a clean install of Homebridge and Mosquitto gave me V2.0.9.
    From this moment on, MQT communication no longer works.
    MQT: connection attempt...
    MQT: connection failed, rc -2. Try again in 60 seconds

    I commented out my old setup (V1) and left your suggestion but still no connection:
    allow_anonymous verdadero
    Password date /etc/mosquitto/passwd
    listeners 1883

    Any other ideas on how to fix this?
    Thanks in advance!
    cris

    (Video) Raspberry Pi install mosquitto MQTT broker

    answers

    1. Remove the line from the password file unless you are using passwords. If you use passwords, remove the allow_anonymous line.

      answers

  6. First of all thank you for all of this. NB: The following is likely a specific date: March 5, 2021.

    I'm testing things out on multiple Raspberry Pis, with the runner installed on an old Model B running as a NAS, then posting MQTT from an Rpi 400 to the kitchen table (probably not a one-time setup). Everything works fine until I get to the websockets tutorial - the problem is finding examples of a local config file. As far as I can see, Mosquitto doesn't have a default config file, you must create your own.

    On pins, sudo apt install mosquitto mosquitto-clients after upgrade and full upgrade installs mosquitto version 1.5. So I followed the install instructions above (I'm running Buster) to see if I could get v1.6. However, I now have version 2.0.8 which works fine when I try to subscribe and post via two terminal windows on the NAS. However, my Python script (using your instructions) on the Pi kitchen table (which worked fine) now says "socket.error: [Errno 111] Connection Reweed" when I try to make a client.connect connection

    This is most likely a broker configuration issue. I'm currently trying to figure this out and I'm assuming I'll have to come up with my own setup. Perhaps Mosquitto v 2 is a bit more demanding on permissions?

    Thanks again and any help is greatly appreciated.
    John

    answers

    1. Mosquitto 2's default configuration is more secure than a basic configuration file so it will continue to function as in previous versions.

      listeners 1883
      allow_anonymous verdadero

      Just create this simple file and copy it to /etc/mosquitto/mosquitto.conf as mosquitto.conf
      o utilícelo al manually start Mosquitto as
      mosquito -c conffile
      rgds
      Steve

      answers

  7. This only installs an updated 1.6.x series on the Raspberry Pi, but 2.0.x on Ubuntu.
    I'm having trouble getting one broker to send issues to another on the 1.6.x series (random socket errors and reconnect difficulty) which seems to work on the 2.0.x series on Ubuntu, but I'd really like to use the Pi.

    answers

    1. I don't think v2 is still available for the Pi as it's a different chipset. I didn't see any socket errors on the Pi, how do you test?
      rgds
      Steve

      (Video) install mosquitto on raspberry pi

      answers

      1. I installed Moskitto on a new Raspberry Pi on March 11, 2021 using the method provided here and (without doing anything special) got version 2.0.9. Of course, v2 is already available on Raspbian.

        I also updated an existing Raspberry Pi running Moskito 1.6.12 to 2.0.9 following the order listed above:
        sudo apt-get update
        sudo apt-get install moskito
        sudo apt-get install mosquito clients

        answers

  8. Thanks, this method only worked for me to install Mosquitto on Rpi 3.

    answers

  9. I think your second test window should have `mosquitto_pub -h localhost -t test -m message`

    answers

    1. yes i changed
      Rgds
      Steve

      answers

leave a message

We use cookies on our website to give you the most relevant experience, remembering your preferences and repeat visits. By clicking "Accept All" you agree to the use of ALL cookies. However, you can visit the "Cookie Settings" to give controlled consent.

Videos

1. Installing Mosquitto Server (MQTT) on Raspberry Pi 3
(Dr. Anand Nayyar)
2. Installing MQTT on Raspberry Pi - Mosquitto MQTT
(Tasty Tech Harbour)
3. AWS IoT | How to install Mosquitto Broker on Amazon EC2
(That Project)
4. Install Mosquitto MQTT broker with authentication on Raspberry Pi
(GAURAVDS)
5. How to install and Run MQTT(Mosquitto) Server and Client on Raspberry/Ubuntu System
(EmbeddedVisions)
6. IoT x01: Install Mosquitto on a Raspberry Pi
(Heinz Peterschofsky)
Top Articles
Latest Posts
Article information

Author: Aron Pacocha

Last Updated: 03/01/2023

Views: 5395

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Aron Pacocha

Birthday: 1999-08-12

Address: 3808 Moen Corner, Gorczanyport, FL 67364-2074

Phone: +393457723392

Job: Retail Consultant

Hobby: Jewelry making, Cooking, Gaming, Reading, Juggling, Cabaret, Origami

Introduction: My name is Aron Pacocha, I am a happy, tasty, innocent, proud, talented, courageous, magnificent person who loves writing and wants to share my knowledge and understanding with you.