MQTT

How to fix mosquitto local-only mode despite ‘listener 1883’

If mosquitto is printing the local only message even though you have listener 1883 in your config file, check if mosquitto is using the correct config file. In my case, I mis-spelled the config file path (conf instead of config), hence mosquitto used the default config file, not my config file and therefore ignored all statements I put in my config file.

Posted by Uli Köhler in MQTT, Networking

How to fix Mosquitto ‘exited with code 13’

When mosquitto exits with code 13 such as a in a docker based setup, you will often see not error messsage:

Attaching to mosquitto_mosquitto_1
mosquitto_mosquitto_1 exited with code 13

However, there will be an error message in mosquitto.logSo, ensure that you have configured a log_dest file in your mosquitto.conf such as:

log_dest file /mosquitto/log/mosquitto.log

and check that file. In my case it showed these error messages:

1637860284: mosquitto version 2.0.14 starting
1637860284: Config loaded from /mosquitto/config/mosquitto.conf.
1637860284: Error: Unable to open pwfile "/mosquitto/conf/mosquitto.passwd".
1637860284: Error opening password file "/mosquitto/conf/mosquitto.passwd".

In my case, the path of the password file was mis-spelled (conf instead of config)

Note that you need to create the password file in order for mosquitto to start up!

See How to setup standalone mosquitto MQTT broker using docker-compose for example commands on how to create the user and the password file

 

Posted by Uli Köhler in Docker, MQTT

How to disable mosquitto MQTT local-only mode and listen on all IP addresses

When starting Mosquitto using a default configuration file, you will see log message like

mosquitto_1  | 1637858580: Starting in local only mode. Connections will only be possible from clients running on this machine.

indicating that the mosquitto MQTT broker is only listening on 127.0.0.1 and is not reachable over the network.

In order to fix this, you can simply bind to all IP addresses using

bind_address 0.0.0.0
listener 1883

in mosquitto.conf

Full mosquitto.conf example

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

listener 1883
## Authentication ##
allow_anonymous false
password_file /mosquitto/conf/mosquitto.conf

See our previous post on How to setup standalone mosquitto MQTT broker using docker-compose for further details on how to setup a mosquitto MQTT broker using this config.

If mosquitto is still printing the local only message even though you have listener 1883 in your config file, check if mosquitto is using the correct config file. In my case, I mis-spelled the config file path (conf instead of config), hence mosquitto used the default config file, not my config file and therefore ignored all statements I put in my config file.

Posted by Uli Köhler in MQTT, Networking

How to setup standalone mosquitto MQTT broker using docker-compose

docker-compose.yml

version: "3"

services:
  mosquitto:
    image: eclipse-mosquitto
    network_mode: host
    volumes:
      - ./conf:/mosquitto/conf
      - ./data:/mosquitto/data
      - ./log:/mosquitto/log

Now create conf/mosquitto.conf

persistence true
persistence_location /mosquitto/data/
log_dest file /mosquitto/log/mosquitto.log

listener 1883
## Authentication ##
# allow_anonymous false
password_file /mosquitto/conf/mosquitto.conf

Now create the first user using

docker-compose exec mosquitto mosquitto_passwd -c /mosquitto/conf/mosquitto.passwd mosquitto

You can optionally create more users using the -b (batch) flag instead of -c , supplying the password on the command line:

docker-compose exec mosquitto mosquitto_passwd -b /mosquitto/conf/mosquitto.passwd seconduser shoaCh3ohnokeathal6eeH2marei2o

Now start mosquitto using

docker-compose up

or create a systemd service to autostart it.

We are running mosquitto using network_mode: host. Mosquitto will, by default, listen on port 1883 (MQTT). You can configure more services using conf/mosquitto.conf, see this StackOverflow post for more info.

Posted by Uli Köhler in MQTT

How to setup Home-Assistant MQTT with username & password

The following setting in configuration.yml connects to a local mosquitto MQTT broker using username and password:

mqtt:
  broker: "127.0.0.1"
  username: "homeassistant"
  password: "iraughij3Phoh7ne9Aoxingi2eimoo"

Complete configuration.yml:

default_config:

http:
  use_x_forwarded_for: true
  trusted_proxies:
  - 127.0.0.1
  ip_ban_enabled: true
  login_attempts_threshold: 5

mqtt:
  broker: "127.0.0.1"
  username: "homeassistant"
  password: "iraughij3Phoh7ne9Aoxingi2eimoo"

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

 

Posted by Uli Köhler in Home-Assistant, MQTT

How to fix mosquitto_passwd overwriting all other users in the password file

Problem:

When running mosquitto_passwd like this:

docker-compose exec mosquitto mosquitto_passwd /mosquitto/conf/mosquitto.passwd myuser

or with the -c parameter:

docker-compose exec mosquitto mosquitto_passwd -c /mosquitto/conf/mosquitto.passwd myuser

the user is created but all other users who where previously listed in the file are deleted.

Solution:

Create the first user using the -c flag in order to create the file if it does not exist

docker-compose exec mosquitto mosquitto_passwd -c /mosquitto/conf/mosquitto.passwd firstuser

Then create additional users using -b (batch mode),which allows you to specify the password on the command line:

docker-compose exec mosquitto mosquitto_passwd -b /mosquitto/conf/mosquitto.passwd seconduser shoaCh3ohnokeathal6eeH2marei2o

When using -b, old users will not be deleted.

Posted by Uli Köhler in MQTT