摘自:https://blog.csdn.net/ty1121466568/article/details/81118468 shell
本文爲參考網上其餘博文搭建出服務器後的步驟記錄,若有冒犯,請私信!!!vim
目錄... 3服務器
1.2方法二:在Ubuntu下使用apt-get安裝... 5ide
在Ubuntu下安裝該軟件的經常使用方法有兩種,兩種均能使該軟件做爲中轉服務器使用,不過筆者在使用方法二安裝的mosquitto時,有些配置沒有生效,不知是不是筆者當時在配置是否有誤仍是該方式安裝的不徹底(該方式安裝的mosquitto要比編譯安裝的少一些示例配置文件)。要想使用雙向認證,單向認證等功能,推薦使用方法一。
step1.下載源碼
Mosquitto官網:http://mosquitto.org/
Mosquitto最新的源碼:http://mosquitto.org/files/source/
選擇一個喜歡的版本便可,筆者在這裏選擇最新的1.5版
mosquitto-1.5.tar.gz
step2.解壓源碼
tar -xzvf mosquitto-1.5.tar.gz
step3.進入源碼目錄
cd mosquitto-1.5/
step4.編譯和安裝
make && make install
其中,make install須要root權限。
在安裝過程當中會因爲缺失一些庫而報錯,直接百度,而後安裝便可
可能遇到的問題及其解決方式:
【1】編譯找不到openssl/ssl.h
【解決方法】——安裝openssl
sudo apt-get install libssl-dev
【2】編譯過程找不到ares.h
sudo apt-get install libc-ares-dev
【3】編譯過程找不到uuid/uuid.h
sudo apt-get install uuid-dev
【4】使用過程當中找不到libmosquitto.so.1
error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
【解決方法】——修改libmosquitto.so位置
# 建立連接
sudo ln -s /usr/local/lib/libmosquitto.so.1 /usr/lib/libmosquitto.so.1
# 更新動態連接庫
sudo ldconfig
【5】make: g++:命令未找到
【解決方法】
安裝g++編譯器
sudo apt-get install g++
step5.程序安裝位置:
step6.本機測試
打開一個終端,執行如下命令訂閱主題"mqtt"
$mosquitto_sub -h localhost -t "mqtt"-v
打開另一個終端,發佈消息到主題 「mqtt」
$mosquitto_pub -h localhost -t "mqtt"-m "Hello MQTT"
如今你會看到消息被顯示在前一個終端上了.
step7. 網絡測試(test.mosquitto.org)
從test.mosquitto.org訂閱全部主題,任何人發佈的任何消息你均可以收到,你會看到大量有趣消息滾動在你的屏幕上。
你也能夠發佈一些有趣的消息,全部訂閱的人也一樣會收到。
$mosquitto_sub -h test.mosquitto.org -t "#" -v
固然,你也能夠只訂閱特定主題,用來接收你本身的消息
$mosquitto_sub -h test.mosquitto.org -t "msg_only_from_sgks" -v
在另一個終端上發佈消息到特定主題,消息"My cat is Luna"應該會顯示在上一個終端上
$mosquitto_pub -h test.mosquitto.org -t "msg_only_from_sgks" -m "My cat is Luna"
step 1. 引入mosquitto倉庫並更新
$sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
$sudo apt-get update
step 2. 執行如下命令安裝mosquitto包
$sudo apt-get installmosquitto
step 3. 安裝mosquitto開發包
$sudo apt-get installlibmosquitto-dev
step 4. 安裝mosquitto客戶端
$sudo apt-get installmosquitto-clients
step 5. 查詢mosquitto是否正確運行
$sudo service mosquitto status
step 6. 本機測試
打開一個終端,執行如下命令訂閱主題"mqtt"
$mosquitto_sub -h localhost -t "mqtt"-v
打開另一個終端,發佈消息到主題 「mqtt」
$mosquitto_pub -h localhost -t "mqtt"-m "Hello MQTT"
如今你會看到消息被顯示在前一個終端上了.
step 7. 網絡測試(test.mosquitto.org)
從test.mosquitto.org訂閱全部主題,任何人發佈的任何消息你均可以收到,你會看到大量有趣消息滾動在你的屏幕上。
你也能夠發佈一些有趣的消息,全部訂閱的人也一樣會收到。
$mosquitto_sub -h test.mosquitto.org -t "#" -v
固然,你也能夠只訂閱特定主題,用來接收你本身的消息
$mosquitto_sub -h test.mosquitto.org -t "msg_only_from_sgks" -v
在另一個終端上發佈消息到特定主題,消息"My cat is Luna"應該會顯示在上一個終端上
$mosquitto_pub -h test.mosquitto.org -t "msg_only_from_sgks" -m "My cat is Luna"
使用以下shell 來生成證書:
1 # * Redistributions in binary form must reproduce the above copyright 2 3 # notice, this list of conditions and the following disclaimer in the 4 5 # documentation and/or other materials provided with the distribution. 6 7 # * Neither the name of the axTLS project nor the names of its 8 9 # contributors may be used to endorse or promote products derived 10 11 # from this software without specific prior written permission. 12 13 # 14 15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 22 23 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 26 27 # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 30 31 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 33 # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 35 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 37 # 38 39 40 41 # 42 43 # Generate the certificates and keys for testing. 44 45 # 46 47 48 49 50 51 PROJECT_NAME="TLS Project" 52 53 54 55 # Generate the openssl configuration files. 56 57 cat > ca_cert.conf << EOF 58 59 [ req ] 60 61 distinguished_name = req_distinguished_name 62 63 prompt = no 64 65 66 67 [ req_distinguished_name ] 68 69 O = $PROJECT_NAME Dodgy Certificate Authority 70 71 EOF 72 73 74 75 cat > server_cert.conf << EOF 76 77 [ req ] 78 79 distinguished_name = req_distinguished_name 80 81 prompt = no 82 83 84 85 [ req_distinguished_name ] 86 87 O = $PROJECT_NAME 88 89 CN = 192.168.111.100 90 91 EOF 92 93 94 95 cat > client_cert.conf << EOF 96 97 [ req ] 98 99 distinguished_name = req_distinguished_name 100 101 prompt = no 102 103 104 105 [ req_distinguished_name ] 106 107 O = $PROJECT_NAME Device Certificate 108 109 CN = 192.168.111.101 110 111 EOF 112 113 114 115 mkdir ca 116 117 mkdir server 118 119 mkdir client 120 121 mkdir certDER 122 123 124 125 # private key generation 126 127 openssl genrsa -out ca.key 1024 128 129 openssl genrsa -out server.key 1024 130 131 openssl genrsa -out client.key 1024 132 133 134 135 # cert requests 136 137 openssl req -out ca.req -key ca.key -new \ 138 139 -config ./ca_cert.conf 140 141 openssl req -out server.req -key server.key -new \ 142 143 -config ./server_cert.conf 144 145 openssl req -out client.req -key client.key -new \ 146 147 -config ./client_cert.conf 148 149 150 151 # generate the actual certs. 152 153 openssl x509 -req -in ca.req -out ca.crt \ 154 155 -sha1 -days 5000 -signkey ca.key 156 157 openssl x509 -req -in server.req -out server.crt \ 158 159 -sha1 -CAcreateserial -days 5000 \ 160 161 -CA ca.crt -CAkey ca.key 162 163 openssl x509 -req -in client.req -out client.crt \ 164 165 -sha1 -CAcreateserial -days 5000 \ 166 167 -CA ca.crt -CAkey ca.key 168 169 170 171 openssl x509 -in ca.crt -outform DER -out ca.der 172 173 openssl x509 -in server.crt -outform DER -out server.der 174 175 openssl x509 -in client.crt -outform DER -out client.der 176 177 178 179 mv ca.crt ca.key ca/ 180 181 mv server.crt server.key server/ 182 183 mv client.crt client.key client/ 184 185 186 187 mv ca.der server.der client.der certDER/ 188 189 190 191 rm *.conf 192 193 rm *.req 194 195 rm *.srl
將上述代碼保存爲makefile.sh
作以下修改,終端執行。
- 修改 CN 域中 IP 地址爲你主機/設備的 IP 地址
- [可選]加密位數 1024 修改成你須要的加密位數
進行以下測試,以驗證證書是否可用:
$openssl verify -CAfile ca/ca.crt server/server.crt
$openssl verify -CAfile ca/ca.crt client/client.crt
結果以下:
step 1.進入配置文件
sudo vim /etc/mosquitto/mosquitto.conf
step 2.找到 Default listener,在該欄下進行以下配置
再找到Certificate based SSL/TLS support字段.
即:
port 8883 cafile /etc/mosquitto/CA/ca.crt certfile /etc/mosquitto/CA/server/server.crt keyfile /etc/mosquitto/CA/server/server.key require_certificate true use_identity_as_username true
|
根據本身路徑不一樣配置校驗文件路徑,筆者是把文件放在/etc/mosquitto/CA/下
注意!!!
根據單向認證和雙向認證須要,可能需修改的字段有:
a) port 8883 // MQTT服務器將選擇此端口 listen
b) cafile /etc/mosquitto/CA/ca.crt
雙向認證必須配置爲你的CA證書
單向認證(一般認爲是client校驗server證書,下同)可選配置
單向認證中,server 和 client 端 ca 配置必須保持一致。即 server 若配置 ca.crt ,則 client 必須配置 ca.crt, server 不配置ca.crt ,client 也不可配置 ca.crt
路徑必須爲絕對路徑!!!
c) certfile /etc/mosquitto/CA/server/server.crt
單項認證和雙向認證都必須配置爲你的server證書
d) keyfile /etc/mosquitto/CA/server/server.key
單項認證和雙向認證都必須配置爲你的server私鑰
e) require_certificate true
單向認證需設置爲 false,註釋此行,默認也是 false
雙向認證必須配置爲true
f) use_identity_as_username true
單向認證設置爲 false,註釋此行,默認也是 false
雙向認證一般設置爲true
step 3.重啓服務
mosquitto -c /etc/mosquitto/mosquitto.conf
若是提示端口被佔用,先ps出mosquitto,再kill掉
ps -aux | grep "mosquitto"
kill -9 XXXXX
單向認證只須要註釋兩行便可:
#require_certificate true
#use_identity_as_username true
以下:
port 8883 cafile /etc/mosquitto/CA/ca/ca.crt certfile /etc/mosquitto/CA/server/server.crt keyfile /etc/mosquitto/CA/server/server.key
|
step 1.
在先將/etc/mosquitto/mosquitto.conf 文件按2.4節配置默認打開雙向認證,再找到 Extra listener字段
進行以下配置,打開另外一個端口用做單向認證
step 2.
再在該字段下找到Certificate based SSL/TLS support字段
step 3.重啓服務
mosquitto -c /etc/mosquitto/mosquitto.conf
若是提示端口被佔用,先ps出mosquitto,再kill掉
ps -aux | grep "mosquitto"
kill -9 XXXXX
step 1.進入到CA證書目錄下:
step 2.雙向:
終端一:訂閱
mosquitto_sub -h 10.30.11.47 -p 8883 -t "mqtt/server/topic" --cafile ./ca/ca.crt --cert ./client/client.pem --key ./client/client.key &
終端二:發佈
mosquitto_pub -h 10.30.11.47 -p 8883 -t "mqtt/server/topic" -m "hello,world!" --cafile ./ca/ca.crt --cert ./server/server.pem --key ./server/server.key
step 3. 單向
終端一:訂閱
mosquitto_sub -h 10.30.11.47 -p 8884 -t "mqtt/server/topic" --cafile ./ca/ca.crt &
終端二:發佈
mosquitto_pub -h 10.30.11.47 -p 8884 -t "mqtt/server/topic" -m " hello,world!" --cafile ./ca/ca.crt