← All posts

OpenWrt ubus CLI in practice (2): list, call, listen, send, monitor

Grow through practice

No need to finish reading — test now

ubus is not REST and it is not a config file. It is OpenWrt's inter-process bus. The CLI is best for three jobs: confirm an object exists, validate method arguments, and watch events or calls.

Know which socket you are talking to

  • ubusd is the bus daemon; the default socket is /var/run/ubus/ubus.sock.
  • ubus is the client CLI.
  • -s <socket> selects another socket, which is useful when running the official examples on a PC.
  • An empty ubus list does not mean the bus is broken; it may simply have no registered objects.
ubus list
ubus list -v
ubus list network.interface

-v prints methods and argument types, while -S gives simpler output for scripts. On a PC:

./ubus -s /tmp/ubus.sock list -v

call: invoke a method

ubus call system board
ubus call system info
ubus call network.interface dump

system board fields vary by OpenWrt version and device, but the shape is usually:

{
  "kernel": "5.15.x",
  "hostname": "OpenWrt",
  "system": "MediaTek MT7621",
  "model": "Your Device",
  "board_name": "your_board",
  "release": {
    "distribution": "OpenWrt",
    "version": "23.05.x"
  }
}

Pass a JSON object for methods that take arguments:

ubus call test hello '{"id":1,"msg":"hello"}'

Key points:

  • The argument is a JSON object, not key=value.
  • Method and field names must match the signature from ubus list -v.
  • -t 5 sets a five-second timeout; the default is 30 seconds.
  • ubus call is ubus_lookup_id plus ubus_invoke; check the return code instead of assuming “no output” means success.

listen and send: events

ubus send emits an event; ubus listen subscribes by event name and defaults to *.

# terminal 1
ubus listen

# terminal 2
ubus send myevent '{"msg":"hello"}'

Listen to one event:

ubus listen myevent

Object notifications are different; use ubus subscribe:

ubus subscribe network.interface

To wait for an object instead of polling with sleep:

ubus wait_for network.interface.wan

monitor: capture bus traffic

ubus monitor shows who calls whom and what payload is sent. It is useful when a command succeeds but the behavior is wrong.

ubus monitor
ubus monitor -m invoke -m data
ubus monitor -M r
  • -m selects message types such as invoke, data, notify, add_object.
  • -M r shows receive traffic; -M t shows transmit traffic.
  • Payloads may contain secrets; do not leave it running.
  • Some firmware/ACLs deny monitor; fall back to logread or server logs.

Troubleshooting

SymptomLikely causeFirst check
Failed to connect to ubusubusd is down or socket path is wrongCheck ps, pass -s
Command failed: ... (Not found)Object or method name is wrongCompare with ubus list -v
Invalid argumentJSON name/type mismatch, or ACL rejected object registrationCheck policy and server logs
Permission deniedCurrent user/group lacks ACL accessVerify as root, then add ACL
No replyMethod deferred but never repliedUse -t, inspect server callback
Parse errorInvalid JSON or shell quotingecho the argument and quote JSON with single quotes

A common trap: an empty ubus list plus Not found usually means the server failed to register its object, not that ubus is broken. Under the default ACL, a non-root ubus_add_object can return Invalid argument; verify as root first, then configure ACL.

Reproduce on a PC

The official examples run on Ubuntu without an OpenWrt device:

git clone https://github.com/openwrt/ubus.git
cmake -S ubus -B ubus/build \
  -DBUILD_LUA=OFF -DBUILD_EXAMPLES=ON
cmake --build ubus/build -j"$(nproc)"

sudo ./ubus/build/ubusd -s /tmp/ubus.sock
sudo ./ubus/build/examples/server -s /tmp/ubus.sock
./ubus/build/ubus -s /tmp/ubus.sock list -v

The server registers a test object with hello, watch, and count. See OpenWrt ubus official examples (1): the data model for the policy and payload details.

Boundaries

  • The CLI is for debugging and scripts, not a stable production interface; use the C API or ubus RPC for integration.
  • ubus call JSON is not a cross-version ABI; scripts should tolerate field changes.
  • Policy only checks names and types; it is not a full schema.
  • Events are notifications, not a reliable queue; an offline process will miss them.

Verification

  • ✅ Source: openwrt/ubus 414c60a, CLI behavior in cli.c.
  • ✅ PC: official examples build and run on an isolated socket; see part 1 for the data model.
  • ⚠️ Not verified command-by-command on a physical OpenWrt device; system board fields and ACL behavior vary.
  • ❓ Not covered: monitor permissions across firmware, event loss/backpressure.

One sentence

Start with ubus list -v, validate with ubus call, then use listen and monitor to see events and calls; do not start with C.


Source: openwrt/ubus (414c60a) Series: OpenWrt ubus Official Examples · Next: a minimal server/client with the C API.

Practice

Found this useful?

I write about AI agents × embedded / edge AI. Subscribe, or email me a concrete problem.

Guestbook →Subscribe (RSS)Support

Comments 0

No comments yet — be the first!

No login required; name is optional.