How to Install Mosquitto on The Raspberry Pi

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

Mosquitto doesn’t come installed by default so you will need to install it.

Install Steps

First get the repo 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
sudo wget
sudo wget
sudo wget

To check which version you are running use

cat /etc/os-release

Then update apt information:

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

For windows and linux see this tutorial on my other site.

Starting and Configuring Mosquitto

The install installs mosquitto as a service which starts automatically and tuns on port 1883.

If you need to change the configuration then the configuration file is called mosquitto.conf and is located in the /etc/mosquitto folder.

On the Pi mosquitto can be controlled using the
systemctl command. You can sopt,start and restart the service using.

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

You can find more details, like how to run multiple instances, change port etc on my other site here.

Running Mosquitto Manually

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

The first step is to stop mosquitto using:

sudo systemctl stop mosquitto.service

Then you can start it using:

mosquitto -v   #start in verbose mode

the -v option displays verbose logging information other command line options are:

  • -p port_number
  • -c configuration file

To start mosquitto using a test configuration file use

mosquitto -c test.conf

To start mosquitto using port 1884 use

mosquitto -p 1884

Testing the Install

If you followed the install instructions you will have also installed the mosquitto test clients mosquitto_pub and mosquitto_sub.

You can use these clients to test your mosquitto install.

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

So first open two command prompts in one type.

mosquitto_sub -h localhost -t test -d

in the second type

mosquitto_pub -h localhost -t test -m message

You should see the message appear in the subscribe window.


Related Tutorials and Resources:

Please Let me Know if you found it Useful
[Total: 10 Average: 4.5]


  1. First off, thank you for the effort you put into these guides. They have been a huge help. My initial “toy” setups worked well and easily. Then it came time to add in the security and deployment version.
    I have been battling inscrutable errors, with permissions errors for the pid and log files as well as the conf files.
    After a couple of days bashing down the errors by opening the pid and log files all users and groups, it occurred to me that in my real-use config file I had specified “user mosquitto” 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. So were the default permissions on the pid and log files.
    I believe that at install, all of the mosquitto files and directories have root for owner and group. At least the half-dozen clean reinstalls I’ve done have this. So I’ve been fixing piecemeal permissions to user “mosquitto”.
    For security reasons, I’d like to have the mosquitto user run and own the program and its files. Is there a clean and not piece-by-piece way to do this? I would prefer to have the program execution and mosquitto files owned by the mosquitto user so that they don’t have to be owned by “pi” or “root” with the attendant higher levels of access.

    1. The install should set up the correct permissions. I have done the install on several pis and not encountered a permission problem.
      You do get one when you mosquitto from the command line. Is that what you are doing? If so set the pid and database location to your local folder and it will work ok.

      1. I’ve tried both package install with apt-get and also with synaptics package manager. Both have the same issues as a manual command line install. They work when bone-original, have issues when I start tinkering.
        I think I may have found the root issue without actually fixing anything. I read through the mosquitto.conf man page for about the 47th time, and noticed the section on setting a user.
        I set the user line to “user mosquitto”. This is, of course, the default, but I have a personal habit of explicitly setting even default values that I will depend on. When I commented out that line, all was well and everything worked. Un-commenting it to set the user to “mosquitto” brought the permissions issues back. I have not pored through the source code to find this out for certain, but I believe, based on the mosquitto.conf man page that mosquitto does permissions and user gymnastics whenever there is a setting there.
        I’ve searched extensively for hints on this permissions thing, and none of the references ever got beyond “create the files and go set the permissions”. The real cure for non-expert users (…like me) is probably to NOT change the user parameter in the config file.

        1. Hi
          As I mentioned earlier when running as a service in the background it doesn’t require any permission changes at least I haven’t had to do any.
          However changing file locations in the config file will give you permission problems.

  2. Thank you for this tutorial!
    The only snag I encountered was with the gpg file. The command
    sudo apt-key add mosquitto-repo.gpg.key
    generates the warning “apt-key is deprecated. manage keyring files in trusted.gpg.d instead”.
    I used a workaround found on another website where gpg imports the .key file, then exports the gpg file, and I put the file in trusted.gpg.d, but that seems not to be the “right” way, although it does work.
    It would be great if you could have a look into this.

  3. Hi I have installed MQTT onto RPI 4 & 4g, installed in docker with the allow_ anonymous – listener it states running , however there is an error Address not Available . Is there a solution as i cannot find one trawling the web for a solution brought me to downgrade to eclipse 1.6 image. Is this a solution?
    Hope you can help?

    1. It looks like a port issue and not a mosquitto issue. Check that no other instance of mosquitto is running.

  4. Hi there!
    first of all thanks for the great tutorial. There is a lot of info out there , but most of it is outdated.
    I have the same issue as John has, a fresh install of homebridge and mosquitto that gave me V2.0.9.
    From this point onwards, the MQT communication is not working anymore.
    MQT: Attempting connection…
    MQT: Connect failed, rc -2. Retry in 60 sec

    I have commented my previous (V1) config and left only your proposal, but still no connection:
    allow_anonymous true
    password_file /etc/mosquitto/passwd
    listener 1883

    Any other ideas on how to solve this?
    Thanks beforehand!

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

  5. Firstly, thanks for all this. NB the following is probably date specific – 5th March 2021.

    I’m trying things out on multiple raspberry pi’s with the broker installed on an old Model B running as a nas, then MQTT publishing from a Rpi 400 on the kitchen table (prob not a unique setup). All working fine until I get to the websockets tutorial – the problem being finding examples of a local config file. As far as I can see, mosquitto by default doesn’t have a config file, you have to create your own.

    On the pi nas, after doing update and full-upgrade, sudo apt install mosquitto mosquitto-clients installs mosquitto version 1.5.7 which is a bit old and others say they’re using v 1.6. So, I followed your install instructions above (I’m running buster) to see if I could get v 1.6. However, I now have version 2.0.8, which works fine trying out subscribing and publishing via two terminal windows on the nas. However, my python script (using your instructions) on kitchen table pi (which was working fine) now says ‘socket.error: [Errno 111] Connection refused’ when trying a client.connect

    Presumably, this is a configuration issue on the broker. Currently, I’m trying to figure this out and assume I have to create my own config. Maybe mosquitto v 2 is a bit fussier with permissions?

    Thanks again and any help much appreciated.

    1. Mosquitto 2 default config is more secure a basic config file to make it work as per earlier versions is

      listener 1883
      allow_anonymous true

      just create this simple file and either copy it as mosquitto.conf to /etc/mosquitto/mosquitto.conf
      or use it when manually starting mosquitto as
      mosquitto -c conf_file

  6. This only installs a updated 1.6.x series on Raspberry Pi but 2.0.x on Ubuntu.
    I have problems with getting one broker sending topics to another on the 1.6.x series (random socket errors and difficulty re-connecting) that appears to work find on the 2.0.x series on Ubuntu, but I would really like to use the Pi.

    1. I don’t think the v2 is available for the pi yet as it is a different chipset. I haven’t seen socket errors on pi how are you testing?

      1. I installed mosquitto on a new Raspberry Pi using the method provided here on March 11, 2021, and (without doing anything special) I got version 2.0.9. So evidently v2 is now available on raspbian.

        I also upgraded an existing Raspberry Pi with mosquitto 1.6.12 to 2.0.9 by following the sequence given above:
        sudo apt-get update
        sudo apt-get install mosquitto
        sudo apt-get install mosquitto-clients

Leave a Reply

Your email address will not be published.