{"id":61,"date":"2025-03-29T17:49:29","date_gmt":"2025-03-29T17:49:29","guid":{"rendered":"https:\/\/ag.yomagnet.com\/index.php\/2025\/03\/29\/configuring-mosquitto-client-with-authentication\/"},"modified":"2026-09-29T07:46:11","modified_gmt":"2026-09-29T07:46:11","slug":"configuring-mosquitto-client-with-authentication","status":"publish","type":"post","link":"https:\/\/ag.yomagnet.com\/index.php\/2025\/03\/29\/configuring-mosquitto-client-with-authentication\/","title":{"rendered":"Configuring Mosquitto client with Authentication"},"content":{"rendered":"<p>Prerequisites:<br \/>\n1. Docker<br \/>\n2. Require an existing mosquitto client running on Local or on Virtual Machine or in docker to generate secret key using mosquitto_passwd from command-line<\/p>\n<p>If you dont have client running on your machine, follow this article <a href=\"https:\/\/ag.yomagnet.com\/deploying-mosquitto-message-broker-with-docker\/\">https:\/\/ag.yomagnet.com\/deploying-mosquitto-message-broker-with-docker\/<\/a> to run mosquitto client on machine using docker<\/p>\n<p>As I am already running my mosquitto client using docker, I will run the following docker exec command to generate passwd hash by running following command:<\/p>\n<pre>docker exec -it mosquitto-server mosquitto_passwd -b -c passwd parulbedi mosquitto@1234<\/pre>\n<p>Print the value of secret on terminal using cat command (for temporary purpose store this key locally on notepad)<\/p>\n<pre>docker exec -it mosquitto-server cat passwd<\/pre>\n<pre>You will get a similar output as follows:\r\nparulbedi:$7$101$sSJ7yv075mkm8E2S$DpESCEinRzLnuHLTKm4ynGMJOXDo9Kznc9\/8cUTGrFQSQxJmGwmpDJc0DFA150eMGTVnC5oUPuP8LTOMbjfd\/w==<\/pre>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/docker_exec_mq_client_pass.png\" \/><\/p>\n<p>Exit from container, goto <strong>mosquitto<\/strong> directory &amp; create a new directory with name <strong>mq_passwd<\/strong><\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/mq_passwd_dir.png\" \/><\/p>\n<p>Store the secret key in<strong> mq_passwd<\/strong> directory with filename <strong>mosquitto.passwd<\/strong><\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/update_passwd.png\" \/><\/p>\n<p>Stop &amp; remove currently running mosquitto client docker container<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/docker_stop_service.png\" \/><\/p>\n<p>goto <strong>mosquitto\/mq_config<\/strong> directory &amp; update <strong>allow_anonymous to false<\/strong> &amp; add <strong>password_file<\/strong> parameter in the <strong>mosquitto.conf<\/strong> file<\/p>\n<pre>allow_anonymous false\r\npassword_file \/mosquitto\/config\/mosquitto.passwd<\/pre>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/mq_config_update.png\" \/><\/p>\n<p>Update docker run command to add password configuration in mosquitto client,<\/p>\n<pre>docker run -d --name mosquitto-server -p 1883:1883 -v \"$HOME\/mosquitto\/mq_config:\/mosquitto\/config\" -v \"$HOME\/mosquitto\/mq_data:\/mosquitto\/data\" -v \"$HOME\/mosquitto\/mq_log:\/mosquitto\/log\" -v \"$HOME\/mosquitto\/mq_passwd\/mosquitto.passwd:\/mosquitto\/config\/mosquitto.passwd\" eclipse-mosquitto<\/pre>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/updated_mq_client.png\" \/><\/p>\n<p>Verify both the updated configurations are mounted inside the container<\/p>\n<div style=\"position: relative;display: inline-block\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/mq_passwd_file.png\" \/><br \/>\n<span style=\"position: absolute;top: 10px;left: 10px;color: white;padding: 10px 20px;border-radius: 5px;font-size: 16px;font-weight: bold\">1<\/span><\/div>\n<div style=\"position: relative;display: inline-block\"><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/mq_config_updated.png\" \/><br \/>\n<span style=\"position: absolute;top: 10px;left: 10px;color: white;padding: 10px 20px;border-radius: 5px;font-size: 16px;font-weight: bold\">2<\/span><\/div>\n<p>Lets try to send a MQTT message directly from command line by running the following command<\/p>\n<pre>mosquitto_pub -h localhost -t test -m \"test\"<\/pre>\n<p>Note: I got an error connection because now I need to provide username &amp; password in order to run my test command<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/mq_query_error.png\" \/><\/p>\n<pre>mosquitto_pub -h localhost -t test -m \"test\" -u \"parulbedi\" -P \"mosquitto@1234\"<\/pre>\n<p>Here,<br \/>\n-u parameter requires username<br \/>\n-P parameter requires password<\/p>\n<p>This time terminal move to next-line without any error, this means that command executes successfully<\/p>\n<p><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/ag.yomagnet.com\/wp-content\/uploads\/2025\/03\/mq_query_success.png\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"Prerequisites: 1. Docker 2. Require an existing mosquitto client running on Local or on Virtual Machine or in&hellip;","protected":false},"author":1,"featured_media":62,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"csco_display_header_overlay":false,"csco_singular_sidebar":"","csco_page_header_type":"","csco_page_load_nextpost":"","csco_page_reading_time":"","csco_page_toc_navigation":"","csco_post_video_location":[],"csco_post_video_location_hash":"","csco_post_video_url":"","csco_post_video_bg_start_time":0,"csco_post_video_bg_end_time":0,"csco_post_video_bg_volume":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-61","post","type-post","status-publish","format-standard","has-post-thumbnail","category-docker","cs-entry","cs-video-wrap"],"_links":{"self":[{"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":1,"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":63,"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/posts\/61\/revisions\/63"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/media\/62"}],"wp:attachment":[{"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ag.yomagnet.com\/index.php\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}