Quick Guide to The Mosquitto.conf File With Examples

mosquitto-conf-iconYou can configure the mosquitto broker using a configuration file.

The default configuration file is called mosquitto.conf and it is used by the mosquitto broker when started as a Linux daemon or Windows service.

You will find the mosquitto.conf file in the /etc/mosquito directory on Linux, and in the c:\mosquitto\ directory on Windows.

Note: the Windows install lets you choose the directory.

Important: In order for changes in the mosquitto.conf file to become effective you must restart the mosquitto broker.

Broker Mosquitto.conf Settings

The default mosquitto.conf file has all of the settings commented out and the broker doesn’t require a configuration file to run.

If you look at the settings you will find that they are divided into sections and some of the settings affect the entire broker instance, whereas others affect a particular listener.

Important- An extra  listener is effectively an additional port, and not an additional broker!

The diagram below is my understanding of how the settings apply.

Mosquitto-conf-Structure

Configurations Examples

Mosquitto Broker Listening on Several Ports

The mosquitto broker can be configured to listen on several ports at the same time.

However this configuration doesn’t mean that you have virtual brokers. as most of the configuration is shared.

You cannot, for example, configure a broker to listen on port 1884, and to allow anonymous access, and also to listen on port 1885 and not allow anonymous access.

Because they are part of the same broker instance they share the allow_anonymous setting.

However you can configure a broker to listen on a port and require SSL and also to listen on another port and not use SSL.

Example 1- Listen on ports 1883 and 1884

Section Default listener

port 1883

Section Extra listeners

listener 1884


Example 2- Listen on ports 1883 and 8883 (SSL)

Section Default listener

port 1883

Section Extra listeners

listener 8883

Certificate based SSL/TLS support

(Windows)
cafile c:\mosquitto\certs\ca.crt
keyfile c:\mosquitto\certs\server.key
certfile c:\mosquitto\certs\server.crt

(Linux)

cafile /etc/mosquitto/certs/ca.crt
keyfile /etc/mosquitto/certs/server.key
certfile /etc/mosquitto/certs/server.crt


Example 3- Listen on ports 1883 and WebSockets (SSL)

Section Default listener

port 1883

Section Extra listeners

listener 9001
protocol websockets

See Also MQTT and Mosquitto WebSockets Working Notes


Starting Mosquitto – Notes

When starting mosquitto for the command line unless you specify a configuration file none is used. Therefore

mosquitto
mosquitto -v

and other similar commands start mosquitto without using a configuration file.

If you install mosquitto as a service on Windows then it starts using the mosquitto.conf.

The Linux install also configures the Mosquitto broker to start automatically using the mosquitto.conf file..

Important: When testing mosquitto you need to stop the mosquitto instance that has been started when the machine booted, and then start your own instance from the command line.

Editing The File- Tips

You can use a normal text editor to edit the file. Although it is tempting to edit the particular section of the file it can be very difficult finding what settings you have changed.

Therefore put all of your settings at the top of the file, and use the commented out section as documentation.

If you do edit the individual sections then I have created a simple Python script that will display only the un-commented settings from the file.

Restarting Mosquitto or Reloading Config Files

If you update mosquitto configuration files you can restart mosquitto to pick up the changes.

However on Linux you can reload the configuration files without restarting the broker by sending the HUP signal as follows:

kill-HUP PID # where PID is the process ID as shown below:

send-hup-mosquitto

If you look at the console it should show that the config files have been reloaded

reload-conf-mosquitto

Running Multiple Mosquitto Brokers

You can configure a broker to listen on several ports, but to create multiple brokers with their own configurations then you will need to start multiple instances of mosquitto.

Examples:

Start mosquitto and listen on port 1883

mosquitto -p 1883

Start mosquitto as a daemon and listen on port 1884

mosquitto -p 1884 -d

Start mosquitto as a daemon and use the mosquitti-2.conf file. –  Windows

mosquitto -c c:\mosquitto\mosquitto-2.conf  -d

or Linux

mosquitto -c /etc/mosquitto/mosquitto-2.conf  -d

Here is a video that shows you how

Useful Linux Commands

To stop Mosquitto when running as a daemon:

ps -aux | grep mosquitto
pgrep mosquitto
kill -9 PID (that you get from above command)

Resources:

Mosquitto.conf Manual Page

Related Tutorials

Save

Save

Please rate? And use Comments to let me know more
[Total: 4    Average: 4/5]

3 comments

Leave a Reply

Your email address will not be published. Required fields are marked *