MQTT入门2 -- “Error: Invalid password hash for user nick.”和“Connection Refused: not authorised.”

原文地址:https://www.cnblogs.com/NickQ/p/9277315.html

问题描述:

搭建好mosqitto环境后,利用无密码验证方式,成功通过测试。
但修改配置文件将匿名访问关闭,并设置密码文件

allow_anonymous false
password_file /home/xuqiang/mqtt_passwd

密码文件内容:
参考:https://mosquitto.org/man/mosquitto-conf-5.html

[xuqiang@centos6 ~]$ cat ./mqtt_passwd
nick:xuqiang

开启服务器报错:

[xuqiang@centos6 ~]$ mosquitto -c mosquitto.conf 
1530947452: mosquitto version 1.4.15 (build date 2018-04-21 17:41:08+0800) starting
1530947452: Config loaded from mosquitto.conf.
1530947452: Error: Invalid password hash for user nick.
1530947452: Error opening password file "/home/xuqiang/mqtt_passwd".

提示密码无效。

猜想可能是因为文件明文密码需要加密。
猜想依据:密码示例文件如此,示例文件内容:

[nick@XQLY ~]$ cat /etc/mosquitto/pwfile.example 
roger:$6$clQ4Ocu312S0qWgl$Cv2wUxgEN73c6C6jlBkswqR4AkHsvDLWvtEXZZ8NpsBLgP1WAo/qA+WXcmEN/mjDNgdUwcxRAveqNMs2xUVQYA==
sub_client:$6$U+qg0/32F0g2Fh+n$fBPSkq/rfNyEQ/TkEjRgwGTTVBpvNhKSyGShovH9KHewsvJ731tD5Zx26IHhR5RYCICt0L9qBW0/KK31UkCliw==
pub_client:$6$vxQ89y+7WrsnL2yn$fSPMmEZn9TSrC8s/jaPmxJ9NijWpkP2e7bMJLz78JXR1vW2x8+T3FZ23byJA6xs5Mt+LeOybAHwcUv0OCl40rA==

于是通过linux中用户密码shadow文件,找到用户密码

nick:$6$.KqyKaCc$om0v0xipwvVJOubJjihzCGF7yII4CGZireXCGCj7WskvlDSDwv3qfDk.GWe2/IUO4tqn5XYFR8CpekZz7oLaG/:17718:0:99999:7:::

密文串意思参考:
http://blog.sina.com.cn/s/blog_4d1f40c00101cvd8.html
https://blog.csdn.net/jinyuhongye/article/details/7950961
得到nick用户,明文密码为xuqiang的加密密文为:
$6$.KqyKaCc$om0v0xipwvVJOubJjihzCGF7yII4CGZireXCGCj7WskvlDSDwv3qfDk.GWe2/IUO4tqn5XYFR8CpekZz7oLaG/

将密文添加到 ./mqtt_passwd

[xuqiang@centos6 ~]$ cat ./mqtt_passwd
nick:$6$.KqyKaCc$om0v0xipwvVJOubJjihzCGF7yII4CGZireXCGCj7WskvlDSDwv3qfDk.GWe2/IUO4tqn5XYFR8CpekZz7oLaG/

运行服务器监听程序

[xuqiang@centos6 ~]$ mosquitto -c mosquitto.conf 
1530948296: mosquitto version 1.4.15 (build date 2018-04-21 17:41:08+0800) starting
1530948296: Config loaded from mosquitto.conf.
1530948296: Opening ipv4 listen socket on port 1885.
1530948296: Opening ipv6 listen socket on port 1885.

服务器程序开始监听,正常。

mosquitto 密码文件中,密文确实需要加密

** 以为问题解决了么?! NO,没有 。不信? 继续看**

此时,建立订阅者和发布者

[xuqiang@centos6 ~]$ mosquitto_sub -p 1885 -u nick -P xuqiang -t "test"
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
Connection Refused: not authorised.
^C
[xuqiang@centos6 ~]$ mosquitto_pub -p 1885 -u nick -P xuqiang -t test -m "Hello。"
Connection Refused: not authorised.
Error: The connection was refused.
[xuqiang@centos6 ~]$

不论哪个客户端,都会提示错误 Refused: not authorised. 。

显然,这是密码不正确。可能的原因有很多,最容易想的就是加密方式,linux用户密码生成和mosquitto采用的方法不同。

那么,如何得到一个正确的密文串呢。

通过查看手册,知道了mosquitto_passwd程序
可以使用mosquitto_passwd程序,自动生成
但是,问题是 command not found

[xuqiang@centos6 ~]$ mosquitto_passwd --help
-bash: mosquitto_passwd: command not found

这个问题原因我没找到,但是我重新下载编译了mosquitto-1.4.15,就有了mosquitto_passwd

[xuqiang@centos6 ~]$ mosquitto
mosquitto         mosquitto_passwd  mosquitto_pub     mosquitto_sub  
[xuqiang@centos6 ~]$ mosquitto_passwd  --help
mosquitto_passwd is a tool for managing password files for mosquitto.

Usage: mosquitto_passwd [-c | -D] passwordfile username
       mosquitto_passwd -b passwordfile username password
       mosquitto_passwd -U passwordfile
 -b : run in batch mode to allow passing passwords on the command line.
 -c : create a new password file. This will overwrite existing files.
 -D : delete the username rather than adding/updating its password.
 -U : update a plain text password file to use hashed passwords.

See http://mosquitto.org/ for more information.

这里使用 -U 将已有的明文更改为密文

[xuqiang@centos6 ~]$ vim ./mqtt_passwd
nick:xuqiang
[xuqiang@centos6 ~]$ mosquitto_passwd -U ./mqtt_passwd
[xuqiang@centos6 ~]$ cat ./mqtt_passwd
nick:$6$U3Ln7cn3+tKv0UVG$IU+jS8lPN9iH9N49u7t/eseOOKdvt8cvFjIOXrBo3LPMhf7YidcFubugPGjKOXDkjriiZdRnszb83LNLheVmlw==

当然,也可以直接向文件中写入一个新的用户名和密码

[xuqiang@centos6 ~]$ touch mqtt_passwd
[xuqiang@centos6 ~]$ mosquitto_passwd  -b mqtt_passwd  nick xuqiang

原文地址:https://www.cnblogs.com/NickQ/p/9277315.html

本帖完

** 由于目前还没有理解 linux shadow加密 和 mosquitto 的加密,两种方式有什么异同,等理解了再解释之前为什么会出现密码不正确的原因吧。 **

猜你喜欢

转载自www.cnblogs.com/NickQ/p/9277315.html