mosquitto ---配置SSL/TLS

在服务器电脑上面创建myCA文件夹, 如在/home/qa/ 文件夹下使用命令,

mkdir myCA 

然后执行以下命令,我们将创建并使用其他用户没有权限访问的目录。

sudo chmod 700
cd myCA
 
进入myCA文件夹,然后执行以下命令下载生成证书用的脚本。
wget https://github.com/owntracks/tools/raw/master/TLS/generate-CA.sh

下载完之后,在myCA文件夹下面执行命令

注:在生产环境中使用此实例之前,你应该定制the generate-CA.sh脚本

bash ./generate-CA.sh
 
generate-CA.sh会产生3个文件:ca.crt,ca.key,ca.csr分别为: 证书(.CRT),钥匙(.KEY),请求(.csr文件),这三个文件是给服务器使用的
 
在myCA文件夹下面执行命令
 
bash ./generate-CA.sh client myclient
 
其中myclient是给客户端生成证书的名称,可随意更改。执行完上述命令后,会新生成几个文件。
客户端需要使用的是ca.crt/myclient.crt/myclient.key这三个文件。
 
经过上述步骤之后,生成了服务器使用的ca.crt/localhost.crt/localhost.key文件和客户端使用的
ca.crt/myclient.crt/myclient.key这几个文件,其中ca.crt是同一个文件。
配置mosquitto.conf文件。该文件在mosquitto-1.4.11安装文件夹里面,打开该文件,修改成如下配置:
   port 8883
    protocol mqtt
    cafile /home/myCA/ubuntu/ca.crt
    certfile /home/myCA/ubuntu/localhost.crt
    keyfile /home/myCA/ubuntu/localhost.key
    require_certificate true
    use_identity_as_username true

修改完配置文件之后,执行启动命令mosquitto -c /home/qa/mosquitto-1.4.11/mosquitto.conf -v  

 根据自己的mosquitto-conf文件所在的位置修改上述命令中的参数,然后服务器就以SSL/TSL的方式开始启动。
  此时客户端使用上述过程产生的客户端证书连接该服务器,即可进行通信。

 

参考页面:https://mosquitto.org/man/mosquitto-tls-7.html

猜你喜欢

转载自www.cnblogs.com/saryli/p/9821343.html