Commands Cheat Sheet

Evaluating engineering tools? Get the comparison in Google Sheets

(Perfect for making buy/build decisions or internal reviews.)

Most-used commands
Your email is safe thing.

Thankyou for your submission

We have sent the cheatsheet on your email!
Oops! Something went wrong while submitting the form.

Connection

mosquitto_sub -h [hostname] -p [port] -t [topic] -u [username] -P [password]
Subscribe to a topic with authentication

mosquitto_pub -h [hostname] -p [port] -t [topic] -m [message] -u [username] -P [password]
Publish to a topic with authentication

mosquitto_sub -h [hostname] -p [port] -t [topic] -v
Subscribe and display topic name with messages

mosquitto_pub -h [hostname] -p [port] -t [topic] -f [filename]
Publish file contents to a topic

Topic Management

mosquitto_sub -t '#'
Subscribe to all topics (wildcard)

mosquitto_sub -t '+/sensors/#'
Use wildcards for pattern matching topics

mosquitto_sub -t 'device/+/temperature'
Subscribe to specific pattern with single-level wildcard

Quality of Service (QoS)

mosquitto_pub -t [topic] -m [message] -q 0
Publish with QoS 0 (at most once)

mosquitto_pub -t [topic] -m [message] -q 1
Publish with QoS 1 (at least once)

mosquitto_pub -t [topic] -m [message] -q 2
Publish with QoS 2 (exactly once)

mosquitto_sub -t [topic] -q 2
Subscribe with QoS 2

Retained Messages

mosquitto_pub -t [topic] -m [message] -r
Publish a retained message

mosquitto_pub -t [topic] -n -r
Clear a retained message (publish empty message with retain flag)

Secure Connections

mosquitto_sub -h [hostname] -p 8883 --cafile [ca_file] -t [topic]
Subscribe using TLS

mosquitto_pub -h [hostname] -p 8883 --cafile [ca_file] --cert [cert_file] --key [key_file] -t [topic] -m [message]
Publish using TLS with client certificate

Broker Management

mosquitto -c /etc/mosquitto/mosquitto.conf
Start broker with configuration file

mosquitto -v
Start broker in verbose mode

mosquitto_passwd -c [password_file] [username]
Create password file and add user

mosquitto_passwd -b [password_file] [username] [password]
Add/update user non-interactively

Debugging

mosquitto_sub -d -t [topic]
Subscribe with debug output

mosquitto_pub -d -t [topic] -m [message]
Publish with debug output

mosquitto -v
Run broker with verbose logging