11
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mosquitto で Username と Password を使う

Last updated at Posted at 2018-02-09

次のページを参考にしました。
Mosquitto Username and Password Authentication -Configuration and Testing

Ubuntu 24.04 でのソフトのインストール方法

sudo apt install mosquitto
sudo apt install mosquitto-clients

1 password.txt というファイルを作成します。

password.txt
steve:password
jim:topsecret

2 このファイルを暗号化します。

mosquitto_passwd -U password.txt

次のようなファイルに変換されます。

password.txt
steve:$6$4lRl06+qx3nS+0Gx$SZUEnAv/2u+RhIHJV7lM4NHh7R8AZcrtW1lqdqJ77N36meVnya+lvkX++/S1J+JzBrGWiLG5B20e8tfk1hM9oQ==
jim:$6$JtT1tjFAMAaxMQCt$hZc7Uuaa8UAwC61p/oy3JUAe0kExDskehtKwD6YRUezOajYHT7ZQcpX+1g3ofjsDBqs4LpSaHr3e9rAXc5pG+A==

3 変換されたファイルを /etc/mosquitto/ にコピーします。

4 /etc/mosquitto/conf.d/robot.txt というファイルを作成

/etc/mosquitto/conf.d/robot.txt
allow_anonymous false
password_file /etc/mosquitto/password.txt

5 mosquitto を再起動します。

sudo systemctl restart mosquitto

次は、ローカルでサーバーを動かしている時の接続例です。

サブスクライブ

mosquitto_sub -d -t orz \
        -u jim -P topsecret \
        --topic sensors/topic_1

パブリッシュ

mosquitto_pub -d -t orz -m "こんにちは Feb/09/2018 PM 13:47" \
        -u jim -P topsecret \
        --topic sensors/topic_1

パスワードが合っていない場合の実行結果

Client null sending CONNECT
Client null received CONNACK (5)
Connection error: Connection Refused: not authorised.
Error: The connection was refused.
11
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
16

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?