Linux command
mosquitto_sub 命令
文本
复制后可按需替换文件名、目录或参数。
常用示例
Subscribe to topic
mosquitto_sub -t [sensors/temperature]
Subscribe to all topics
mosquitto_sub -t '#'
Subscribe with wildcard
mosquitto_sub -t '[sensors/+/temperature]'
Connect to remote broker
mosquitto_sub -h [broker.example.com] -t [topic]
Subscribe with authentication
mosquitto_sub -h [broker] -u [username] -P [password] -t [topic]
Subscribe with TLS
mosquitto_sub -h [broker] --cafile [ca.crt] -t [topic]
Show verbose output
mosquitto_sub -v -t '[sensors/#]'
Read single message and exit
mosquitto_sub -t [topic] -C 1
说明
mosquitto_sub subscribes to MQTT topics and prints received messages. MQTT is a lightweight publish/subscribe messaging protocol for IoT and telemetry. Topics use hierarchical naming with / separators. Wildcards expand subscriptions: + matches one level, # matches all remaining levels. For example, sensors/+/temperature matches any sensor's temperature. Quality of Service levels control delivery guarantees: QoS 0 is fire-and-forget, QoS 1 ensures at-least-once delivery, QoS 2 guarantees exactly-once delivery. Retained messages are stored by the broker and sent immediately on subscription. This provides last-known values to new subscribers. TLS encryption protects communication. Client certificates enable mutual authentication. The broker must be configured to accept TLS connections.
参数
- -t _TOPIC_
- Topic to subscribe to (can repeat).
- -h _HOST_
- Broker hostname.
- -p _PORT_
- Broker port (default 1883).
- -u _USER_
- Username.
- -P _PASSWORD_
- Password.
- -i _ID_
- Client ID.
- -v
- Verbose (print topic with message).
- -C _COUNT_
- Exit after receiving count messages.
- -q _QOS_
- Quality of service (0, 1, 2).
- --cafile _FILE_
- CA certificate for TLS.
- --cert _FILE_
- Client certificate.
- --key _FILE_
- Client private key.
- -F _FORMAT_
- Output format string.
- -N
- Don't append newline to messages.
- --retained-only
- Only show retained messages.
FAQ
What is the mosquitto_sub command used for?
mosquitto_sub subscribes to MQTT topics and prints received messages. MQTT is a lightweight publish/subscribe messaging protocol for IoT and telemetry. Topics use hierarchical naming with / separators. Wildcards expand subscriptions: + matches one level, # matches all remaining levels. For example, sensors/+/temperature matches any sensor's temperature. Quality of Service levels control delivery guarantees: QoS 0 is fire-and-forget, QoS 1 ensures at-least-once delivery, QoS 2 guarantees exactly-once delivery. Retained messages are stored by the broker and sent immediately on subscription. This provides last-known values to new subscribers. TLS encryption protects communication. Client certificates enable mutual authentication. The broker must be configured to accept TLS connections.
How do I run a basic mosquitto_sub example?
Run `mosquitto_sub -t [sensors/temperature]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
What does -t _TOPIC_ do in mosquitto_sub?
Topic to subscribe to (can repeat).