阿里云ubuntu 部署mosquitto服务器

发布时间 2023-11-30 15:46:53作者: Amumu1

阿里云ubuntu 部署mosquitto服务器

安装mosquitto

sudo apt install mosquitto

添加用户名密码

touch /etc/mosquitto/pwdfile
sudo chmod 0700 /etc/mosquitto/pwdfile 
sudo chown mosquitto /etc/mosquitto/pwdfile
mosquitto_passwd -b /etc/mosquitto/pwdfile username passwd

修改配置文件

xxx@ubuntu2204:~$ cat /etc/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

persistence true
persistence_location /var/lib/mosquitto/

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

include_dir /etc/mosquitto/conf.d

allow_anonymous false # 禁止匿名登录
password_file /etc/mosquitto/pwdfile #指定用户密码文件
listener 1883 0.0.0.0 # 监听所有1883端口上的消息,默认只能监听本地,不修改远程无法连接

重启服务

systemctl restart mosquitto

查看服务状态

systemctl status mosquitto

测试

#测试机上安装mosquitto-clients
sudo apt  install mosquitto-clients
#订阅
mosquitto_sub -p 1883 -h x.x.x.x-u usrname -P passwd -t test
#发布
mosquitto_pub -p 1883 -h x.x.x.x-u usrname -P passwd -t test -m 123