Search code examples
linuxubuntumqttmosquitto

Mosquitto only opens in local mode Ubuntu


I'm attempting to use an esp32 to publish to a mosquitto MQTT server on my Ubuntu laptop. I continuously run into the error (not really an error, more of a security feature):

1645391623: mosquitto version 2.0.14 starting
1645391623: Using default config.
1645391623: Starting in local only mode. Connections will only be possible from clients running on this machine.
1645391623: Create a configuration file which defines a listener to allow remote access.
1645391623: For more details see https://mosquitto.org/documentation/authentication-methods/
1645391623: Opening ipv4 listen socket on port 1883.
1645391623: Opening ipv6 listen socket on port 1883.
1645391623: mosquitto version 2.0.14 running
1645391674: mosquitto version 2.0.14 terminating

I have been doing a lot of research on this both here on stack exchange and on the general internet. It seems like I need to add something like this to the mosquitto.conf file:

listener 1883
allow_anonymous true

or

listener 1883 0.0.0.0
allow_anonymous true

I want the broker to be accessible from external devices on the local network. Here is what the original mosquitto.conf file looks like:

# 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

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

here is what my modified file looks like:

# 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

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

listener 1883
allow_anonymous true

I get the same message regardless of which file I use.

I am on Ubuntu 20.04.3 LTS.

I would be happy to provide any additional information you might need.

Any help or advice you could give would be greatly appreciated!

EDIT: After using the following command: mosquitto -c /etc/mosquitto/mosquitto.conf

I receive the following output:

mosquitto -c /etc/mosquitto/mosquitto.conf 
1645395680: Loading config file /etc/mosquitto/conf.d/mosquito.conf 
1645395680: Error: Unable to open log file /var/log/mosquitto/mosquitto.log for writing. 
1645395680: Error: Address already in use

or if I use the command with sudo:

1645395654: Loading config file /etc/mosquitto/conf.d/mosquito.conf 

This is after I used the command: sudo systemctl kill mosquitto.service In an attempt to remove the port error.

SOLVED: I ended up using only allow_anonymous true at the end of my file. It appears that I was getting the error by putting in the listener 1883 at the end of the .conf file. I also have begun restarting the service instead of attempting to run it in the console. Thank you for your help!


Solution

  • When installed mosquitto sets up a systemd service that runs as the mosquitto user. I'd you try to run it as a normal user you will hit several problems

    1. If the service is running out will fail because the address/port is already in use by the service
    2. If you stop the service first then it will fail to write the log file because it is owned by the mosquitto user.

    So the best way to test is to update the config file then restart the service (sudo service mosquitto restart) and trail the logs with journalctl -f -u mosquitto.service