Mosquitto (on PI) only works in localhost

Hello,

I am far from being a computer network specialist, but I learn a little bit at a time.
My goal is to dialogue between Arduino with MQTT via Mosquitto installed on a raspberry.
But if it works in localhost, this is not the case in the local network like for example with ‘MQTT Explorer’ on Windows or connection sketches in Arduino.
No way connect to the server. I disabled the firewall of the Asus RT-AC88u router and I do my Ethernet tests.
I installed it on another PI ‘virgin’ and I have the same problem.
It’s been a week that I turn around and lots of tutorials seem to copy each other but without examples or clear explanations.
Not to mention that I do not control English and that translators are sometimes approximate in the technical fields …

A big thank you in advance!


Hi Pollux,

I’m sorry to hear you’re having problems. I’m sure we can sort them out.

From Mosquitto version 2.0 onwards some of the way the broker works changed, which is why the tutorials you are looking at may miss some steps out I imagine.

If you run Mosquitto without a configuration file then it will allow unauthenticated access but on localhost only. The same is true if you have a configuration file but do not define a listener.

If you want to allow connections from other computers, you must define a listener and define some form of authentication. That could be password file based authentication, the dynamic security plugin, or allowing unauthenticated access with the allow_anonymous true option.

Once you’ve done one of those, the broker should work as you expect.

Some configuration examples:

# Completely unauthenticated
listener 1883
allow_anonymous true
# Dynamic security plugin
# Note that the paths may be different on your system
# Use mosquitto_ctrl or the Management Center to control the plugin
listener 1883
plugin /usr/lib/mosquitto_dynamic_security.so
plugin_opt_config_file /var/lib/mosquitto/dynamic-security.json
# Password and Access Control List file
# Use mosquitto_passwd to manage the password file
listener 1883
password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl

Let me know if that works or if you need more help.

Regards,

Roger

Hello,
Yes, another forum gave me a track and I understood the problem by visualizing the log. (“local only mode” …)

sudo tail -f /var/log/mosquitto/mosquitto.log | tee /var/tmp/mosquitto.log
1629960276: Saving in-memory database to /var/lib/mosquitto//mosquitto.db.
1629960276: mosquitto version 2.0.11 starting
1629960276: Config loaded from /etc/mosquitto/mosquitto.conf.
1629960276: Starting in local only mode. Connections will only be possible from clients running on this machine.
1629960276: Create a configuration file which defines a listener to allow remote access.
1629960276: For more details see https://mosquitto.org/documentation/authentication-methods/
1629960276: Opening ipv4 listen socket on port 1883.
1629960276: Opening ipv6 listen socket on port 1883.
1629960276: mosquitto version 2.0.11 running
1629962076: Saving in-memory database to /var/lib/mosquitto//mosquitto.db. 

Sure enough, with trial and error, I modified the configuration file, and it works by adding the lines as you have also suggested in your reply.

allow_anonymous true
listener 1883
protocol mqtt

Moreover, the site suggested in the log: “Authentication methods | Eclipse Mosquitto” is clear even to a francophone.
Good luck and thank you!

I could use help with the same problem. I am using V 2.0.1 and Pi Bullseye. My conf file is:

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

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 true
listner 1883
protocol mqtt

Additional information. No matter where I place my Mosquitto.conf file the command Mosquitto -v always states it is using a default configuration file. I did try putting the file in /etc/mosquitto/config.d.

When you run mosquitto -v then that won’t load any configuration file. You need to use mosquitto -c <path to config file>. The package installation assumes you will be using the built in service management tools for running mosquitto (sudo systemctl start mosquitto) which uses the configuration files already. So either try running manually with -c, or use systemctl to start or restart the current running instance.

Does that help?

Regards,

Roger

After editing the conf file I did a systeemctl restart and the log showed it was reading the config changes and not the default. Seems to be working even after a power down restart. Thanks for your help.

1 Like

I am having the same issue as @mboroff

Per @roger.light suggestion, when I enter mosquitto -c /etc/mosquitto/conf.d/mosquitto.conf

I get the following:

Loading config file /etc/mosquitto/conf.d/mosquitto.conf
Error: Duplicate pid _file value in configuration
Error found at /etc/mosquitto/conf.d/mosquitto.conf:8
Error found at /etc/mosquitto/conf.d/mosquitto.conf:15

Here are some pics of my conf file and directory. Am sure I am 98% there.

Is it possible you have a file in conf.d which is also specifying a pid file?

@roger.light Yes, my .conf file in the conf.d folder had all of the info below:

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 true
listener 1883

I removed everything except

allow_anonymous true
listener 1883

and when I start mosquitto using -c /etc/mosquitto/conf.d/mosquitto.conf, it works and all clients on the LAN can reach it. Success!

How I can ensure that upon starting the Raspberry Pi that the correct mosquitto.conf file is used?