Skip to content

mosquitto

按照指令

shell
sudo apt-get install mosquitto mosquitto-clients

启动指令

shell
sudo systemctl start mosquitto

测试

发布消息

要发布消息,可以使用 Mosquitto 软件包自带的 mosquitto_pub 命令行工具。基本语法如下:

shell
mosquitto_pub -h <hostname> -t <topic> -m <message>

这个命令中的选项含义如下:

  • -h 选项指定 MQTT Broker 的主机名。
  • -t 选项指定要发布消息的主题。
  • -m 选项指定要发布的信息。

订阅主题

订阅主题也很简单。可以使用 mosquitto_sub 命令行工具,其基本语法是:

shell
mosquitto_sub -h <主机> -t <>

这些选项的含义如下:

  • -h 选项指定 MQTT Broker 的主机名。
  • -t 选项指定要订阅的主题。

配置

常用命令

shell
ubuntu@ubuntu:~$ mosquitto -h
mosquitto version 2.0.11

mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker.

Usage: mosquitto [-c config_file] [-d] [-h] [-p port]

 -c : specify the broker config file.
 -d : put the broker into the background after starting.
 -h : display this help.
 -p : start the broker listening on the specified port.
      Not recommended in conjunction with the -c option.
 -v : verbose mode - enable all logging types. This overrides
      any logging options given in the config file.

See https://mosquitto.org/ for more information.
  • -c 或 --config-file:使用此选项加载配置文件。如果不指定,mosquitto broker 将默认监听 1883 端口,并且只绑定到本地回环接口(loopback interface),同时采用 mosquitto.conf(5) 中描述的默认设置。注意在 2.0 版本及以上,如果同时使用了 -p 选项和配置文件中定义的监听器,命令行中的 -p 选项将会被忽略。

  • -d 或 --daemon:以守护进程模式运行 mosquitto。这意味着 mosquitto 将在后台作为服务运行,不影响终端操作。除了运行方式变为后台外,其他行为保持不变。

  • -p 或 --port:指定 broker 监听的端口号。该选项可以最多指定 10 次,以便于打开多个监听不同端口的套接字。对于 1.6.x 及更早版本,使用 -p(或默认的 1883 端口)会绑定到所有网络接口,允许任何网络的连接。但是从 2.0 版本开始,通过 -p 选项指定的监听器仅绑定到本地回环接口,因此只能从本地机器连接。若同时使用 -p 和配置文件中定义的监听器,命令行中的 -p 选项将被忽略。

  • -v 或 --verbose:使用详细日志记录。这相当于在配置文件中设置了 log_type 为 all,启用所有类型的日志输出。启用此选项后,它将覆盖配置文件中设定的所有日志级别选项,确保 mosquitto 以最详细的日志级别运行,有助于调试和监控 broker 状态。

配置文件

ubuntu 22.04 安装之后,配置文件默认在 /etc/mosquitto/mosquitto.conf 里面

shell
ubuntu@ubuntu:~$ cat /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

listener 1883 0.0.0.0
allow_anonymous 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

无密码配置

为了在 MQTT Broker Mosquitto 中实现无认证(匿名)访问,我们可以使用 allow_anonymous 选项进行设置。下面是如何配置匿名访问的具体步骤:

shell
listener 1883
allow_anonymous true

在这段配置中,我们为监听在 1883 端口的 Broker 启用了匿名访问权限。这意味着任何连接到此端口的客户端无需提供用户名和密码即可进行通信。

值得注意的是,在同一台 MQTT Broker 上同时允许匿名访问和经过身份验证的访问是完全可行的。特别是对于动态安全插件而言,它支持对匿名用户和已认证用户提供不同的权限设定,这在某些场景下非常实用。

密码文件配置

  • 创建密码文件

    利用 mosquitto_passwd 工具可以创建并填充密码文件。若要新建一个密码文件并添加用户,请执行以下命令,系统会提示您输入密码。注意,这里的-c 选项表示如果文件已存在则会被覆盖:

    mosquitto_passwd -c <密码文件路径> <用户名>

    若要向已存在的密码文件中添加更多用户或修改现有用户的密码,只需省略 -c 参数:

    mosquitto_passwd <密码文件路径> <用户名>

  • 从密码文件中移除用户

    如需从密码文件中删除某个用户,请使用以下命令:

    mosquitto_passwd -D <密码文件路径> <用户名>

    另外,您也可以在单行命令中添加/更新用户名及其密码,但请注意,这种方式会导致密码明文出现在命令行及命令历史记录中:

    mosquitto_passwd <密码文件路径> <用户名> <密码>
  • 配置 Broker

    开始使用密码文件之前,您需要在 Broker 的配置文件中添加 password_file 选项,并指向您的密码文件位置:

    password_file <密码文件路径>

    确保运行 Mosquitto 服务的用户有权限读取该密码文件。在 Linux/POSIX 系统上,通常这个用户是 mosquitto,而 /etc/mosquitto/password_file 是存放此文件的一个常见路径。

  • 针对不同监听器设置独立的安全设置

    如果您启用了 per_listener_settings true 选项,以便为每个监听器设置不同的安全参数,则必须在相关监听器配置之后指定相应的密码文件:

    listener 1883
    password_file /etc/mosquitto/password_file

    这样,连接到 1883 端口的客户端就需要通过密码文件中的凭证进行身份验证。

验证配置简单举例

在配置文件中同时配置无密码和密码文件配置,配置如下:

shell
# 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

listener 1883 0.0.0.0
user root
password_file /etc/mosquitto/pwfile

allow_anonymous true

其中 /etc/mosquitto/pwfile 是使用 mosquitto_passwd 命令生成的密码文件:

shell
ubuntu@ubuntu:/etc/mosquitto$ sudo mosquitto_passwd -c pwfile admin
[sudo] password for ubuntu:
Password:
Reenter password:

输入此命令后会提示输入 admin 用户的密码,然后就会在目录下生成密码文件。

shell
ubuntu@ubuntu:/etc/mosquitto$ cat pwfile
admin:$7$101$xP6ybd8iMDrXtg5Q$KPVSMyeyzBFyfTSJbopdRrbY/HpHN2Jc4u7FU3NQOaAdFeZ4aTfeZPNj6Brp7lzkaHyppaO8sXZMT7TTyOYyXg==

之后重启服务:

shell
sudo systemctl restart mosquitto

就可以使用匿名登录与正常登录了。

权限配置

权限配置文件

shell
# 打开配置
vim /ect/mosquitto/aclfile

# 李雷只能发布以 test 为前缀的主题,订阅以 $SYS 开头的主题即系统主题
user lilei
topic write test/#
topic read $SYS/#

# 韩梅梅只能订阅以 test 为前缀的主题
user hanmeimei
topic read test/#

然后在配置文件中配置权限配置文件的路径:

shell
acl_file file_path

这个配置可以连接成功,但是无法成功的订阅以及发布。

附录

https://www.emqx.com/zh/blog/mosquitto-mqtt-broker-pros-cons-tutorial-and-modern-alternatives

https://blog.csdn.net/weixin_45459266/article/details/136903981