在 OpenStack 服务器中发布到 Mosquitto

publish to Mosquitto in an OpenStack server

请帮助我了解如何发布到位于服务器中的 Mosquitto,该服务器需要密钥文件才能通过 ssh 连接到它。

我正在尝试从不同网络中的计算机向 Ubuntu 16.04 服务器上的 Mosquitto 发布消息。

我已将问题缩小到服务器的连接配置设置。

要通过 ssh 访问它,我需要传递密钥文件,例如: ssh -i mykeyfile.pem user@ipaddress

问题是我无法从不同网络中的计算机访问 Mosquitto,因为我不知道如何指定密钥文件。我检查了端口,它们是打开的。

这就是我尝试发布的方式:

mosquitto_pub -h ip_address -p 1883 -t test -m "hello world"

使用相同的配置,我在另一台不需要密钥文件的服务器上进行了尝试。我设法发布了一条消息。这就是我得出的结论,问题出在密钥文件上。

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

pid_file /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

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

include_dir /etc/mosquitto/conf.d

我在尝试发布时遇到的错误是:

Error: Connection timed out

编辑

防火墙被禁用,所以 sudo ufw status 的结果是 disactivated。我启用它并设置了一些规则。当前状态是:

Status: active

To                         Action      From
--                         ------      ----
8083                       ALLOW       Anywhere
22                         ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
1883                       ALLOW       Anywhere
8083 (v6)                  ALLOW       Anywhere (v6)
22 (v6)                    ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)
1883 (v6)                  ALLOW       Anywhere (v6)

下一个输出来自 iptables sudo iptables -L -v -n:

Chain ufw-user-input (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:8083
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:8083
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:22
  699 40412 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:80
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:80
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:443
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:443
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:1883
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:1883
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:1883
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0            udp dpt:1883

但是还是不能发布消息。

Mosquitto 所在的计算机运行 Apache2 服务器

我希望这是有道理的。 谢谢。

这是我解决问题的方法。

上下文:

  • Mosquitto 在服务器中,
  • 服务器位于 VM
  • VM 在 OpenStack.

在对 iptables、ufw、firewall 和 mosquitto 的配置文件进行大量(可能是不必要的)更改后,我检查了 OpenStack(我知道……为什么我之前没有提到这个!好吧,我很困惑,这不是我的第一个想法)。然后我意识到我的 VM 的安全组没有 端口 1883 的规则(不允许或拒绝)。我添加了它。

尤里卡,我可以从外部计算机发布消息。

这是我的安全组的样子(添加了最后两行):

我的结论是,不仅只有一扇门,而且像洋葱一样有很多层要穿过!

希望这对其他人有所帮助。