1. 程式人生 > >MQTT入門2 -- “Error: Invalid password hash for user nick.”和“Connection Refused: not authorised.”

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

password org squit www. pla reat pre 沒有 class

原文地址: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 的加密,兩種方式有什麽異同,等理解了再解釋之前為什麽會出現密碼不正確的原因吧。 **

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