OpenWrt ubus CLI in practice (2): list, call, listen, send, monitor
Grow through practice
No need to finish reading — test nowubus 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
ubusdis the bus daemon; the default socket is/var/run/ubus/ubus.sock.ubusis the client CLI.-s <socket>selects another socket, which is useful when running the official examples on a PC.- An empty
ubus listdoes 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 5sets a five-second timeout; the default is 30 seconds.ubus callisubus_lookup_idplusubus_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
-mselects message types such asinvoke,data,notify,add_object.-M rshows receive traffic;-M tshows transmit traffic.- Payloads may contain secrets; do not leave it running.
- Some firmware/ACLs deny monitor; fall back to
logreador server logs.
Troubleshooting
| Symptom | Likely cause | First check |
|---|---|---|
Failed to connect to ubus | ubusd is down or socket path is wrong | Check ps, pass -s |
Command failed: ... (Not found) | Object or method name is wrong | Compare with ubus list -v |
Invalid argument | JSON name/type mismatch, or ACL rejected object registration | Check policy and server logs |
Permission denied | Current user/group lacks ACL access | Verify as root, then add ACL |
| No reply | Method deferred but never replied | Use -t, inspect server callback |
Parse error | Invalid JSON or shell quoting | echo 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 callJSON 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/ubus414c60a, CLI behavior incli.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 boardfields and ACL behavior vary. - ❓ Not covered: monitor permissions across firmware, event loss/backpressure.
One sentence
Start with
ubus list -v, validate withubus call, then uselistenandmonitorto 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.
Comments 0
No comments yet — be the first!