0

I am trying to connect to a mosquitto broker, across linux clients. I can get everything working from the local machine, but when trying to connect from another machine I get the error ConnectionRefusedError: [Error 111] Connection refused.

Here is the process: On the local machine, I install mostquitto, stop the service and start a live instance:

#Terminal 1
sudo service mosquitto stop
mosquitto

I then try and pub and sub, from distinct terminals on that machine:

#Terminal 2
mosquitto_sub -t 'test'
#Terminal 1 shows new connection

#Terminal 3
mosquitto_pub -t 'test' -m 'Hello, world!'
#Terminal 1 shows new connection, and then disconnect.
#Terminal 2 shows 'Hello, world!'

I now try to connect from a remote machine. First I edit the mosquitto config file to allow unauthorized connections:

sudo nano /etc/mosquitto/mosquitto.conf
#Add the following:
listener 1883
allow_anonymous true
protocol mqtt

I note that the mosquitto logs previously showed only local connections allowed, after editing the config file and restarting, the logs no longer show that message.

Then I install paho-mqtt on another machine. I run the following python script:

import paho.mqtt.client as mqtt

client = mqtt.Client('131')
client.connect('192.168.0.146') #The IP of machine 1, running the broker where the code above ran correctly across the terminals

I get the error mentioned above:ConnectionRefusedError: [Error 111] Connection refused. The mosquitto instance on machine 1 shows nothing. Logs show nothing.

I can't work out what is going on. I have read every question on SO that I can find. Nothing goes beyond changing the config file. I have tried running the broker on two machines (laptop and pi). I have tried connecting from multiple different sources: esp32 board, different laptop and pi. Nothing works. I can only assume there is some network-wide problem, but my network isn't isolating devices as I ssh into my pi all the time and have wifi lights and switches running on the LAN.

If anyone can help me troubleshoot I would be very grateful.

MorrisseyJ
  • 1,191
  • 12
  • 19
  • You always have to pass a configuration file to mosquitto, it will not pick one up by default (the service explicitly passes the path to `/etc/mosquitto/mosquitto.conf`) make sure you are using the `-c` command line argument. Also make sure the firewall has port 1883 open – hardillb Nov 25 '22 at 23:08
  • @hardillb Yes, this was the issue. If you want to post as an answer i'll happily upvote. – MorrisseyJ Nov 27 '22 at 23:04

1 Answers1

1

Mosquitto will not pick up a default configuration file, you must always pass the configuration file with the -c command line argument or it will fall back to the baked in config (that will only listen on localhost)

The service includes -c /etc/mosquitto/mosquitto.conf to force it to use the config file.

hardillb
  • 54,545
  • 11
  • 67
  • 105