sudo apt update
sudo apt install mosquitto mosquitto-clients
sudo mosquitto_passwd -c /etc/mosquitto/passwd myuser
sudo nano /etc/mosquitto/mosquitto.conf
# 允許匿名用戶連接(默認為 true,我們先關閉它)
allow_anonymous false# 指定密碼文件路徑
password_file /etc/mosquitto/passwd# 設置監聽器(可選,確保它在監聽本地和/或網絡)
listener 1883
sudo chmod 644 /etc/mosquitto/passwd
查看日志
/var/log/mosquitto/mosquitto.log
# 第二個用戶
sudo mosquitto_passwd /etc/mosquitto/passwd user2
測試
mosquitto_sub -h localhost -t "test/topic" -u "myuser" -P "your_password"
mosquitto_pub -h localhost -t "test/topic" -m "Hello Secure MQTT!" -u "myuser" -P "your_password"