Hello. I followed strictly the tutorial in the book “Smart Home” (pages 417-419), trying to set up the MQTT broker in the Droplet of Digital Ocean. Unfortunately, the service doesn’t run properly. The commands introduced were:
sudo apt update && sudo apt upgrade
sudo apt install -y mosquitto mosquitto-clients
sudo systemctl enable mosquitto.service
mosquitto -v
1709992561: mosquitto version 2.0.18 starting
1709992561: Using default config.
1709992561: Starting in local only mode. Connections will only be possible from clients running on this machine.
1709992561: Create a configuration file which defines a listener to allow remote access.
1709992561: For more details see https://mosquitto.org/documentation/authentication-methods/
1709992561: Opening ipv4 listen socket on port 1883.
1709992561: Opening ipv6 listen socket on port 1883.
1709992561: mosquitto version 2.0.18 running
BUT WHEN CHECKING STATUS:
sudo systemctl status mosquitto
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; preset: en>
Active: inactive (dead)
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
AND CANNOT BE RESTARTED:
sudo systemctl restart mosquitto
Job for mosquitto.service failed because the control process exited with error code.
See “systemctl status mosquitto.service” and “journalctl -xeu mosquitto.service” for details.
Hi.
Did you fo
llow the steps on page 418?
Or did you skip that part?
If you run the recommended commands
systemctl status mosquitto.service
and
journalctl -xeu mosquitto.service
What do you get?
Regards,
Sara
Hi Sara, I followed everything in the pages 417-420. This is what I get:
root@carlos-iot-system:~# sudo systemctl status mosquitto.service
× mosquitto.service – Mosquitto MQTT Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Sun 2024-03-10 15:22:10 UTC; 39s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 87225 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 87226 ExecStartPre=/bin/chown mosquitto:mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 87227 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 87228 ExecStartPre=/bin/chown mosquitto:mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 87229 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited, status=3)
Main PID: 87229 (code=exited, status=3)
CPU: 16ms
Mar 10 15:22:10 carlos-iot-system systemd[1]: mosquitto.service: Main process exited, code=exited, status=3/NOTIMPLEMENT>
Mar 10 15:22:10 carlos-iot-system systemd[1]: mosquitto.service: Failed with result ‘exit-code’.
Mar 10 15:22:10 carlos-iot-system systemd[1]: Failed to start mosquitto.service – Mosquitto MQTT Broker.
Mar 10 15:22:10 carlos-iot-system systemd[1]: mosquitto.service: Scheduled restart job, restart counter is at 5.
Mar 10 15:22:10 carlos-iot-system systemd[1]: Stopped mosquitto.service – Mosquitto MQTT Broker.
Mar 10 15:22:10 carlos-iot-system systemd[1]: mosquitto.service: Start request repeated too quickly.
Mar 10 15:22:10 carlos-iot-system systemd[1]: mosquitto.service: Failed with result ‘exit-code’.
Mar 10 15:22:10 carlos-iot-system systemd[1]: Failed to start mosquitto.service – Mosquitto MQTT Broker.
AND
root@carlos-iot-system:~# sudo journalctl -xeu mosquitto.service
░░
░░ Automatic restarting of the unit mosquitto.service has been scheduled, as the result for
░░ the configured Restart= setting for the unit.
Mar 10 15:22:10 carlos-iot-system systemd[1]: Stopped mosquitto.service – Mosquitto MQTT Broker.
░░ Subject: A stop job for unit mosquitto.service has finished
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A stop job for unit mosquitto.service has finished.
░░
░░ The job identifier is 8045 and the job result is done.
Mar 10 15:22:10 carlos-iot-system systemd[1]: mosquitto.service: Start request repeated too quickly.
Mar 10 15:22:10 carlos-iot-system systemd[1]: mosquitto.service: Failed with result ‘exit-code’.
░░ Subject: Unit failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ The unit mosquitto.service has entered the ‘failed’ state with result ‘exit-code’.
Mar 10 15:22:10 carlos-iot-system systemd[1]: Failed to start mosquitto.service – Mosquitto MQTT Broker.
░░ Subject: A start job for unit mosquitto.service has failed
░░ Defined-By: systemd
░░ Support: http://www.ubuntu.com/support
░░
░░ A start job for unit mosquitto.service has finished with a failure.
░░
░░ The job identifier is 8045 and the job result is failed.
lines 495-520/520 (END)
- Can you tell me exactly what this command is returning?
mosquitto -v
According to your first post, everything is actually running smoothly:
1709992561: mosquitto version 2.0.18 running
Can you also tell me which Ubuntu version you are running in your Digital Ocean droplet?
Hi Rui, please see below:
root@carlos-iot-system:~# mosquitto -v
1710087436: mosquitto version 2.0.18 starting
1710087436: Using default config.
1710087436: Starting in local only mode. Connections will only be possible from clients running on this machine.
1710087436: Create a configuration file which defines a listener to allow remote access.
1710087436: For more details see https://mosquitto.org/documentation/authentication-methods/
1710087436: Opening ipv4 listen socket on port 1883.
1710087436: Error: Address already in use
1710087436: Opening ipv6 listen socket on port 1883.
1710087436: Error: Address already in use
root@carlos-iot-system:~#
The Ubuntu version running in the droplet is Ubuntu 23.10 x64.
It looks like your Mosquitto has been installed successfully. However, you might have made some sort of mistake while editing your config file.
Did you modify it:
sudo nano /etc/mosquitto/mosquitto.conf
To look exactly like this?
This is exactly what I have:
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.gz
per_listener_settings true
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
allow_anonymous false
listener 1883
password_file /etc/mosquitto/passwd
That’s really weird, it looks like you’ve done everything correctly. Did you run the following commands for the password with sudo?
Enable Remote Access/ Authentication
To enable remote access so that we can communicate with IoT devices, we need to edit/create a configuration file.
We’ll add authentication with user and password.
1) Run the following command, but replace YOUR_USERNAME with the username you want to use:
sudo mosquitto_passwd -c /etc/mosquitto/passwd YOUR_USERNAME
I’ll be using the MQTT user sara, so I run the command as follows:
sudo mosquitto_passwd -c /etc/mosquitto/passwd sara
When you run the preceding command with the desired username, you’ll be asked to enter a password. No characters will be displayed while you enter the password. Enter the password and memorize the user/pass combination, you’ll need it later in your projects to make a connection with the broker.
This previous command creates a password file called passwd on the /etc/mosquitto directory. Now, we need to edit the mosquitto configuration file so that it only allows authentication with the username and password we’ve defined.
Yes, I have done everything as in your book, several times, but I always get the same issue.
I have now tried installing mosquitto in a docker container in that droplet and it runs perfectly.
Thank you for bringing this issue to my attention. I started a new droplet from scratch and I was re-testing the guide. It looks like in this new version it’s now required an extra step to properly configure the passwd files permissions. I’ve already updated this guide:
I’ve added an extra step. After creating the passwd file, you need to change the permissions:
2) Set the correct permissions in the passwd file:
sudo chown mosquitto /etc/mosquitto/passwd
These are the full instructions, I recommend following them again from start and it should work.
Enable Remote Access/ Authentication
To enable remote access so that we can communicate with IoT devices, we need to edit/create a configuration file.
We’ll add authentication with user and password.
1) Run the following command, but replace YOUR_USERNAME with the username you want to use:
sudo mosquitto_passwd -c /etc/mosquitto/passwd YOUR_USERNAME
I’ll be using the MQTT user sara, so I run the command as follows:
sudo mosquitto_passwd -c /etc/mosquitto/passwd sara
When you run the preceding command with the desired username, you’ll be asked to enter a password. No characters will be displayed while you enter the password. Enter the password and memorize the user/pass combination, you’ll need it later in your projects to make a connection with the broker.
This previous command creates a password file called passwd on the /etc/mosquitto directory. Now, we need to edit the mosquitto configuration file so that it only allows authentication with the username and password we’ve defined.
2) Set the correct permissions in the passwd file:
sudo chown mosquitto /etc/mosquitto/passwd
3) Run the following command to edit the configuration file:
sudo nano /etc/mosquitto/mosquitto.conf
4) Add the following line at the top of the file (make sure it is at the top of the file, otherwise it won’t work):
per_listener_settings true
5) Also add the following three lines to allow connection for authenticated users and tell Mosquitto where the username/password file is located.
allow_anonymous false
listener 1883
password_file /etc/mosquitto/passwd
Your configuration file will look as follows (the new lines are in bold):
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
per_listener_settings true
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
allow_anonymous false
listener 1883
password_file /etc/mosquitto/passwd
6) Press CTRL-X, then Y, and finally press Enter to exit and save the changes.
7) Restart Mosquitto for the changes to take effect.
sudo systemctl restart mosquitto
8) Wait a few seconds. To check if Mosquitto is running, you can type the following command:
sudo systemctl status mosquitto
Now, you have Mosquitto MQTT broker installed on the cloud with authentication with username and password enabled.
On your ESP32/ESP8266 Arduino code, on the MQTT Host, you should use your droplet IP address.
Sorry, I have uninstalled and re-installed mosquitto, but still doesn’t work:
root@carlos-iot-system:~# sudo systemctl restart mosquitto
Job for mosquitto.service failed because the control process exited with error code.
See “systemctl status mosquitto.service” and “journalctl -xeu mosquitto.service” for details.
root@carlos-iot-system:~# sudo systemctl status mosquitto
× mosquitto.service – Mosquitto MQTT Broker
Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Sun 2024-03-10 18:33:44 UTC; 1min 8s ago
Docs: man:mosquitto.conf(5)
man:mosquitto(8)
Process: 4175 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 4176 ExecStartPre=/bin/chown mosquitto:mosquitto /var/log/mosquitto (code=exited, status=0/SUCCESS)
Process: 4177 ExecStartPre=/bin/mkdir -m 740 -p /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 4178 ExecStartPre=/bin/chown mosquitto:mosquitto /run/mosquitto (code=exited, status=0/SUCCESS)
Process: 4179 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited, status=3)
Main PID: 4179 (code=exited, status=3)
CPU: 16ms
Mar 10 18:33:44 carlos-iot-system systemd[1]: mosquitto.service: Main process exited, code=exited, status=3/NOTIMPLEMENTED
Mar 10 18:33:44 carlos-iot-system systemd[1]: mosquitto.service: Failed with result ‘exit-code’.
Mar 10 18:33:44 carlos-iot-system systemd[1]: Failed to start mosquitto.service – Mosquitto MQTT Broker.
Mar 10 18:33:44 carlos-iot-system systemd[1]: mosquitto.service: Scheduled restart job, restart counter is at 5.
Mar 10 18:33:44 carlos-iot-system systemd[1]: Stopped mosquitto.service – Mosquitto MQTT Broker.
Mar 10 18:33:44 carlos-iot-system systemd[1]: mosquitto.service: Start request repeated too quickly.
lines 1-19…skipping…
Rui, I have now created a completely new droplet and following the steps got it running.
Many thanks!