Key takeaways

  • UCI is the single source of configuration on OpenWrt: plain-text files in /etc/config, edited through the uci tool and applied by reloading services.
  • Use uci batch for repeatable multi-line changes and /etc/uci-defaults/ for first-boot configuration baked into images.
  • Generate per-device configuration from templates and variables, store the rendered result, and compare it with the device to detect drift.
  • Write idempotent changes: running the same change twice should leave the device in the same state.

OpenWrt has one of the cleanest configuration models in embedded Linux. Nearly everything, from network interfaces to the firewall to Wi-Fi, lives in a handful of structured text files. That makes configuration easy to read and script on one device, and very manageable across many, provided you treat it as data rather than as something people edit by hand.

The UCI model

UCI (Unified Configuration Interface) stores configuration as packages, sections and options. Each package is a file in /etc/config: network, wireless, firewall, dhcp, system, dropbear, uhttpd and so on. A section has a type and an optional name; options are single values, and lists hold multiple values.

# /etc/config/network (excerpt)
config interface 'lan'
    option device 'br-lan'
    option proto 'static'
    option ipaddr '192.168.1.1'
    option netmask '255.255.255.0'

config device
    option name 'br-lan'
    option type 'bridge'
    list ports 'lan1'
    list ports 'lan2'

Named sections ('lan') are addressed by name, as in network.lan.ipaddr. Anonymous sections are addressed by type and index, as in firewall.@rule[0]. Indexes shift when sections are added or removed, which is a frequent source of scripting bugs. Prefer named sections in anything you generate.

Services do not read the files directly at runtime in most cases. procd init scripts and netifd read UCI when a service starts or reloads, so a change on disk takes effect only after the relevant service reloads.

Working with the uci command

The uci tool edits a staging area and writes it out on commit:

uci show network.lan              # inspect a section
uci get network.lan.ipaddr        # read one value
uci set network.lan.ipaddr='10.10.0.1'
uci add_list dhcp.lan.dhcp_option='6,10.10.0.2'
uci changes                       # list uncommitted changes
uci revert network                # discard them
uci commit network                # write /etc/config/network
reload_config                     # reload services whose config changed

For anything longer than a couple of lines, use uci batch. It reads commands from standard input and applies them in one process, which is faster and easier to generate from a template:

uci -q batch <<'EOF'
set network.guest=interface
set network.guest.proto='static'
set network.guest.device='br-guest'
set network.guest.ipaddr='192.168.20.1'
set network.guest.netmask='255.255.255.0'
set firewall.guest=zone
set firewall.guest.name='guest'
set firewall.guest.network='guest'
set firewall.guest.input='REJECT'
set firewall.guest.forward='REJECT'
set firewall.guest.output='ACCEPT'
commit
EOF
reload_config

Note the set section=type form: it creates a named section if missing, and is harmless if it exists. That property matters for idempotency, covered below.

First-boot configuration with uci-defaults

Scripts in /etc/uci-defaults/ run once during first boot after flashing or a factory reset. If a script exits with status 0, it is deleted; if it fails, it stays and runs again on the next boot. This is the standard way to bake configuration into a custom image built with the SDK or Image Builder, by placing files under files/etc/uci-defaults/.

#!/bin/sh
# files/etc/uci-defaults/90-company-defaults
uci -q batch <<'EOF'
set system.@system[0].timezone='IST-5:30'
set system.@system[0].zonename='Asia/Kolkata'
set dropbear.@dropbear[0].PasswordAuth='off'
set dropbear.@dropbear[0].RootPasswordAuth='off'
commit
EOF
exit 0

Keep uci-defaults scripts small and safe to re-run. They may run again after a factory reset, and a half-applied script that keeps failing will run on every boot.

What survives a sysupgrade

By default, sysupgrade preserves the files in /etc/config and a set of other known paths. You can list exactly what will be kept with sysupgrade -l, add paths to /etc/sysupgrade.conf (one per line), and back the same set up with sysupgrade -b /tmp/backup.tar.gz.

Two consequences are easy to miss. First, uci-defaults scripts in the new image do run after an upgrade that kept settings, so they must not blindly overwrite customer configuration; check for an existing value before setting one. Second, configuration kept across upgrades may reference options a new release renamed. The move from ifname to device and DSA-based ports around OpenWrt 21.02 is the classic example. Test upgrades with real, old configuration, not just fresh installs.

Templating with variables

Once you manage more than a few devices, stop thinking in terms of individual configuration files. Think of a template that describes the intended UCI state, and a small set of variables that differ per device or site.

Template and variables render to UCI, applied to device, with drift detection loop Base template role + site layers Device variables SSID, VLAN, keys Rendered UCI versioned in git Apply uci batch Device /etc/config Drift check compare uci export vs. rendered
Templates and variables render the intended UCI state; a periodic check compares it with what the device actually has.

A few rules keep templates maintainable:

  • Layer them. Security and system settings in a base layer, then role (gateway, access point, mesh node), then site. Per-device variables last.
  • Keep variables few and typed. A VLAN ID should be validated as an integer in range before it reaches a device.
  • Use named sections. Generated anonymous sections are hard to update in place.
  • Own whole packages where you can. If the template owns firewall entirely, you can replace it atomically; if it owns half, you must merge carefully.

Idempotency

An idempotent change produces the same end state no matter how many times it runs. That matters because remote applies are retried after timeouts, and you often cannot tell whether a partial apply happened.

Practical habits: use set pkg.name=type rather than add, which creates a new anonymous section each time; delete a list before re-adding its items rather than appending; and prefer replacing a whole package from a rendered file over a long series of incremental edits. uci import can load a complete package from standard input when you want the device to match a rendered file exactly.

Drift detection

Drift is the gap between what you intended and what the device actually runs. It comes from engineers fixing things by hand, customers using LuCI, scripts that half-failed and firmware upgrades that migrated options.

The simplest detection is to export the device's configuration with uci export <package> or uci show, normalise it (sort, strip comments, exclude volatile options), and diff it against the rendered template. Run it on a schedule and after every change. Then decide per package what drift means: re-apply automatically for security-critical packages such as firewall and dropbear, and alert a human for others.

Tip: Exclude values the device legitimately generates, such as keys created on first boot or options written by services, from your diff. Otherwise every device shows permanent drift and people learn to ignore the report.

Versioning and backup

Store templates and rendered per-device configuration in version control. That gives you review before changes ship, a precise answer to "what changed last Tuesday", and a rollback target. Back up the device side too: a periodic sysupgrade -b archive, copied off the device, captures files outside UCI such as certificates and custom scripts listed in /etc/sysupgrade.conf.

Never store plaintext secrets in a shared repository. Keep keys and passwords in a secrets store and inject them at render time.

Ansible over SSH vs. agent and cloud templates

AspectAnsible over SSHAgent with cloud templates
ReachabilityNeeds an SSH path to every device at run timeDevice connects outbound; works behind NAT/CGNAT
Device requirementsPython is usually absent; use raw commands or community OpenWrt rolesSmall agent package in the image
Offline devicesSkipped; must re-run laterChange queued and applied on reconnect
Drift detectionCheck mode on demandContinuous, reported by the agent
Best fitLabs, small estates, devices on a reachable management networkDistributed fleets, customer sites, products

Many teams use both: Ansible for the lab and infrastructure, an agent and controller for deployed devices.

How CloudWRT helps

The CloudWRT Platform provides configuration templates with variable substitution, bulk operations across device groups, and scheduled configuration rollouts with rollback, delivered through an outbound connection from each device. That removes most of the scripting and reachability work described above.

For custom images, our R&D team can build OpenWrt firmware with your uci-defaults, sysupgrade rules and agent integration. See also our guides to zero-touch provisioning and fleet management, or start a free trial.

Frequently asked questions

Do I need to reboot after changing UCI configuration?

Usually not. After uci commit, run reload_config or restart the affected service. Some changes, such as certain kernel or switch settings, may still need a reboot.

Do uci-defaults scripts run after a sysupgrade?

Yes, scripts included in the new image run on its first boot, including when settings were kept. Write them to check existing values so they do not overwrite customer configuration.

Where does LuCI store its changes?

LuCI writes to the same UCI files in /etc/config, via rpcd and ubus. Changes made in LuCI therefore show up as drift if you manage configuration from templates.

Can I keep custom files across upgrades?

Yes. Add their paths to /etc/sysupgrade.conf and confirm with sysupgrade -l that they are included.