User Tools

Site Tools


doc:uci

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
doc:uci [2012/12/07 06:57]
mypopy
doc:uci [2016/07/17 12:29] (current)
MrBW [UCI data model]
Line 1: Line 1:
 +====== The UCI System ======
  
 +The abbreviation [[doc:​techref:​uci|UCI]] ​ stands for //​**__U__**nified **__C__**onfiguration **__I__**nterface//​ and is intended to centralize the configuration of OpenWrt. ​
 +
 +Configuration should be easy and straightforward,​ making life easier! UCI is all about that. It is the successor to the NVRAM-based configuration found in the White Russian series of OpenWrt. UCI can be seen as OpenWrt'​s main configuration user interface for the most important system settings. Typically, these are settings that are crucial for the functioning of the device, such as are typically found in the web interface of routers and embedded devices; that is, functionality that is integrated in the system builds. Examples are the main network interface configuration,​ wireless settings, logging functionality and remote access configuration.
 +
 +In addition, selected third party programs have been made compatible with the UCI system, so these can be managed more easily as well. Many programs have their own configuration files lying around somewhere, like ''/​etc/​network/​interfaces'',​ ''/​etc/​exports'',​ ''/​etc/​dnsmasq.conf''​ or ''/​etc/​samba/​smb.conf''​ and they often use different syntaxes. With OpenWrt, you don't have to bother with any of them and need to change only the UCI configuration file(s). Of course, most of the software that you would like to install will not have been prepared for UCI configuration,​ which is a good thing, because oftentimes you will want the full power of an application'​s own configuration interface, as it was intended by the developers. Therefore, only a few selected programs which benefit from availability of a centralised configuration have been made UCI-compatible by the OpenWrt package maintainers (see the UCI configuration file list below). ​
 +
 +Most applications (save some that are made in-house) are made UCI-compatible by the package maintainer by simply writing the original configuration file (which is read by the program) according to the chosen settings in the corresponding UCI file. This is done upon running the initialization scripts in ''/​etc/​init.d/''​. See [[doc:​techref:​initscripts|Init scripts]] for more information. Thus, when starting a daemon with such a UCI-compatible initialization script, you should be aware that the program'​s original configuration file gets overwritten. For example, in the case of [[doc:​howto:​cifs.server|Samba/​CIFS]],​ the file ''/​etc/​samba/​smb.conf''​ is overwritten with UCI settings from the UCI configuration file ''/​etc/​config/​samba''​ when running ''/​etc/​init.d/​samba start''​. In addition, the application'​s configuration file is often stored in RAM instead of in flash, because it does not need to be stored in non-volatile memory and it is rewritten after every change, based on the UCI file. There are ways to disable UCI in case you want to adjust settings in the original configuration file not available through UCI. FIXME add the recommended way please, or link to it.\\ 
 +On [[doc:​howto:​cifs.server]] you can see how to disable UCI for samba.
 +
 +For those non-UCI compatible programs, there is a convenient [[doc:​howto:​notuci.config|list of some non-UCI configuration files]] you may want to tend to. Note that, for most third party programs, you should consult the program'​s own documentation.
 +
 +/* NOTE THIS IS NOT TRUE AND THEREFORE COMMENTED OUT. UCI IS NOTHING MORE THEN A CENTRAL PLACE FOR YOUR CONFIG. SEE THE EXAMPLE BELOW: YOU STILL HAVE TO RESTART THE uHTTPd SERVER MANUALLY. WITHOUT UCI THIS WOULD WORK THE SAME. THIS COMMENT SECTION CAN BE DELETED AFTER SOME TIME OR AFTER THE ORIGINAL AUTHOR HAS READ IT: You no longer have to reboot your system to make configuration changes. You can use the [[doc:​uci#​command.line.utility|UCI command line utility]] instead. AGAIN, UCI COMMAND LINE UTILITY HAS NOTHING TO DO WITH THE ABILITY TO COMMIT CHANGES WITHOUT REBOOT */
 +
 +
 +
 +
 +===== Common Principles =====
 +OpenWrt'​s central configuration is split into several files located in the ''/​etc/​config/''​ directory. Each file relates roughly to the part of the system it configures. You can edit the configuration files with a text editor or modify them with the command line utility program ''​uci''​. UCI configuration files are also modifiable through various programming APIs (like Shell, Lua and C), which is also how web interfaces like [[doc:​howto:​luci.essentials‎|LuCI]] make changes to the UCI files. ​
 +
 +Upon changing a UCI configuration file, whether through a text editor or the command line, the services or executables that are affected must be (re)started (or, in some cases, simply reloaded) by an [[doc/​techref/​initscripts|init.d call]], such that the updated UCI configuration is applied to them. Many programs are made compatible with UCI in this way by making their init.d script write their standard software specific configuration files. The init.d script first properly writes such a configuration file to the software'​s expected location and it is read in again by restarting the executable. Note that just (re)starting the executable directly, without init.d calls, will not result in an UCI update to relegate UCI config to the program'​s expected [[http://​en.wikipedia.org/​wiki/​Configuration_file|configuration file]]. Changes in files in ''/​etc/​config/''​ then take no effect. ​
 +
 +As an example of modifying the UCI configuration,​ suppose you want to change the device'​s IP address from the default ''​192.168.1.1''​ to ''​192.168.2.1''​. To do this, using any text editor, such as vi, change the line
 +
 +''​option ipaddr 192.168.1.1''​
 +
 +in the file ''/​etc/​config/​network''​ to: 
 +
 +''​option ipaddr 192.168.2.1''​
 +
 +Next, commit the settings by running
 +
 +''/​etc/​init.d/​network restart''​
 +
 +In this case, remember that you have to login again using SSH as the device is now accessible at its new IP address!
 +
 +===== Configuration Files =====
 +
 +^ File ^ Description ^
 +^ Basic ^^
 +| [[doc:​uci:​dhcp|/​etc/​config/​dhcp]] | Dnsmasq configuration and DHCP settings |
 +| [[doc:​uci:​dropbear|/​etc/​config/​dropbear]] | SSH server options |
 +| [[doc:​uci:​firewall|/​etc/​config/​firewall]] | NAT, packet filter, port forwarding, etc.  |
 +| [[doc:​uci:​network|/​etc/​config/​network]] | Switch, interface and route configuration |
 +| [[doc:​uci:​system|/​etc/​config/​system]] | Misc. system settings |
 +| [[doc:​uci:​wireless|/​etc/​config/​wireless]] | Wireless settings and wifi network definition |
 +^ IPv6 ^^
 +| [[doc:​uci:​ahcpd|/​etc/​config/​ahcpd]] | Ad-Hoc Configuration Protocol (AHCP) server and forwarder configuration |
 +| [[doc:​uci:​aiccu|/​etc/​config/​aiccu]] | AICCU client configuration |
 +| [[doc:​uci:​dhcp6c|/​etc/​config/​dhcp6c]] | WIDE-DHCPv6 client |
 +| [[doc:​uci:​dhcp6s|/​etc/​config/​dhcp6s]] | WIDE-DHCPv6 server |
 +| [[doc:​uci:​gw6c|/​etc/​config/​gw6c]] | GW6c client configuration |
 +| [[doc:​uci:​radvd|/​etc/​config/​radvd]] | Router Advertisement (radvd) configuration |
 +^ Other ^^
 +| [[doc:​uci:​babeld|/​etc/​config/​babeld]] | babeld configuration |
 +| [[doc:​uci:​bbstored|/​etc/​config/​bbstored]] | BoxBackup server configuration |
 +| [[doc:​uci:​ddns|/​etc/​config/​ddns]] | Dynamic DNS configuration (ddns-scripts) |
 +| [[doc:​uci:​etherwake|/​etc/​config/​etherwake]] | Wake-on-Lan:​ etherwake |
 +| [[doc:​uci:​freifunk_p2pblock|/​etc/​config/​freifunk_p2pblock]] | Uses iptables layer7-, ipp2p- and recent-modules to block p2p/​filesharing traffic |
 +| [[doc:​uci:​fstab|/​etc/​config/​fstab]] | Mount points and swap |
 +| [[doc:​uci:​hd-idle|/​etc/​config/​hd-idle]] | Another idle-daemon for attached hard drives |
 +| [[doc:​uci:​httpd|/​etc/​config/​httpd]] | Web server options (Busybox httpd, deprecated) |
 +| [[doc:​uci:​ipset-dns|/​etc/​config/​ipset-dns]] | Configure [[http://​git.zx2c4.com/​ipset-dns/​about/​ ipset-dns]] |
 +| [[doc:​uci:​luci|/​etc/​config/​luci]] | Base LuCI config |
 +| [[doc:​uci:​luci_statistics|/​etc/​config/​luci_statistics]] | Configuration of Statistics packet|
 +| [[doc:​uci:​mini_snmpd|/​etc/​config/​mini_snmpd]] | mini_snmpd settings |
 +| [[doc:​uci:​minidlna|/​etc/​config/​minidlna]] | MiniDLNA ​ settings |
 +| [[doc:​uci:​mjpg-streamer|/​etc/​config/​mjpg-streamer]] | Streaming application for Linux-UVC compatible webcams ​ |
 +| [[doc:​uci:​mountd|/​etc/​config/​mountd]] | OpenWrt automount daemon |
 +| [[doc:​uci:​mroute|/​etc/​config/​mroute]] | Configuration files for multiple WAN routes |
 +| [[doc:​uci:​multiwan|/​etc/​config/​multiwan]] | Simple multi WAN configuration |
 +| [[doc:​uci:​mwan3|/​etc/​config/​mwan3]] | Multi-WAN config with load balancing and failover |
 +| [[doc:​uci:​nodogsplash|nodogsplash Configuration]] | nodogsplash configuration |
 +| [[doc:​uci:​ntpclient|/​etc/​config/​ntpclient]] | Getting the correct time |
 +| [[doc:​uci:​p910nd|/​etc/​config/​p910nd]] | config for non-spooling Printer daemon [[doc:​howto:​p910nd.server]] |
 +| [[doc:​uci:​pure-ftpd|/​etc/​config/​pure-ftpd]] | Pure-FTPd server config |
 +| [[doc:​uci:​qos|/​etc/​config/​qos]] | Implementing Quality of Service for the //upload// |
 +| [[doc:​uci:​racoon|/​etc/​config/​racoon]] | racoon IPsec daemon |
 +| [[doc:​uci:​samba|/​etc/​config/​samba]] | settings for the Microsoft file and print services daemon |
 +| [[doc:​uci:​snmpd|/​etc/​config/​snmpd]] | SNMPd settings |
 +| [[doc:​uci:​sqm|/​etc/​config/​sqm]] | SQM settings |
 +| [[doc:​uci:​sshtunnel|/​etc/​config/​sshtunnel]] | Settings for the package ''​sshtunnel''​ |
 +| [[doc:​uci:​stund|/​etc/​config/​stund]] | STUN server configuration |
 +| [[doc:​uci:​tinc|/​etc/​config/​tinc]] | [[doc/​howto/​vpn.tinc|tinc]] package configuration |
 +| [[doc:​uci:​transmission|/​etc/​config/​transmission]] | BitTorrent configuration |
 +| [[doc:​uci:​uhttpd|/​etc/​config/​uhttpd]] | Web server options (uHTTPd) |
 +| [[doc:​uci:​miniupnpd|/​etc/​config/​upnpd]] | miniupnpd UPnP server settings |
 +| [[doc:​uci:​users|/​etc/​config/​users]] | user database for different services |
 +| [[doc:​uci:​ushare|/​etc/​config/​ushare]] | uShare UPnP server settings |
 +| [[doc:​uci:​vblade|/​etc/​config/​vblade]] | vblade userspace AOE target |
 +| [[doc:​uci:​vnstat|/​etc/​config/​vnstat]] | vnstat downloader settings |
 +| [[doc:​uci:​wifitoggle|/​etc/​config/​wifitoggle]] | Script to toogle WiFi with a button |
 +| [[doc:​uci:​wol|/​etc/​config/​wol]] | Wake-on-Lan:​ wol |
 +| [[doc:​uci:​wshaper|/​etc/​config/​wshaper]] | wondershaper qos script settings |
 +| [[doc:​uci:​znc|/​etc/​config/​znc]] | ZNC bouncer configuration |
 +
 +===== File Syntax =====
 +
 +The UCI configuration files usually consist of one or more ''​config''​ statements, so called sections with one or more option statements defining the actual values.
 +
 +Below is an example of a simple configuration file:
 +(also see [[https://​wiki.openwrt.org/​doc/​uci#​uci_data_model|uci_data_model]])
 +<​code>​package '​example'​
 +
 +config '​example'​ '​test'​
 +        option ​  '​string' ​     'some value'
 +        option ​  '​boolean' ​    '​1'​
 +        list     '​collection' ​ 'first item'
 +        list     '​collection' ​ '​second item'</​code>​
 +
 +  * The ''​config '​example'​ '​test'​ ''​ statement defines the start of a section with the type ''​example''​ and the name ''​test''​. There can also be so called anonymous sections with only a type, but no name identifier. The type is important for the processing programs to decide how to treat the enclosed options.
 +
 +  * The ''​option '​string'​ 'some value' ''​ and ''​ option '​boolean'​ '​1'​ ''​ lines define simple values within the section. Note that there are no syntactical differences between text and boolean options. Per convention, boolean options may have one of the values '​0',​ '​no',​ '​off',​ '​false'​ or '​disabled'​ to specify a false value or '​1'​ , '​yes',​ '​on',​ '​true'​ or '​enabled'​ to specify a true value.
 +
 +  * In the lines starting with a ''​list''​ keyword an option with multiple values is defined. All ''​list''​ statements that share the same name, ''​collection''​ in our example, will be combined into a single list of values with the same order as in the configuration file.
 +
 +  * The indentation of the ''​option''​ and ''​list''​ statements is a convention to improve the readability of the configuration file but it's not syntactically required.
 +
 +  * If an option is absent and not required, the default value is assumed. If it is absent and required, it may trigger an error in the application or other unwanted behaviour.
 +  ​
 +  * A way to disable a config section, that does not have a ''​enabled''​ option to be disabled, is renaming the config section identifier (or type, in this case ''​example''​) to a value not recognized by the processes that uses those values. Normally a ''​disabled_identifier''​ as config section type/​identifier is sufficient.
 +
 +Usually you do not need to enclose identifiers or values in quotes. Quotes are only required if the enclosed value contains spaces or tabs. Also it's legal to use double- instead of single-quotes when typing configuration options.
 +
 +All of the examples below are valid UCI syntax:
 +
 +  * ''​option example value''​
 +  * ''​option '​example'​ value''​
 +  * ''​option example "​value"''​
 +  * ''​option "​example" ​   '​value'​ ''​
 +  * ''​option ​  '​example'​ "​value"''​
 +
 +In contrast, the following examples are not valid UCI syntax:
 +
 +  * ''​option '​example"​ "​value'​ ''​ (quotes are unbalanced)
 +  * ''​option example some value with space ''​ (note the missing quotes around the value)
 +
 +It is important to know that UCI identifiers and config file names may contain only the characters ''​a-z'',​ ''​0-9''​ and ''​_''​. E.g. no hyphens (-) are allowed. Option values may contain any character (as long they are properly quoted).
 +
 +====== Editor plugins ======
 +
 +Syntax highlighting and (slightly) more in vim: [[https://​github.com/​cmcaine/​vim-uci |vim-uci]] - works well with sshfs (need openssh-sftp-server).
 +===== Command Line Utility =====
 +
 +For adjusting settings, one normally changes the UCI config files directly. However, for scripting purposes, all of UCI configuration can also be read and changed using the ''​uci''​ command line utility. For developers requiring automatic parsing of the UCI configuration,​ it is therefore redundant, unwise, and inefficient to use ''​awk''​ and ''​grep''​ to parse OpenWrt'​s config files. The ''​uci''​ utility offers all functionality with respect to modifying and parsing UCI. 
 +
 +Below is the usage, as well as some useful examples of how to use this powerful utility. ​
 +
 +|{{:​meta:​icons:​tango:​48px-dialog-warning.svg.png?​nolink&​24}} When using ''​uci''​ to write configuration files, the files are always rewritten in whole and non-recognised commands are omitted. This means that any extraneous lines in the file are deleted, such as comments. If you have UCI configuration files that you have edited yourself and you want to preserve your own comments and blank lines, you should not use the command line utility but edit the files normally. Note that some files, such as the uHTTPd configuration file, already contain many comments when the application is first installed. Also, note that some applications such as [[doc/​techref/​luci|LuCI]] also use the ''​uci''​ utility and thus may rewrite UCI configuration files.|
 +
 +/* Remember, friends don't let friends parse their own config files! ​ << I fail to see the point ... */
 +
 +
 +When there are multiple rules next to each other, UCI supports array-like references for them.  If there are 8 NTP servers defined in ''/​etc/​config/​system'',​ UCI will let you reference their sections as ''​system.@timeserver[0]''​ for the first or ''​system.@timeserver[7]''​ for the last one.  You can also use negative indexes, such as ''​system.@timeserver[-1]''​. ​ "​-1"​ means the last one, "​-2"​ means the second-to-last one, and so on.  This comes in very handy when appending new rules to the end of a list.  See the examples below.
 +
 +==== Usage ====
 +''​root@OpenWrt:/​lib/​config#​ **uci**''​
 +
 +<​code>​
 +Usage: uci [<​options>​] <​command>​ [<​arguments>​]
 +
 +Commands:
 + batch
 + export ​    ​[<​config>​]
 + import ​    ​[<​config>​]
 + changes ​   [<​config>​]
 + commit ​    ​[<​config>​]
 + add        <​config>​ <​section-type>​
 + add_list ​  <​config>​.<​section>​.<​option>​=<​string>​
 + del_list ​  <​config>​.<​section>​.<​option>​=<​string>​
 + show       ​[<​config>​[.<​section>​[.<​option>​]]]
 + get        <​config>​.<​section>​[.<​option>​]
 + set        <​config>​.<​section>​[.<​option>​]=<​value>​
 + delete ​    <​config>​[.<​section[.<​option>​]]
 + rename ​    <​config>​.<​section>​[.<​option>​]=<​name>​
 + revert ​    <​config>​[.<​section>​[.<​option>​]]
 + reorder ​   <​config>​.<​section>​=<​position>​
 +
 +Options:
 + -c <​path> ​ set the search path for config files (default: /​etc/​config)
 + -d <​str> ​  set the delimiter for list values in uci show
 + -f <​file> ​ use <​file>​ as input instead of stdin
 + -m         when importing, merge data into an existing package
 + -n         name unnamed sections on export (default)
 + -N         ​don'​t name unnamed sections
 + -p <​path> ​ add a search path for config change files
 + -P <​path> ​ add a search path for config change files and use as default
 + -q         quiet mode (don't print error messages)
 + -s         force strict mode (stop on parser errors, default)
 + -S         ​disable strict mode
 + -X         do not use extended syntax on '​show'​
 +</​code>​
 +
 +^ ''​Command''​ ^ Target ^ Description ​ ^
 +| ''​commit'' ​  | ''​[<​config>​]''​ | Writes changes of the given configuration file, or if none is given, all configuration files, to the filesystem. All "uci set", "uci add", "uci rename"​ and "uci delete"​ commands are staged into a temporary location and written to flash at once with "uci commit"​. This is not needed after editing configuration files with a text editor, but for scripts, GUIs and other programs working directly with UCI files. |
 +| ''​batch'' ​   | - | Executes a multi-line UCI script which is typically wrapped into a //here// document syntax. |
 +| ''​export'' ​  | ''​[<​config>​]''​ | Exports the configuration in a machine readable format. It is used internally to evaluate configuration files as shell scripts. |
 +| ''​import'' ​  | ''​[<​config>​]''​ | Imports configuration files in UCI syntax. |
 +| ''​changes'' ​ | ''​[<​config>​]''​ | List staged changes to the given configuration file or if none given, all configuration files. |
 +| ''​add'' ​     | ''<​config> ​ <​section-type>''​ | Add an anonymous section of type ''​section-type''​ to the given configuration. |
 +| ''​add_list''​ | ''<​config>​.<​section>​.<​option>​=<​string>''​ | Add the given string to an existing list option. |
 +| ''​del_list''​ | ''<​config>​.<​section>​.<​option>​=<​string>''​ | Remove the given string from an existing list option. |
 +| ''​show'' ​    | ''​[<​config>​[.<​section>​[.<​option>​]]]''​ | Show the given option, section or configuration in compressed notation. |
 +| ''​get'' ​     | ''<​config>​.<​section>​[.<​option>​]''​ | Get the value of the given option or the type of the given section. |
 +| ''​set'' ​     | ''<​config>​.<​section>​[.<​option>​]=<​value>''​ | Set the value of the given option, or add a new section with the type set to the given value. |
 +| ''​delete'' ​  | ''<​config>​[.<​section[.<​option>​]]'' ​ | Delete the given section or option. |
 +| ''​rename'' ​  | ''<​config>​.<​section>​[.<​option>​]=<​name>''​ | Rename the given option or section to the given name. |
 +| ''​revert'' ​  | ''<​config>​[.<​section>​[.<​option>​]]''​ | Revert the given option, section or configuration file. |
 +| ''​reorder'' ​  | ''<​config>​.<​section>​=<​position>''​ | todo |
 +
 +===== UCI data/​object ​ model =====
 +=== Elements ====
 +The elements in UCI model are:
 +  * **config**: main configuration groups like **network**,​ **system**, **firewall**. Each configuration group has it's own file in **/​etc/​config**
 +  * **sections**:​ config is divided into sections. A section can either be **named** or **unnamed**.
 +  * **types**: a section can have a type. E.g in the the network config we typically have 4 sections of the type "​interface"​. The sections are "​lan",​ "​wan",​ "​loopback"​ and "​wan6"​
 +  * **options**:​ each section have some options where you set your configuration values
 +  * **values**: value of option
 +
 +=== Sections naming ===
 +Sections deserve some extra explanation in regards to naming.
 +A section can be //named// or //​unnamed//​. ​
 +Unnamed sections will get an **autogenerated ID/CFGID** (like "​cfg073777"​) and be presented with an **anonymous-name** (like "​@switch[0]"​)
 +
 +Example of **anonymous-name** (cmd: uci show network):
 +<​code>​
 +...
 +network.@switch[0]=switch
 +network.@switch[0].name='​switch0'​
 +network.@switch[0].reset='​1'​
 +network.@switch[0].enable_vlan='​1'​
 +...
 +</​code>​
 +
 +Example of **autogenerated ID/CFGID** (cmd: "uci show network.@switch[0]):​
 +<​code>​
 +network.cfg073777=switch
 +network.cfg073777.name='​switch0'​
 +network.cfg073777.reset='​1'​
 +network.cfg073777.enable_vlan='​1'​
 +</​code>​
 +
 +=== Different presentation ===
 +The same config section can be presented in different ways:
 +
 +  * Human-friendly:​ as presented in the config files or with the command "uci export <​config>"​
 +  * Programmable:​ as presented with the command "uci show <​config>​.."​
 +
 +^ Different model presentations ^^^ 
 +| **Human-friendly**,​ named section ("uci show network"​) | **Human-friendly**,​ unnamed section ("uci show network"​) |  |
 +| {{:​media:​doc:​howtos:​uci_hr_named.png?​nolink|}} | {{:​media:​doc:​howtos:​uci_hr_unmaned.png?​nolink|}} |  |
 +| **Programmable**,​ named section ("uci show network.wan"​) | **Programmable**,​ unnamed section, anonoumys name ("uci show network"​) | **Programmable**,​ unnamed section, CFGID ("uci show network.@switch[0]"​) |
 +| {{:​media:​doc:​howtos:​uci_prg_named.png?​nolink|}} | {{:​media:​doc:​howtos:​uci_prg_unmaned_an.png?​nolink|}} | {{:​media:​doc:​howtos:​uci_prg_unmaned_cfgid.png?​nolink|}} ​ |
 +==== Examples: ====
 +
 +=== Setting a value ===
 +
 +If we want to change the listening port of the [[doc:​howto:​http.uhttpd|uHTTPd Web Server]] from 80 to 8080 we change the configuration in ''/​etc/​config/​uhttpd''​ :
 +
 +| ''​root@OpenWrt:​~#​ **uci set uhttpd.main.listen_http=8080**
 +root@OpenWrt:​~#​ **uci commit uhttpd**
 +root@OpenWrt:​~#​ **/​etc/​init.d/​uhttpd restart**
 +root@OpenWrt:​~#''​ |
 +
 +Done, now the configuration file is updated and uHTTPd listens on port 8080.
 +
 +=== Export an entire configuration ===
 +
 +| ''​root@OpenWrt:​~#​ **uci export httpd**
 +package '​httpd'​
 +
 +config '​httpd'​
 + option '​port'​ '​80'​
 + option '​home'​ '/​www'​
 +
 +root@OpenWrt:​~#''​ |
 +
 +=== Show the configuration "​tree"​ for a given config ===
 +
 +| ''​root@OpenWrt:​~#​ **uci show httpd**
 +httpd.@httpd[0]=httpd
 +httpd.@httpd[0].port=80
 +httpd.@httpd[0].home=/​www
 +root@OpenWrt:​~#''​ |
 +
 +=== Display just the value of an option ===
 +
 +| ''​root@OpenWrt:​~#​ **uci get httpd.@httpd[0].port**
 +80
 +root@OpenWrt:​~#''​ |
 +
 +=== append an entry to a list ===
 +
 +| ''​uci add_list system.ntp.server='​0.de.pool.ntp.org'''​ |
 +
 +=== replace a list completely ===
 +
 +| ''​uci delete system.ntp.server
 +uci add_list system.ntp.server='​0.de.pool.ntp.org'​
 +uci add_list system.ntp.server='​1.de.pool.ntp.org'​
 +uci add_list system.ntp.server='​2.de.pool.ntp.org'​
 +''​ |
 +
 +=== Create a new named section of a given type ===
 +
 +"uci add blah" will create a new _anonymous_ section of type blah. ie, 
 +
 +| ''#​touch /​etc/​config/​playapp
 +#uci show playapp
 +#uci add playapp blah
 +cfg02a4fc
 +#uci show playapp
 +playapp.@blah[0]=blah
 +# uci commit && cat /​etc/​config/​playapp ​
 +
 +config blah
 +
 +#
 +''​|
 +
 +If you actually want a named section of that type, for instance, ​
 +
 +|''​config blah this_name
 +    option xxx yyy
 +config blah other_name
 +    option xxx zzz
 +''​|
 +
 +Then "uci add" cannot be used, instead, use this syntax
 +
 +|''#​rm /​etc/​config/​playapp && touch /​etc/​config/​playapp
 +# uci set playapp.myname=mysectiontype
 +# uci set playapp.othername=mysectiontype
 +# uci commit && uci show playapp
 +playapp.myname=mysectiontype
 +playapp.othername=mysectiontype
 +# cat /​etc/​config/​playapp ​
 +
 +config mysectiontype '​myname'​
 +
 +config mysectiontype '​othername'​
 +
 +''​|
 +
 +
 +=== UCI paths ===
 +
 +Consider this example config file:
 +
 +|''#​ /​etc/​config/​foo
 +config bar '​first'​
 + option name '​Mr. First'
 +config bar
 + option name '​Mr. Second'​
 +config bar '​third'​
 + option name '​Mr. Third'
 +''​|
 +
 +Then the paths below are equal in every group:
 +
 +|''#​ Mr. First
 +uci get foo.@bar[0].name
 +uci get foo.@bar[-0].name
 +uci get foo.@bar[-3].name
 +uci get foo.first.name
 +
 +# Mr. Second
 +uci get foo.@bar[1].name
 +uci get foo.@bar[-2].name
 +# uci get foo.second.name won't work; label '​second'​ undefined.
 +
 +# Mr. Third
 +uci get foo.@bar[2].name
 +uci get foo.@bar[-1].name
 +uci get foo.third.name
 +''​|
 +
 +If you show it, you get :
 +
 +|''#​ **uci show foo**
 +foo.first=bar
 +foo.first.name=Mr. First
 +//​foo.@bar[0]=bar//​
 +//​foo.@bar[0].name=Mr. Second//
 +foo.third=bar
 +foo.third.name=Mr. Third
 +''​|
 +
 +But if you used "uci show foo.@bar[0]",​ you will see:
 +
 +|''#​ **uci show foo.@bar[0]**
 +//​foo.first=bar//​
 +//​foo.first.name=Mr. First// :-(
 +''​|
 +
 +
 +=== Query interface state ===
 +
 +| ''​root@OpenWrt:​~#​ **uci -P/​var/​state show network.wan**
 +network.wan=interface
 +network.wan.ifname=eth0.1
 +network.wan.proto=dhcp
 +network.wan.defaultroute=0
 +network.wan.peerdns=0
 +network.wan.device=eth0.1
 +network.wan.ipaddr=10.11.12.13
 +network.wan.broadcast=255.255.255.255
 +network.wan.netmask=255.255.255.0
 +network.wan.gateway=10.11.12.1
 +network.wan.dnsdomain=
 +network.wan.dns=10.11.12.100 10.11.12.200
 +network.wan.up=1
 +network.wan.lease_gateway=10.11.12.1
 +network.wan.lease_server=10.11.12.25
 +network.wan.lease_acquired=1262482940
 +network.wan.lease_lifetime=5400
 +network.wan.lease_hostname=x-10-11-12-13
 +root@OpenWrt:​~#''​ |
 +
 +
 +=== Add a firewall rule ===
 +
 +This is a good example of both adding a firewall rule to forward the TCP SSH port, and of the negative (-1) syntax used with uci.
 +
 +| ''​root@OpenWrt:​~#​ uci add firewall rule
 +root@OpenWrt:​~#​ uci set firewall.@rule[-1].src=wan
 +root@OpenWrt:​~#​ uci set firewall.@rule[-1].target=ACCEPT
 +root@OpenWrt:​~#​ uci set firewall.@rule[-1].proto=tcp
 +root@OpenWrt:​~#​ uci set firewall.@rule[-1].dest_port=22
 +root@OpenWrt:​~#​ uci commit firewall
 +root@OpenWrt:​~#​ /​etc/​init.d/​firewall restart''​ |
 +
 +=== Get WAN IP address ===
 +  * Backfire <code bash>uci -P/​var/​state get network.wan.ipaddr</​code>​
 +  * The uci state vars are deprecated and not used anymore for network related information. In Trunk (not really uci) do <code bash>. /​lib/​functions/​network.sh;​ network_get_ipaddr ip wan; echo $ip</​code>​
 +
 +=== Get WAN interface ===
 +  * Backfire <code bash>uci -P/​var/​state get network.wan.ifname</​code>​
 +  * The uci state vars are deprecated and not used anymore for network related information. In Trunk (not really uci) do <code bash>. /​lib/​functions/​network.sh;​ network_get_device if_wan wan; echo $if_wan</​code>​
 +  * Or <code bash>. /​lib/​functions/​network.sh;​ network_get_physdev if_wan wan; echo $if_wan</​code>​
 +
 +=== Get SSID ===
 +<​code>​ uci get wireless.@wifi-iface[-1].ssid</​code>​
 +
 +==== uci-defaults ====
 +
 +To set some system defaults the first time the device boots, create a script in the folder ''/​etc/​uci-defaults/''​.
 +
 +All scripts in that folder are automatically executed by ''/​etc/​init.d/​boot''​ and if they exited with code 0 __deleted afterwards__ (scripts that did not exit with code 0 are not deleted and will be re-executed during the next boot until they also successfully exit).
 +
 +In a live router you can see the existing uci-defaults scripts in ''/​rom/​etc/​uci-defaults''​ , as ''/​etc/​uci-defaults''​ itself is typically empty (after all scripts have been run ok and have been deleted).
 +
 +This is a simple example of changing the default ip at first boot.
 +
 +<​code>#​!/​bin/​sh
 +uci set network.lan.ipaddr=192.168.178.1
 +uci commit network
 +exit 0</​code>​
 +
 +This is a simple example of changing the default SSID and also turning WIFI on at first boot.
 +
 +<​code>#​!/​bin/​sh
 +uci set wireless.@wifi-device[0].disabled=0
 +uci set wireless.@wifi-iface[0].ssid=OpenWrt0815
 +uci commit wireless
 +exit 0</​code>​
 +
 +Easiest way to include uci-defaults scripts in your firmware may be as custom files.
 +See [[doc:​howto:​build#​custom_files|Buildroot - Custom files]] or [[doc:​howto:​obtain.firmware.generate#​files_variable|SDK - Custom files]]
 +
 +===== Porting UCI to a different Linux distribution =====
 + See [[doc:​techref:​uci#​usage_outside_of_openwrt|UCI (Unified Configuration Interface) – Technical Reference]]