serversetup_mosquitto - dl3ebb/OpenIot GitHub Wiki

Install Mosquitto

Installing Mosquitto on Ubuntu is very simple.

On a shell (via SSH or terminal on the desktop), enter:

sudo apt install -y mosquitto mosquitto-clients

To check if the service has started, run:

sudo systemctl status mosquitto

You should see something like this:

● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-12-10 17:02:07 CET; 2min 1s ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
....

The service is now running with the default configuration, as no configuration file exists yet.

Let’s create this file:

sudo nano /etc/mosquitto/conf.d/default.conf

Add the following two lines:

listener 1883
allow_anonymous false
password_file /etc/mosquitto/passwd

This tells Mosquitto that anonymous access is not allowed. If you want to skip authentication for a smaller setup, you can use 'allow_anonymous true' and ommit the password_file.

Next, we create the password file:

sudo nano /etc/mosquitto/passwd

You can add as many users as you like, but we will start with just one:

openiot:<your_password>

Now, change the permissions of the new file and assign it to the mosquitto user and group:

sudo chmod 0700 /etc/mosquitto/passwd
sudo chown mosquitto:mosquitto /etc/mosquitto/passwd

Before Mosquitto can accept passwords, they must be encrypted:

sudo mosquitto_passwd -U /etc/mosquitto/passwd

This will output a warning, that the owner of the passwd file is not root. However if you change the owner to root, the service will fail to start, as it starts under the user mosquitto. So you may ignore this message.

You can verify that the encryption was successful:

sudo cat /etc/mosquitto/passwd

You should see an output like this:

openiot:$7$101....FamIQ==

Finally, restart the Mosquitto service:

sudo systemctl restart mosquitto.service

Verify that the service has started:

sudo systemctl status mosquitto.service

The output should look like this:

● mosquitto.service - Mosquitto MQTT Broker
     Loaded: loaded (/usr/lib/systemd/system/mosquitto.service; enabled; preset: enabled)
     Active: active (running) since Tue 2024-12-10 17:54:00 CET; 1min 8s ago
       Docs: man:mosquitto.conf(5)
             man:mosquitto(8)
...

Now let's start our MQTT client, for example, MQTT Explorer, and test the connection to our new server:

  1. Open MQTT Explorer or your preferred MQTT client.
  2. Configure the connection with the following settings:
    • Host: The IP address or hostname of your Mosquitto server (e.g., openiot or 10.103.1.10).
    • Port: Default is 1883.
    • Username: openiot (or the username you added in the password file).
    • Password: The password you set for the user.
  3. Click "Connect" to establish the connection.

If everything is configured correctly, the client should connect successfully to the MQTT server. You can now test publishing and subscribing to topics to ensure everything works as expected.

Click on "Connect"

If you have an application that uses MQTT and Web Frontend, you can simply enter your credentials into the MQTT page, press save and reboot the device.

You should then find the application's topics in the MQTT Broker.


| ← Previous Page (Prepare Server) | ↑ Server Setup Main Page | Next Page (Install Syslog) β†’ |