sudo apt install -y mosquitto mosquitto-clients
sudo systemctl enable mosquitto.service
mosquitto -v
Mosquitto Broker Enable Remote Access (Authentication: user and password)
You can add a user/password authentication to your MQTT broker.
1) Run the following command, but replace YOUR_USERNAME with the username you want to use:
sudo mosquitto_passwd -c /etc/mosquitto/passwd YOUR_USERNAME
I’ll be using the MQTT user petermark, so I run the command as follows:
sudo mosquitto_passwd -c /etc/mosquitto/passwd petermark
sudo nano /etc/mosquitto/mosquitto.conf
# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example
per_listener_settings true
pid_file /run/mosquitto/mosquitto.pid
persistence true
persistence_location /var/lib/mosquitto/
log_dest file /var/log/mosquitto/mosquitto.log
include_dir /etc/mosquitto/conf.d
allow_anonymous false
listener 1883
password_file /etc/mosquitto/passwd
sudo systemctl restart mosquitto
sudo systemctl status mosquitto
How to test:
mosquitto_sub -d -t testTopic -u user -P pass
mosquitto_pub -d -t testTopic -m "Hello world!"