如何为 Mosquitto Broker 设置身份验证?

Tho*_*chi 2 authentication mqtt mosquitto raspberry-pi3

在 Mosquitto 上配置身份验证时遇到问题。

只要我允许匿名连接mosquitto.conf,我就可以毫无问题地访问代理(发布和订阅)。但是一旦我启用身份验证,我就会得到一个“ connection refused”。我遵循了多个关于如何设置身份验证的教程,但我无法让它工作,也无法弄清楚原因。

我已经在 Raspberry PI 3 B 型上安装了 Mosquitto 1.4.10。

我使用命令创建了一个密码文件:sudo mosquitto_passwd -c /etc/mosquitto/passwd test并将密码设置为“test”。

我已设置allow_anonymous为 false 并将 password_file设置为/etc/mosquitto/passwdin mosquitto.conf

然后,当我尝试命令时sudo mosquitto_sub -t hello/world -u test -P test,我收到“错误:连接被拒绝”。

我检查了密码文件,它似乎是正确的(它具有正确的名称和路径/etc/mosquitto/passwd,并且包含我的用户“test”的条目)。

题:

我在配置中犯了错误吗?我错过了什么吗?

har*_*llb 7

使用默认的 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

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

password_file /etc/mosquitto/passwd
allow_anonymous false

include_dir /etc/mosquitto/conf.d
Run Code Online (Sandbox Code Playgroud)

然后使用mosquitto_passwd创建密码文件

mosquitto_passwd -c /etc/mosquitto/passwd test
Run Code Online (Sandbox Code Playgroud)

然后重新启动mosquitto service mosquitto restart

(添加密码后重启服务很重要,因为文件是在启动时读取的,或者当 mosquitto 收到 HUP 信号时)

您现在应该可以使用以下mosquitto_sub命令进行连接

mosquitto_sub -t hello/world -u test -P test
Run Code Online (Sandbox Code Playgroud)

你可以拖尾 /var/log/mosquitto/mosquitto.log 文件,看看是否有任何错误