Ubuntu Server 16.04.1阿里云、腾讯云服务器上搭建mosquitto MQTT服务器

3 篇文章 0 订阅

折腾了一天,终于搞出来了(重置了云服务器系统7,8次)。之前试了两种安装方法,一种是直接源码编译安装,二种是直接命令行就安装了。我还是觉得直接命令行装比较好,源码下载编译安装,中途并不是那么顺利。

1.安装

#添加存储库列表
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

这时会弹出如下,不管他直接回车继续就好

 More info: https://launchpad.net/~mosquitto-dev/+archive/ubuntu/mosquitto-ppa
Press [ENTER] to continue or ctrl-c to cancel adding it
#更新软件
sudo apt-get update
#安装mosquitto
sudo apt-get install mosquitto
#安装mosquitto-clients
sudo apt-get install mosquitto-clients

2.配置文件

虽然通过上面的步骤已经安装好了,并且可以使用了,但是是默认是没有安全认证的,就是说不用密码是直接连接,所以我们继续配置用户名和密码。

#查看当前路径下的文件
ls

输出当前路径下什么都没有。

ubuntu@VM-0-8-ubuntu:~$ ls
ubuntu@VM-0-8-ubuntu:~$ 

切换回上层目录

cd ..
#再次进行ls查看文件
ls

输出如下,看到了ubuntu文件:

ubuntu@VM-0-8-ubuntu:~$ cd ..
ubuntu@VM-0-8-ubuntu:/home$ ls
ubuntu

我们再次回到上层目录,再查看文件

ubuntu@VM-0-8-ubuntu:/home$ cd ..
ubuntu@VM-0-8-ubuntu:/$ ls
bin  boot  data  dev  etc  home  initrd.img  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var  vmlinuz

我们的目的是找到 mosquitto.conf这个配置文件

其实它就在 etc/mosquitto 下面

我们进入这个文件夹

cd /etc/mosquitto

显示下文件,就找到了我们需要的配置文件

ubuntu@VM-0-8-ubuntu:/$ cd /etc/mosquitto
ubuntu@VM-0-8-ubuntu:/etc/mosquitto$ ls
ca_certificates  certs  conf.d  mosquitto.conf

接下来我们去编辑这个配置文件(使用vim,如果不是很熟悉可以去百度下操作)

这里特别注意,需要管理员权限运行,不然配置好文件后无法保存。

#编辑这个配置文件
sudo vim 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
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"mosquitto.conf" 13L, 348C 

i 进入编辑模式(非中文输出法下)

填加如下的内容

allow_anonymous false

password_file /etc/mosquitto/pwfile

acl_file /etc/mosquitto/aclfile

最后得到的是

解释一下添加的3行的意思

# 禁止匿名访问
allow_anonymous false
# 认证配置
password_file /etc/mosquitto/pwfile
# 权限配置
acl_file /etc/mosquitto/aclfile

 这时候按Ese退出编辑模式 再输入 :wq (保存退出)

查看是否有认证配置pwfile文件

ubuntu@VM-0-8-ubuntu:/etc/mosquitto$ ls
ca_certificates  certs  conf.d  mosquitto.conf

没有pwfile文件,我们自己创建文件

sudo touch /etc/mosquitto/pwfile

创建用户名和密码

#LiMing 是我设置的用户名
sudo mosquitto_passwd /etc/mosquitto/pwfile LiMing

接着会提示你连续两次进行输入设置密码,设置成功后,所有的客户端连接服务器都需要输入账号和密码了

运行MQTT服务器

 mosquitto -c /etc/mosquitto/mosquitto.conf -d

需要指出的是,这里默认的端口是1883端口。

2019.8.9修正:修正博文中的一个很大的错误,这样配置后,依然密码和用户名不能使用。意思就是说,密码和用户名没有配置成功,任意人知道了IP和端口都可以连接上。给一个博文链接https://cloud.tencent.com/developer/article/1350337,经过实测可以成功设置用户名和密码。

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值