How do I start Mosquitto MQTT broker on startup in Ubuntu 16.04

Solution 1:

Installing sudo apt install mosquitto on 16.04 adds the /etc/init.d/mosquitto script. systemd uses that script to generate /run/systemd/generator.late/mosquitto.service, and this is what it says:

# Automatically generated by systemd-sysv-generator

[Unit]
Documentation=man:systemd-sysv-generator(8)
SourcePath=/etc/init.d/mosquitto
Description=LSB: mosquitto MQTT v3.1 message broker
Before=multi-user.target
Before=multi-user.target
Before=multi-user.target
Before=graphical.target
Before=shutdown.target
After=remote-fs.target
After=systemd-journald-dev-log.socket
Conflicts=shutdown.target

[Service]
Type=forking
Restart=no
TimeoutSec=5min
IgnoreSIGPIPE=no
KillMode=process
GuessMainPID=no
RemainAfterExit=yes
ExecStart=/etc/init.d/mosquitto start
ExecStop=/etc/init.d/mosquitto stop
ExecReload=/etc/init.d/mosquitto reload

Looking inside /etc/init.d/mosquitto tells us that

/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf is already run by default

So just put your config into /etc/mosquitto/conf.d (referenced by mosquitto.conf) and enjoy.


Being a regular systemd service, you can:

  • systemctl status mosquitto to see its current status
  • sudo systemctl (start|stop) mosquitto to change the status for the current session
  • sudo systemctl (enable|disable) mosquitto to change startup behavior (without affecting the current state)

Solution 2:

Be sure that you have mosquitto running:

ps -aux | grep mosquitto

And you have this in default.conf:

listener 1883