OpenWrt Forum Archive

Topic: UCI network config

The content of this topic has been archived on 2 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello,

    I would like to change my lan settings via "UCI" back and forth in between "static" and "dhcp".

    When I use the command below, is it OK or enough ?

    uci set network.interface.proto=static

    uci set network.interface.proto=dhcp


config 'interface' 'example'
        option 'proto'     'static'
        option 'ifname'    'eth0'
        option 'ipaddr'    '192.168.1.200'
        option 'netmask'   '255.255.255.0'
        option 'dns'       '192.168.1.1'

Is the config below works or not ?

config 'interface' 'example'
        option 'proto'     'dhcp'
        option 'ifname'    'eth0'
        option 'ipaddr'    '192.168.1.200'
        option 'netmask'   '255.255.255.0'
        option 'dns'       '192.168.1.1'

(Last edited by enkavak on 22 Jun 2017, 17:53)

This is not enough; you will also need to commit the changes and then reload the network service.

From a configuration perspective, it is usually a better idea to write the full config when switching between protocols. The rationale behind this is that the config should always be 'complete', in a sense.

When the protocol is 'dhcp', options such as 'ipaddr' or 'netmask' are not necessary, and may cause a validation error. A validation error, on the other hand, will prevent these settings from being applied to netifd.

As per your example, the following commands will delete an existing 'example' configuration (if there is one), create a new static IP interface configuration for 'eth0' device, commit the changes to UCI, and reload the network service to apply the settings:

uci delete network.example
uci set network.example='interface'
uci set network.example.proto='static'
uci set network.example.ifname='eth0'
uci set network.example.ipaddr='192.168.1.200'
uci set network.example.netmask='255.255.255.0'
uci set network.example.dns='192.168.1.1'
uci commit network
/etc/init.d/network reload

The following commands, on the other hand, will do the same steps, but will use DHCP instead of a static IP configuration:

uci delete network.example
uci set network.example='interface'
uci set network.example.proto='dhcp'
uci set network.example.ifname='eth0'
uci commit network
/etc/init.d/network reload

EDIT: It is also possible to write two separate configurations, 'example_static' and 'example_dhcp' which both target the 'eth0' network interface, and then use the 'enabled' option to toggle between the two.

First we setup the configurations:

uci delete network.example_dhcp
uci delete network.example_static

uci set network.example_dhcp='interface'
uci set network.example_dhcp.proto='dhcp'
uci set network.example_dhcp.ifname='eth0'
uci set network.example_dhcp.enabled='0'

uci set network.example_static='interface'
uci set network.example_static.proto='static'
uci set network.example_static.ifname='eth0'
uci set network.example_static.ipaddr='192.168.1.200'
uci set network.example_static.netmask='255.255.255.0'
uci set network.example_static.dns='192.168.1.1'
uci set network.example_static.enabled='0'

uci commit network

After the setup, you can use the following command sequence to selectively toggle between the configurations:

uci set network.example_dhcp.enabled='0'
uci set network.example_static.enabled='1'
uci commit network
/etc/init.d/network reload

To use the DHCP configuration, just toggle the 'enabled' bits the other way around.

(Last edited by Antek on 23 Jun 2017, 12:42)

The discussion might have continued from here.