Install Mosquitto MQTT Broker on Arch Linux

Install Mosquitto MQTT Broker on Arch Linux

Mosquitto is a lightweight MQTT broker that uses publish/subscribe model. Since it is written in Python it can run on practically any Linux and many other systems. It also comes with command line utilities to publish and subscribe to MQTT clients.

 

Install Mosquitto

Install Packer on Arch Linuxinstall Yaourt or use another package wrapper.

 

Add a PGP signature.

gpg --recv-keys 779B22DFB3E717B7

This may take quite a while, so be patient.

 

Install Mosquitto from AUR with packer.

packer -S mosquitto

 

If the key is not added an error will occur preventing the installation.

ERROR: One or more PGP signatures could not be verified!
The build failed.

 

Install Mosquitto Manually

Download the latest archive.

wget http://mosquitto.org/files/source/mosquitto-1.4.5.tar.gz

 

Unpack the archive.

tar xzf mosquitto-1.4.5.tar.gz

 

Change the current directory to the newly created directory.

cd mosquitto-1.4.5

 

Build the package.

make

 

Install the package.

sudo make install

 

Remove the leftover installation files.

sudo rm -Rd mosquitto-1.4.5

 

Remove the package archive.

sudo rm mosquitto-1.4.5.tar.gz

 

Configure Mosquitto

Create a system account and group.

sudo useradd -r -s /bin/false mosquitto

 

Copy configuration file from the example directory.

sudo cp /etc/mosquitto/mosquitto.conf.example /etc/mosquitto/mosquitto.conf

 

Open the configuration file.

sudo nano /etc/mosquitto/mosquitto.conf

Add /var/run/mosquitto.pid to pid_file line. Optionally change the default user, port and possibly other settings by uncommenting the relevant line and entering the relevant values.

 

Create a systemd script.

sudo nano /etc/systemd/system/mosquitto.service

 

Copy the script and save.

[Unit]
Description=Mosquitto MQTT Broker daemon
ConditionPathExists=/etc/mosquitto/mosquitto.conf
Requires=network.target

[Service]
User=mosquitto
Group=mosquitto
Type=simple
ExecStartPre=/usr/bin/rm -f /var/run/mosquitto.pid
ExecStart=/usr/local/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d
ExecReload=/bin/kill -HUP $MAINPID
PIDFile=/run/mosquitto.pid
Restart=on-failure

[Install]
WantedBy=multi-user.target

 

Run Mosquitto

Start manually.

mosquitto

 

Start the systemd service.

sudo systemctl start mosquitto

 

Enable the systemd service to run on boot.

sudo systemctl enable mosquitto

 

Use Mosquitto

Subscribe to a topic with mosquitto_sub command.

mosquitto_sub -t home/frontgarden/doorbell

 

Publish a topic to a broker.

mosquitto_pub -h 192.168.0.100 -p 1883 -t home/livingroom/temperature -m test message

If the target broker is on the same system -h option can be omitted as the host defaults to localhost. Similarly the -p can be omitted if the default port is used. The topic is specified with -t option and the message is -m option.

 

A fix to the mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory error is outlined below.

 

Open the dynamic linker configuration file.

sudo nano /etc/ld.so.conf

Add /usr/local/lib line before # End of file line line and save the file.

 

Configure dynamic linker run-time bindings.

sudo /sbin/ldconfig