|
| 1 | +Setting Up an MQTT broker (Mosquitto) |
| 2 | +===================================== |
| 3 | + |
| 4 | +`Mosquitto <https://mosquitto.org/>`_ is a popular choice and is available for most platforms. |
| 5 | +`Download/install instructions <https://mosquitto.org/download/>`_ are pretty straight forward. |
| 6 | + |
| 7 | +.. admonition:: Installing Mosquitto on Linux |
| 8 | + :class: todo |
| 9 | + |
| 10 | + .. code-block:: shell |
| 11 | +
|
| 12 | + sudo apt-get install mosquitto |
| 13 | +
|
| 14 | +Configuring Mosquitto |
| 15 | +--------------------- |
| 16 | + |
| 17 | +You may have to change the default configuration used by the Mosquitto broker to allow other |
| 18 | +network devices to connect. For me (using v2.0.11 on Ubuntu), this meant editing the file |
| 19 | +located at **/etc/mosquitto/mosquitto.conf**. |
| 20 | + |
| 21 | +Open the file with root permission from the terminal (or via SSH client): |
| 22 | + |
| 23 | +.. code-block:: shell |
| 24 | +
|
| 25 | + sudo nano /etc/mosquitto/mosquitto.conf |
| 26 | +
|
| 27 | +Make sure a ``listener`` is bound to a port (typically ``1883`` by default) with the domain |
| 28 | +``0.0.0.0`` (the server machine's ``localhost``). If you want to skip |
| 29 | +mqtt_user_password_, then add a line that sets ``allow_anonymous`` to ``true``. |
| 30 | +It should end up looking like this: |
| 31 | + |
| 32 | +.. code-block:: pacmanconf |
| 33 | + :caption: /etc/mosquitto/mosquitto.conf |
| 34 | +
|
| 35 | + listener 1883 0.0.0.0 |
| 36 | +
|
| 37 | + # to bypass username and password configuration |
| 38 | + allow_anonymous true |
| 39 | +
|
| 40 | +Save and close the file, then :std:option:`systemctl restart` the broker. |
| 41 | + |
| 42 | +Checking the Mosquitto broker logs |
| 43 | +********************************** |
| 44 | + |
| 45 | +By default, the logs for mosquitto are saved to **/var/log/mosquitto/mosquitto.log**. This can be |
| 46 | +changed with the ``log_dest file`` value in the configuration: |
| 47 | + |
| 48 | +.. code-block:: text |
| 49 | + :caption: Default Log Destination in **/etc/mosquitto/mosquitto.conf** |
| 50 | +
|
| 51 | + log_dest file /var/log/mosquitto/mosquitto.log |
| 52 | +
|
| 53 | +If the broker fails to understand a given configuration, then these logs will point to what |
| 54 | +configuration option was erroneous. |
| 55 | + |
| 56 | +.. code-block:: shell |
| 57 | + :caption: Print Logs in the terminal (requires root permission) |
| 58 | +
|
| 59 | + sudo cat /var/log/mosquitto/mosquitto.log |
| 60 | +
|
| 61 | +.. _mqtt_user_password: |
| 62 | + |
| 63 | +Setting a username and password |
| 64 | +------------------------------- |
| 65 | + |
| 66 | +It is recommended that your MQTT broker's access be secured via a username and password. |
| 67 | +The Mosquitto broker uses a password file to store these values securely. |
| 68 | + |
| 69 | +1. .. code-block:: shell |
| 70 | + :caption: Create the password file for a user |
| 71 | + |
| 72 | + mosquitto_passwd -c pswd.txt username |
| 73 | + |
| 74 | + The above command creates a password file named ``pswd.txt`` for a user named ``username``. |
| 75 | + |
| 76 | + .. details:: Adding another user |
| 77 | + :class: info |
| 78 | + |
| 79 | + Use the ``-b`` switch to add more users: |
| 80 | + |
| 81 | + .. code-block:: shell |
| 82 | +
|
| 83 | + mosquitto_passwd -b pswd.txt other_username user_password |
| 84 | + .. details:: Removing a user |
| 85 | + :class: error |
| 86 | + |
| 87 | + Use the ``-D`` switch to remove a user: |
| 88 | + |
| 89 | + .. code-block:: shell |
| 90 | +
|
| 91 | + mosquitto_passwd -D pswd.txt other_username user_password |
| 92 | + .. note:: |
| 93 | + If you inspect the password file after creation, you will notice that the password |
| 94 | + associated with usernames is not what you entered. This is because ``mosquitto_passwd`` |
| 95 | + encrypts the password using a SHA512 scheme. |
| 96 | +2. .. code-block:: shell |
| 97 | + :caption: Move the password file to the broker's configuration path |
| 98 | + |
| 99 | + sudo mv pswd.txt /etc/mosquitto/ |
| 100 | + |
| 101 | + The **pswd.txt** file you created should now be next to you broker's configuration file |
| 102 | + (**/etc/mosquitto/mosquitto.conf**). |
| 103 | +3. Add the following lines to the broker's configuration file. |
| 104 | + |
| 105 | + .. details:: Open your broker's configuration file |
| 106 | + :class: faq |
| 107 | + |
| 108 | + .. code-block:: shell |
| 109 | +
|
| 110 | + sudo nano /etc/mosquitto/mosquitto.conf |
| 111 | +
|
| 112 | + .. code-block:: text |
| 113 | + :caption: add the password file's path to the configuration |
| 114 | +
|
| 115 | + per_listener_settings true |
| 116 | +
|
| 117 | + listener 1883 0.0.0.0 |
| 118 | + allow_anonymous false |
| 119 | + password_file /etc/mosquitto/pswd.txt |
| 120 | +
|
| 121 | + - ``per_listener_settings`` is required to assign the password file to a listener. |
| 122 | + - ``alow_anonymous`` should be disabled if you want to prohibit non-authenticated access to |
| 123 | + your broker. |
| 124 | + - ``password_file`` is the path to the password file created with encrypted passwords. |
| 125 | + |
| 126 | +4. :std:option:`systemctl restart` (or :std:option:`systemctl start`) to force the broker to use |
| 127 | + the updated configuration. |
| 128 | + |
| 129 | +.. admonition:: Enabling SSL |
| 130 | + :class: check |
| 131 | + |
| 132 | + If desired, you can enable SSL support in your broker for additional security and |
| 133 | + anti-corruption of data. Since this is all rather technical and a bit more involved, I would |
| 134 | + recommend following `Steve's Internet Guide <http://www.steves-internet-guide.com/mosquitto-tls/>`_. |
| 135 | + |
| 136 | +MQTT Explorer |
| 137 | +------------- |
| 138 | + |
| 139 | +To verify that this library is publishing and subscribing topics with your MQTT broker, I |
| 140 | +recommend using the `MQTT Explorer app <https://mqtt-explorer.com/>`_ (which works well |
| 141 | +on my Windows PC). |
| 142 | +`Downloads are available <https://github.com/thomasnordquist/MQTT-Explorer/releases/latest>`_ |
| 143 | +for most platforms. There's even a stable release deployed in the Windows App Store and the |
| 144 | +Snap Store for Linux. |
0 commit comments