How-To Get Started with Mosquitto MQTT Broker on a Raspberry Pi



In this video, I’ll be covering how-to install, setup, and use Mosquitto MQTT broker on a Raspberry Pi. Mosquitto is an awesome open source MQTT broker that you can run on a device like a Raspberry Pi. It’s great because you don’t have to share you data to the cloud and can keep everything locally on your system. Plus, it’s free! MQTT is a great protocol for various DIY tech – like an Arduino, NodeMCU, or ESP8266 chip. If you have any questions, let me know! Happy to help with what I can!


— The Tech I Use —

—Favorite Parts—

Raspberry Pi 3 –
Case –
SD Card –
Power Supply –
Aeotec Z-Stick Gen 5 –
NodeMCU ESP8266 Chip –

—Shell Commands—

sudo apt-get update
sudo apt-get upgrade

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

sudo nano /etc/mosquitto/mosquitto.conf

allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883

sudo mosquitto_passwd -c /etc/mosquitto/pwfile username

mosquitto_sub -d -u username -P password -t “dev/test”
mosquitto_pub -d -u username -P password -t “dev/test” -m “Hello world”


Mosquitto Configuration –
MQTT Home Assistant –
My Website –
Home Assistant –
HA Forum –
HA Chatroom –
HA GitHub –



  1. Love your Vids, this one helped me with my MQTT Server. But that part with the router and the port forwarding is extremly dangerous. Never ever expose Ports to the Internet if you don't have to. Actually i can't even think of a scenario to use MQTT on the internet. But MQTT is open source which means finding vulnerabilitys or zero days is a lot easier.
    Also you don't remotely stress that you should use a complex password for your MQTT Server. You need it rarely so put it in a password manager and make it 30 chars long.
    Someone that sets up his MQTT like in your example is gonna have a bad time.

    I'd love a video about Smart Home Network Security and Password Security because it gets rarely mentioned in any Smart Home tutorial.
    I'm a network security engineer. Maybe i could help?

  2. trying to get this done, but starting with the call (sudo apt-get install mosquitto) doesnt work on Raspbian, but i followed instructions and got it isntalled by downloading the file through github (webrowser), later on "make" and "sudo make isntall" worked, then trying to modify the config comes empty.

  3. don't run nano (or any editor) as root unnecessarily! use sudoedit, which makes a temporary copy of the file, calls the editor (from the unprivileged account) on that copy, then copies it over the original when you exit the editor.

  4. when I type this in mosquitto_sub -d -u username -P password -t "dev/test"

    I get this
    pi@raspberrypi:~ $ mosquitto_sub -d -u username -P password -t "dev/test"

    Client mosqsub|1246-raspberryp sending CONNECT

    Client mosqsub|1246-raspberryp received CONNACK (0)

    Client mosqsub|1246-raspberryp sending SUBSCRIBE (Mid: 1, Topic: dev/test, QoS: 0)

    Client mosqsub|1246-raspberryp received SUBACK

    Subscribed (mid: 1): 0

    when I type this
    mosquitto_pub -d -u username -P password -t "dev/test" -m "Hello world"

    nothing happens

  5. Nice video! Please bear in mind, if you open the port of any MQTT broker to the internet from your home network, please secure it with authentication and authorization. You can read more about this here ( Otherwise it is open to anybody in the web.

  6. Ha, I love things like this. I can't log in with putty the connection is refused. I suspect there is a long list of things I have to do before 'just logging in". Also by the time I've figured that out, I'll probably find all this is out of date as well as the RPi.

  7. try this Web SCADA platform to create free MQTT connection to public MQTT broker:

  8. Hi Ben,
    Great instruction as always, but I think I am missing something. I have followed your other video that you published a year later called “Setting Up the SSH Add On – HassIO” works fine and enables access to HassIO as described. However this means that I cannot follow your instruction for installing updating using ‘sudo apt-get update’ because (as far as I can see) I do not have access to pi@raspberrypi: prompt.
    I have tried a new connection in Putty without SSH connection authorisation but no luck there.
    Does the SSH add-on as set up for HassIO mean I no longer have access to a ‘normal’ SSH connection?
    I can’t help but think that I must be missing something basic, but any help would be greatfully received.

  9. sorry if this is not allowed, but another video on here really messed this up, can you help please

    pi@hassbian:~ $ sudo apt-get install mosquitto

    Reading package lists… Done

    Building dependency tree

    Reading state information… Done

    mosquitto is already the newest version (1.5.5-0mosquitto1).

    0 upgraded, 0 newly installed, 0 to remove and 53 not upgraded.

    1 not fully installed or removed.

    After this operation, 0 B of additional disk space will be used.

    Do you want to continue? [Y/n] y

    Setting up mosquitto (1.5.5-0mosquitto1) …

    Failed to start mosquitto.service: Unit mosquitto.service is not loaded properly : Invalid argument.

    See system logs and 'systemctl status mosquitto.service' for details.

    invoke-rc.d: initscript mosquitto, action "start" failed.

    ● mosquitto.service – Mosquitto MQTT Broker daemon

    Loaded: error (Reason: Invalid argument)

    Active: inactive (dead)

    Jan 27 21:28:56 hassbian systemd[1]: [/etc/systemd/system/mosquitto.service…'='.

    Jan 27 21:28:56 hassbian systemd[1]: [/etc/systemd/system/mosquitto.service…'='.

    Jan 27 21:28:56 hassbian systemd[1]: [/etc/systemd/system/mosquitto.service…'='.

    Hint: Some lines were ellipsized, use -l to show in full.

    dpkg: error processing package mosquitto (–configure):

    subprocess installed post-installation script returned error exit status 1

    Errors were encountered while processing:


    E: Sub-process /usr/bin/dpkg returned an error code (1)

  10. HI: a little problem my port :1883 does not appear on the order comand:.((C: Users > netstat )) …………End CMD mosquitto the message comes ((Error: Unable to connect because the target computer refused the connection)).

  11. Newbie here….. I am struggling with understanding the basic 'architecture' of running HA on a Raspberry Pi. I have one Pi and a micro SD card with Raspbian (ip address: and a micro SD card with HA (ip address: In my mind this means that I either run the OS Raspbian or I run HA, but when i watch the various HA video's you all use Linux (e.g. Sudo) commands while you have HA up and running. When I run HA, I can not SSH into Raspbian on my PI, I can only SSH into HA. Any hints or tips on how these two environments are integrated while the PI has the HA SD card, is very much appreciated!

  12. Finally got past the connection refused and was successful in publishing the Hello World message to topic dev/test when sending from the pi console. When trying to publish from MQTT service in a browser it fails.

  13. You need to add the following to create file called /lib/systemd/system/mosquitto.service
    Add this content to it:
    Description=Mosquitto MQTT Broker daemon

    ExecStart=/bin/sh -c "/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d"
    ExecReload=/bin/kill -HUP $MAINPID


    This will allow you create a service which can be enabled to start at boot with : sudo systemctl start mosquitto,
    I also found that you need to modify the lines in
    sudo nano /etc/mosquitto/mosquitto.conf to

    allow_anonymous false
    password_file /etc/mosquitto/pwfile
    port 1883

    after you do this reboot

  14. May people and myself have been having issue with connection refused after rebooting. It seems running the command: mosquitto -d will fix the issue but that command needs to be run after each reboot. Therefore, my fix was to place a start up command in my LXDE file to run the mosquitto -d command upon restart / boot up. An easy single echo line command: echo "mosquitto -d" >> ~/.config/lxsession/LXDE-pi/autostart Now I have access after reboot without issue. Ben might want to add that to you commands in the description above … Good luck all.

  15. can you tell us how to connect owntracks app to an mqtt broker and then publish the information in the form of latitudes and longitudes in google maps in another device? Please do reply 🙂 Cheers

  16. Fantastic tutorial, works perfect first time
    For those who are having issues, persist with it, because it is not this tutorial, but your own environment.

  17. Hi Ben, nice VID, like all yours, but for me "port: 1883" in configuration.yaml makes homeassistant refuse to start … also comes up with an error – any hints ? Without that line my home assistant rans well so far …

  18. core-ssh:/# mosquitto_sub -d -u LedStrip -P LedStrip -t "dev/test"
    Error: Address not available
    :C Can every people help me?

  19. If you're not able to connect, try erasing the username and password fields in the sonoff configuration menu. The default values won't work, they need to be cleared out and saved as blank (they'll fill in with '0' afterwards I think, not sure).

  20. Hi Ben,
    Need some help configuring a tasmotized WEMOS d1 mini with a ds18b20 temp sensor to talk with ha.
    Everything is working, but not able to push temp values to dashboard ….. HELP!!!!!

  21. the following error coming
    The following packages have unmet dependencies:
    mosquitto : Depends: libssl1.0.0 (>= 1.0.1) but it is not installable
    E: Unable to correct problems, you have held broken packages.


Please enter your comment!
Please enter your name here