Ubuntu mosquitto 安装及配置

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto -y
sudo apt-get install mosquitto-clients -y

通过上诉命令完成mosquitto的安装,版本mosquitto version 2.0.10。

修改配置文件,用以启用mosquitto的各项功能。

# 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

打开/etc/mosquitto/mosquitto.conf,发现需要将配置文件放置于/etc/mosquitto/conf.d/目录下,示例文件可以从/usr/share/doc/mosquitto/examples/目录下提取,发现其是一个压缩包,将其解压缩,然后复制到/etc/mosquitto/conf.d/目录下。

xx@ubuntu:/etc/mosquitto$ cd  /usr/share/doc/mosquitto/examples/
xx@ubuntu:/usr/share/doc/mosquitto/examples$ ls -lh
总用量 24K
-rw-r--r-- 1 root root 230 Apr  3  2021 aclfile.example
-rw-r--r-- 1 root root 12K Apr  3  2021 mosquitto.conf.gz
-rw-r--r-- 1 root root  23 Apr  3  2021 pskfile.example
-rw-r--r-- 1 root root 355 Apr  3  2021 pwfile.example
cd  /usr/share/doc/mosquitto/examples/
sudo gzip -d mosquitto.conf.gz 
sudo cp mosquitto.conf /etc/mosquitto/conf.d/

手动启动mosquitto,方便查看日志排查出现的问题。

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

1.配置成无用户密码校验和无TLS连接

listener 1883
allow_anonymous true 

配置文件如上配置,然后启动mosquito。

验证:

订阅
mosquitto_sub -t mytest  -h localhost -p 1883

发布
mosquitto_pub  -t mytest -m mymessage -h localhost -p 1883

2.配置成用户密码校验和无TLS连接

listener 1883
allow_anonymous false
password_file /etc/mosquitto/pwfile

修改配置文件如上,然后添加用户

xx@ubuntu:~$ sudo mosquitto_passwd -c /etc/mosquitto/pwfile test
Password: 
Reenter password: 

启动mosquito进行验证

订阅
 mosquitto_sub -t mytest  -h localhost -p 1883 -u test -P test

发布
mosquitto_pub  -t mytest -m mymessage -h localhost -p 1883 -u test -P test

3.配置无密码用户校验和tls单向认证

listener  8883  
cafile /etc/mosquitto/Myca/ca.crt
certfile /etc/mosquitto/Myca/server.crt
keyfile /etc/mosquitto/Myca/server.key
allow_anonymous true   

一般默认tls连接使用8883端口号。

接下来需要通过penssl生成证书。参考链接MQTT服务-Mosquitto简单安装及TLS双向认证配置 - 大脸猫爱吃鱼!!! - 博客园

sudo mkdir /etc/mosquitto/Myca 
cd /etc/mosquitto/Myca
sudo  openssl genrsa -des3 -out ca.key 2048
sudo openssl req -new -x509 -days 3650 -key ca.key -out ca.crt
sudo openssl genrsa -out server.key 2048
sudo openssl req -new -out server.csr -key server.key
sudo openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 3650

启动mosquito进行验证

订阅
mosquitto_sub -t testtls --cafile /etc/mosquitto/Myca/ca.crt -h 1.168.31.195 -p 8883 --tls-version tlsv1.2

发布
mosquitto_pub -t testtls -m tls_MQTT --cafile /etc/mosquitto/Myca/ca.crt -h 192.168.31.195 -p 8883 --tls-version tlsv1.2

4.配置密码用户校验和tls单向认证

listener  8883  
cafile /etc/mosquitto/Myca/ca.crt
certfile /etc/mosquitto/Myca/server.crt
keyfile /etc/mosquitto/Myca/server.key
allow_anonymous false    
password_file /etc/mosquitto/pwfile

启动mosquito进行验证

订阅
mosquitto_sub -t testtls --cafile /etc/mosquitto/Myca/ca.crt -h 1.168.31.195 -p 8883 --tls-version tlsv1.2 -u test -P test

发布
mosquitto_pub -t testtls -m tls_MQTT --cafile /etc/mosquitto/Myca/ca.crt -h 192.168.31.195 -p 8883 --tls-version tlsv1.2 -u test -P test

5.配置无密码用户校验和tls双向认证

listener  8883  
cafile /etc/mosquitto/Myca/ca.crt
certfile /etc/mosquitto/Myca/server.crt
keyfile /etc/mosquitto/Myca/server.key
allow_anonymous true    
require_certificate true 
use_identity_as_username true  

配置文件修改成如上。

参数配置详情可参考此链接mosquitto 配置详解(1):认证鉴权和通用配置项_知者智者的博客-CSDN博客_use_identity_as_username

生成客户端证书

sudo openssl genrsa -out client.key 2048
sudo openssl req -new -out client.csr -key client.key
sudo  openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 3650

启动mosquito进行验证

订阅
mosquitto_sub -t testtls --cafile /etc/mosquitto/Myca/ca.crt -h 192.168.31.195 -p 8883 --tls-version tlsv1.2 -u test -P test --cert /etc/mosquitto/Myca/client.crt --key /etc/mosquitto/Myca/client.key

发布
mosquitto_pub -t testtls -m tls_MQTT --cafile /etc/mosquitto/Myca/ca.crt -h 192.168.31.195 -p 8883 --tls-version tlsv1.2  --cert /etc/mosquitto/Myca/client.crt --key /etc/mosquitto/Myca/client.key

6.配置密码用户校验和tls双向认证

listener  8883  
cafile /etc/mosquitto/Myca/ca.crt
certfile /etc/mosquitto/Myca/server.crt
keyfile /etc/mosquitto/Myca/server.key
allow_anonymous false    
require_certificate true 
use_identity_as_username false  
password_file /etc/mosquitto/pwfile

启动mosquito进行验证

订阅
mosquitto_sub -t testtls --cafile /etc/mosquitto/Myca/ca.crt -h 192.168.31.195 -p 8883 --tls-version tlsv1.2 -u test -P test --cert /etc/mosquitto/Myca/client.crt --key /etc/mosquitto/Myca/client.key

发布
mosquitto_pub -t testtls -m tls_MQTT --cafile /etc/mosquitto/Myca/ca.crt -h 192.168.31.195 -p 8883 --tls-version tlsv1.2  --cert /etc/mosquitto/Myca/client.crt --key /etc/mosquitto/Myca/client.key -u test -P test

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
安装配置Mosquitto服务器是使用MQTT协议的关键步骤。以下是在Linux系统上安装配置Mosquitto服务器的一般步骤: 1. 安装Mosquitto服务器: - 在Ubuntu/Debian上,打开终端并运行以下命令: ``` sudo apt-get update sudo apt-get install mosquitto ``` - 在CentOS/RHEL上,打开终端并运行以下命令: ``` sudo yum update sudo yum install mosquitto ``` 2. 启动Mosquitto服务: - 在Ubuntu/Debian上,Mosquitto服务将自动启动。如果需要手动启动或停止服务,可以运行以下命令: ``` sudo systemctl start mosquitto # 启动Mosquitto服务 sudo systemctl stop mosquitto # 停止Mosquitto服务 ``` - 在CentOS/RHEL上,Mosquitto服务默认不会自动启动。你可以使用以下命令手动启动或停止服务: ``` sudo systemctl start mosquitto # 启动Mosquitto服务 sudo systemctl stop mosquitto # 停止Mosquitto服务 sudo systemctl enable mosquitto # 设置Mosquitto服务在系统启动时自动启动 ``` 3. 配置Mosquitto服务器: - Mosquitto配置文件位于`/etc/mosquitto/mosquitto.conf`(Ubuntu/Debian)或`/etc/mosquitto/conf.d/mosquitto.conf`(CentOS/RHEL)。 - 打开配置文件并根据需要进行修改。以下是一些常见的配置选项: - `listener`:设置服务器监听的端口,默认为1883。 - `allow_anonymous`:设置是否允许匿名连接,默认为true。 - `password_file`:指定保存用户密码的文件(如果使用用户名和密码进行认证)。 - `persistence`:设置是否启用持久化,默认为false。 - 更多配置选项,请参考Mosquitto官方文档:https://mosquitto.org/man/mosquitto-conf-5.html 4. 重启Mosquitto服务: - 在Ubuntu/Debian上,运行以下命令重启Mosquitto服务: ``` sudo systemctl restart mosquitto ``` - 在CentOS/RHEL上,运行以下命令重启Mosquitto服务: ``` sudo systemctl restart mosquitto ``` 安装配置Mosquitto服务器后,你就可以使用MQTT客户端进行连接和通信了。希望这能帮到你!如果你有任何其他问题,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值