Setting User Credentials|Mosquitto Broker

 The Eclipse moquitto provides the open source message Broker which supports the Mqtt Protocol versions 5.0,3.11,3,1.The Mqtt protocol is a very lightweight and can be operated on microcontroller based systems like Raspberry Pi to computer PC servers. The Mqtt clients can be operated from the small microcontroller based sensor which are constrained.

The Mosquitto Brokers can be installed on your systems by visiting the Eclipse mosquitto Download page.Here the setup file suitable to various os can be downloaded and installed. IF want to How to setup the Mosquiito Broker on WIndows PC then click here.

Once the Broker is setup on the system. We can initiate testing of the Mosquitto Broker by using mosquitto Publish and Subscribe Commands. To know some details about the Mqtt Publish and Subscribe operation details visit the blog Publish and Subscribe-Mqtt.






Set up Username and Password for the Moquitto Broker

The Username and password can be set for mosquitto Broker. With the client will have to provide username and password authentication for getting connected with the Broker. The USername and password can be seen in the plain text format  so it not the most secured way until and unless the Mqtt communication is encrypted with SSL/TLS. But any new client will have provide the User credential before establishing the connection with the Broker

In order to enable the functionality of setting up Username and password the following two things are
required to be done.

1. Create the password file 

2. Edit Mosquitto config file to provide the path for the password file.


There two methods with which the username and password can be set


Method 1

Create new the password file like password.txt  and specify the user name and password as shown below.




In this file two username passwords are set 

1. abc:1234
2.efg:5678

Save the file in the same folder where mosquitto Broker application is present.(In my case, it is c:\mosquitto).


In the next step we will the utility called the mosquitto utility called mosquitto_passwd  (which is located in the same folder where moquitto Broker application is located). to encrypt the password

Run the following command from the command prompt terminal for windows 

mosquitto_passwd -U password file name.

C:\mosquitto>mosquitto_passwd -U password.txt


Now open the password.txt file to check whether the passwords are encrypted or not. Here are the contests of the password file.




Open the mosquitto.conf file or newly created test.conf file(which was created for mosquitto broker).
and set the allow_annonymous as false and specify the path for the password file as shown below.



 

Now run the mosquitto application with the modified test.conf file  from the command prompt terminal.


C:\mosquitto>mosquitto -c test.conf -v
1658660510: mosquitto version 2.0.14 starting
1658660510: Config loaded from test.conf.
1658660510: Opening ipv6 listen socket on port 1883.
1658660510: Opening ipv4 listen socket on port 1883.
1658660510: mosquitto version 2.0.14 running

Testing the  Mosquitto Broker with User Credentails

We will test the username and password authentication for the mosquitto Broker by running the Publish message command from new command prompt terminal.

mosquitto_pub -h localhost -t test -m hello -r  -d 

log for publisher terminal. 

c:\mosquitto>mosquitto_pub -h localhost -t test -m hello -r  -d
Client null sending CONNECT
Client null received CONNACK (5)
Connection error: Connection Refused: not authorised.
Error: The connection was refuse


log for the Broekr Termianl

C:\mosquitto>mosquitto -c test.conf -v
1658661009: mosquitto version 2.0.14 starting
1658661009: Config loaded from test.conf.
1658661009: Opening ipv6 listen socket on port 1883.
1658661009: Opening ipv4 listen socket on port 1883.
1658661009: mosquitto version 2.0.14 running
1658661016: New connection from ::1:54448 on port 1883.
1658661016: Sending CONNACK to auto-47DE6F65-2C93-C346-0EB1-16C62830A448 (0, 5)
1658661016: Client auto-47DE6F65-2C93-C346-0EB1-16C62830A448 disconnected, not authorised.

The log shows the connection has been refused by the Broker

We will try the mosquitto_pub command with user credentials.

mosquitto_pub -h localhost -t test -m hello -r  -u abc  -P 1234 -d

In this command user name abc and password 1234 is provided.Run this command

log for Publisher client terminal

c:\mosquitto>mosquitto_pub -h localhost -t test -m hello -r  -u abc  -P 1234 -d
Client null sending CONNECT
Client null received CONNACK (0)
Client null sending PUBLISH (d0, q0, r1, m1, 'test', ... (5 bytes))
Client null sending DISCONNECT


log for the Broker terminal

1658661346: New connection from ::1:54469 on port 1883.
1658661346: New client connected from ::1:54469 as auto-BE767873-B3EE-1A3D-E9EB-B6A030638C69 (p2, c1, k60, u'abc').
1658661346: No will message specified.
1658661346: Sending CONNACK to auto-BE767873-B3EE-1A3D-E9EB-B6A030638C69 (0, 0)
1658661346: Received PUBLISH from auto-BE767873-B3EE-1A3D-E9EB-B6A030638C69 (d0, q0, r1, m0, 'test', ... (5 bytes))
1658661346: Received DISCONNECT from auto-BE767873-B3EE-1A3D-E9EB-B6A030638C69
1658661346: Client auto-BE767873-B3EE-1A3D-E9EB-B6A030638C69 disconnected.

Now the Broker accepted the connection and received the published message.

Now the run the subscriber command from the new command prompt terminal with second username password combination i.e. username def  and password as 5678 

mosquitto_sub -h localhost - t test  -u def   -P 5678 -d

log for subscriber terminal

c:\mosquitto>mosquitto_sub -h localhost -t test  -u efg -P 5678 -d
Client null sending CONNECT
Client null received CONNACK (0)
Client null sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0, Options: 0x00)
Client null received SUBACK
Subscribed (mid: 1): 0
Client null received PUBLISH (d0, q0, r1, m0, 'test', ... (5 bytes))
hello

So the subscriber client has got connected with the Broker and received the message hello over topic test.


Method 2

The password file can be created using the command as

mosquitto_passwd -c passwordfile user

Here user name is to be specified.

Run this command file from the command prompt.

c:\mosquitto>mosquitto_passwd -c password1 uvv
Password:
Reenter password:

here password file name is password1 and username os uvv.The utility prompts for the password.
Enter the password and then renter the password again,(Be careful, the password is not echoed.).
The password entered is abc.

Create the other username and password in the by using the command.

mosquitto_passwd -b password1 username password

Run the command from the command prompt.

mosquitto_passwd -b password1 xyz efg


Check the password file created in the folder where the mosquitto Broker is installed,



Enter the new password1 file name with path in the mosquitto.conf or test.conf file

as password_file c:\mosquitto\password1.

Run fresh mosquitto application from the command prompt with the modified conf file with the command.

mosquitto -c test.conf    -v

Test the Broker with new username and password by using the mosquitto_pub command

mosquitto_pub -h localhost -t test -m hello -r  -u uvv  -P abc -r -d.

Log for Publisher client

c:\mosquitto>mosquitto_pub -h localhost -t test -m hello -r  -u uvv  -P abc -r -d
Client null sending CONNECT
Client null received CONNACK (0)
Client null sending PUBLISH (d0, q0, r1, m1, 'test', ... (5 bytes))
Client null sending DISCONNECT

The connection of the client is successful.

Run the subscribe command from the command prompt with new username and password.

mosquitto_sub -h localhost -t test  -u uvv -P abc -d

Log for subscriber client

c:\mosquitto>mosquitto_sub -h localhost -t test  -u uvv -P abc -d
Client null sending CONNECT
Client null received CONNACK (0)
Client null sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0, Options: 0x00)
Client null received SUBACK
Subscribed (mid: 1): 0

The subscriber got successfully connected with the Broker.


Related Blogs:





Related Youtube:







Post a Comment

Previous Post Next Post