$linuxjunkies
>

mosquitto_sub(1)

Subscribe to MQTT topics and receive published messages from a Mosquitto broker.

UbuntuDebianFedoraArch

Synopsis

mosquitto_sub [OPTION]... -t TOPIC [TOPIC ...]

Description

mosquitto_sub is a simple MQTT client that subscribes to one or more topics on a Mosquitto broker and prints all received messages to standard output. It connects to the specified broker, subscribes to the given topics using MQTT topic filters (which support wildcards), and displays incoming messages in real-time.

Topic names can include wildcards: the + character matches a single level, and # matches multiple levels. Multiple topics can be specified by using the -t flag multiple times. The client will stay connected until terminated or until an error occurs.

Common options

FlagWhat it does
-h HOSTHostname or IP address of the MQTT broker (default: localhost)
-p PORTPort number for the broker connection (default: 1883 for MQTT, 8883 for MQTT over TLS)
-t TOPICMQTT topic(s) to subscribe to; use multiple -t for multiple topics
-q QoSQuality of Service level (0, 1, or 2; default: 0)
-u USERNAMEUsername for authentication with the broker
-P PASSWORDPassword for authentication with the broker
-vPrint topic name along with message payload
-F FORMATOutput format string with escape sequences like %t (topic), %m (message), %q (QoS)
--cafile FILEPEM file containing trusted CA certificates for TLS connections
-i CLIENT_IDClient ID to use when connecting to the broker (default: mosquitto_sub_PID)
-k KEEPALIVEKeep-alive interval in seconds (default: 60)
-C MSG_COUNTExit after receiving the specified number of messages

Examples

Subscribe to a single topic on a remote broker and print all messages

mosquitto_sub -h broker.example.com -t 'home/temperature'

Subscribe to multiple topic patterns using wildcards to match all temperature and humidity sensors

mosquitto_sub -t 'home/+/temperature' -t 'home/+/humidity'

Subscribe to all topics under 'sensors/' and print both topic name and message with -v flag

mosquitto_sub -h broker.example.com -t 'sensors/#' -v

Connect to a TLS-enabled broker on a non-standard port with authentication

mosquitto_sub -h 192.168.1.100 -p 8883 -u myuser -P mypass -t 'status/device' --cafile ca.crt

Subscribe with custom output formatting to show topic name and message with a temperature symbol

mosquitto_sub -t 'home/temperature' -F '%t: %m°C'

Subscribe with QoS level 1 and exit after receiving 10 messages

mosquitto_sub -t 'alerts/#' -C 10 -q 1

Related commands