如何在 Ubuntu 18.04 上安装 Mosquitto MQTT 消息传递代理并确保其安全 [快速入门] (Quickstart)

介绍

MQTT是一个机器到机器的消息协议,旨在为物联网设备提供轻量级的发布/订阅通信。

在这个紧缩的快速启动教程中,我们将安装和配置Mosquitto,并使用Let's Encrypt SSL证书来确保我们的MQTT流量。

前提条件

在开始本教程之前,您将需要:

  • 一个 Ubuntu 18.04 服务器与非根, sudo 允许的用户和基本的防火墙设置,如在 本 Ubuntu 18.04 服务器设置教程
  • 一个域名指向您的服务器。本教程将使用位置持有者 mqtt.example.com
  • 端口 80 必须在您的服务器上未使用。

步骤1:安装软件

首先,我们将安装一个自定义的软件存储库,以获得最新版本的Certbot,Let’s Encrypt客户端:

1sudo add-apt-repository ppa:certbot/certbot

按「ENTER」來接受,然後安裝 Mosquitto 和 Certbot 的軟體包:

1sudo apt install certbot mosquitto mosquitto-clients

接下来我们将收集我们的SSL证书。

第2步:下载SSL证书

在您的防火墙中打开端口80:

1sudo ufw allow 80

然后运行Certbot来获取证书. 请确保在这里更换您的服务器的域名:

1sudo certbot certonly --standalone --preferred-challenges http -d mqtt.example.com

您将被要求输入电子邮件地址并同意服务条款. 这样做后,您应该看到一个消息,告诉您过程成功,您的证书存储在哪里。

我们将配置 Mosquitto 下次使用这些证书。

步骤三:设置蚊子

首先,我们将创建一个密码文件,Mosquitto将使用它来验证连接. 使用mosquitto_passwd来做到这一点,确保取代您自己喜欢的用户名:

1sudo mosquitto_passwd -c /etc/mosquitto/passwd your-username

你会被要求两次密码。

现在为 Mosquitto 打开一个新的配置文件:

1sudo nano /etc/mosquitto/conf.d/default.conf

这将打开一个空的文件. 粘贴如下:

 1[label /etc/mosquitto/conf.d/default.conf]
 2allow_anonymous false
 3password_file /etc/mosquitto/passwd
 4
 5listener 1883 localhost
 6
 7listener 8883
 8certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
 9cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
10keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem
11
12listener 8083
13protocol websockets
14certfile /etc/letsencrypt/live/mqtt.example.com/cert.pem
15cafile /etc/letsencrypt/live/mqtt.example.com/chain.pem
16keyfile /etc/letsencrypt/live/mqtt.example.com/privkey.pem

请务必将您在步骤 2 中使用的域名替换为mqtt.example.com

该文件执行如下:

  • 禁用匿名登录
  • 使用我们的密码文件来启用密码身份验证
  • 在端口 1883 上设置一个不受保护的倾听器,仅用于 localhost
  • 在端口 8883 上设置一个安全的倾听器 * 在端口 8083 上设置一个安全的基于网络的倾听器

重新启动 Mosquitto 以获取配置更改:

1sudo systemctl restart mosquitto

检查以确保服务再次运行:

1sudo systemctl status mosquitto
 1[secondary_label Output]
 2 mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
 3   Loaded: loaded (/etc/init.d/mosquitto; generated)
 4   Active: active (running) since Mon 2018-07-16 15:03:42 UTC; 2min 39s ago
 5     Docs: man:systemd-sysv-generator(8)
 6  Process: 6683 ExecStop=/etc/init.d/mosquitto stop (code=exited, status=0/SUCCESS)
 7  Process: 6699 ExecStart=/etc/init.d/mosquitto start (code=exited, status=0/SUCCESS)
 8    Tasks: 1 (limit: 1152)
 9   CGroup: /system.slice/mosquitto.service
10           └─6705 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

状态应该是正在运行的。如果没有,请检查配置文件并重新启动。

1sudo tail /var/log/mosquitto/mosquitto.log

如果一切顺利,请使用ufw来允许两个新的端口通过防火墙:

1sudo ufw allow 8883
2sudo ufw allow 8083

现在 Mosquitto 已设置,我们将配置 Certbot 重新启动 Mosquitto 更新证书后。

步骤 4 – 配置 Certbot 更新

Certbot 会在其到期前自动更新我们的 SSL 证书,但需要告知其在这样做后重新启动 Mosquitto 服务。

打开您的域名 Certbot 更新配置文件:

1sudo nano /etc/letsencrypt/renewal/mqtt.example.com.conf

在最后一行中添加以下renew_hook选项:

1[label /etc/letsencrypt/renewal/mqtt.example.com.conf]
2renew_hook = systemctl restart mosquitto

保存并关闭文件,然后运行Certbot干运行,以确保语法正常:

1sudo certbot renew --dry-run

如果你没有看到任何错误,你已经设置了,让我们测试我们的MQTT服务器。

步骤五:测试蚊子

我们在步骤 1 中安装了一些命令行 MQTT 客户端,我们可以在 localhost 倾听器上订阅主题 test :

1mosquitto_sub -h localhost -t test -u "your-user" -P "your-password"

我們可以用「mosquitto_pub」發表:

1mosquitto_pub -h localhost -t test -m "hello world" -u "your-user" -P "your-password"

若要使用端口 8883 的安全收听器订阅,请执行以下操作:

1mosquitto_sub -h mqtt.example.com -t test -p 8883 --capath /etc/ssl/certs/ -u "your-username" -P "your-password"

以下是你如何向安全听众发布:

1mosquitto_pub -h mqtt.example.com -t test -m "hello world" -p 8883 --capath /etc/ssl/certs/ -u "your-username" -P "your-password"

请注意,我们正在使用完整的主机名称而不是localhost。由于我们的SSL证书为mqtt.example.com发行,如果我们尝试安全连接到localhost,我们会收到一个错误,称主机名称不匹配证书主机名。

要测试websocket的功能,我们将使用基于浏览器的公共MQTT客户端。 在浏览器中打开Eclipse Paho javascript客户端实用程序并如下填写连接信息:

  • Host 是您 Mosquitto 服务器的域名, mqtt.example.com
  • Port8083
  • ClientId 可以留到默认的随机值
  • Path 可以留到默认值的 /ws
  • 用户名 是您的 Mosquitto 用户名从步骤 3
  • 密码 是您在步骤 3 中选择的密码

剩余的字段可以留给其默认值。

点击 连接 后,客户端将连接到您的服务器. 您可以使用 订阅发布消息 面板在 连接 面板下面发布和订阅。

结论

我们现在已经设置并测试了一台安全、密码保护和SSL加密的MQTT服务器,这可以作为您IoT、家庭自动化或其他项目的强大而安全的消息传输平台。

Published At
Categories with 技术
comments powered by Disqus