原文地址:https://www.cnblogs.com/NickQ/p/9277315.html
搭建好mosqitto环境后,利用无密码验证方式,成功通过测试。 但修改配置文件将匿名访问关闭,并设置密码文件
allow_anonymous false password_file /home/nickxq/mqtt_passwd密码文件内容: 参考:https://mosquitto.org/man/mosquitto-conf-5.html
[nickxq@centos6 ~]$ cat ./mqtt_passwd nick:nickxq开启服务器报错:
[nickxq@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/nickxq/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.htmlhttps://blog.csdn.net/jinyuhongye/article/details/7950961 得到nick用户,明文密码为nickxq的加密密文为:$6$.KqyKaCc$om0v0xipwvVJOubJjihzCGF7yII4CGZireXCGCj7WskvlDSDwv3qfDk.GWe2/IUO4tqn5XYFR8CpekZz7oLaG/
将密文添加到 ./mqtt_passwd
[nickxq@centos6 ~]$ cat ./mqtt_passwd nick:$6$.KqyKaCc$om0v0xipwvVJOubJjihzCGF7yII4CGZireXCGCj7WskvlDSDwv3qfDk.GWe2/IUO4tqn5XYFR8CpekZz7oLaG/运行服务器监听程序
[nickxq@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,没有 。不信? 继续看**
此时,建立订阅者和发布者
[nickxq@centos6 ~]$ mosquitto_sub -p 1885 -u nick -P nickxq -t "test" Connection Refused: not authorised. Connection Refused: not authorised. Connection Refused: not authorised. Connection Refused: not authorised. ^C [nickxq@centos6 ~]$ mosquitto_pub -p 1885 -u nick -P nickxq -t test -m "Hello。" Connection Refused: not authorised. Error: The connection was refused. [nickxq@centos6 ~]$不论哪个客户端,都会提示错误 Refused: not authorised. 。
显然,这是密码不正确。可能的原因有很多,最容易想的就是加密方式,linux用户密码生成和mosquitto采用的方法不同。
那么,如何得到一个正确的密文串呢。
通过查看手册,知道了mosquitto_passwd程序 可以使用mosquitto_passwd程序,自动生成 但是,问题是 command not found
[nickxq@centos6 ~]$ mosquitto_passwd --help -bash: mosquitto_passwd: command not found这个问题原因我没找到,但是我重新下载编译了mosquitto-1.4.15,就有了mosquitto_passwd
[nickxq@centos6 ~]$ mosquitto mosquitto mosquitto_passwd mosquitto_pub mosquitto_sub [nickxq@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 将已有的明文更改为密文
[nickxq@centos6 ~]$ vim ./mqtt_passwd nick:nickxq [nickxq@centos6 ~]$ mosquitto_passwd -U ./mqtt_passwd [nickxq@centos6 ~]$ cat ./mqtt_passwd nick:$6$U3Ln7cn3+tKv0UVG$IU+jS8lPN9iH9N49u7t/eseOOKdvt8cvFjIOXrBo3LPMhf7YidcFubugPGjKOXDkjriiZdRnszb83LNLheVmlw==当然,也可以直接向文件中写入一个新的用户名和密码
[nickxq@centos6 ~]$ touch mqtt_passwd [nickxq@centos6 ~]$ mosquitto_passwd -b mqtt_passwd nick nickxq原文地址:https://www.cnblogs.com/NickQ/p/9277315.html
转载于:https://www.cnblogs.com/NickQ/p/9277315.html