Issue changing between DHCP client and static IP on boot

Hello, I am new to openWrt so please bear with!

I have a relatively niche use case where I need a single router to have two uses - one that it is a stand alone closed offline network where devices can connect directly and the other where this router can be linked via ethernet to an upstream router.

For this I have put together a script that runs on start up that identifies if there is an ethernet connected and if so puts the router into static protocol. If there isn't it enables a DHCP client so listen to the upstrea router.

The scripts functions but when the router has been set on the static protocol I cannot access the router via as before. Could somebody shine some light as to why this is happening and what the fix would be? Script code below -


changes_made=0  # Set the flag to 0 initially (no changes made)

# Check the link status of the Ethernet interface
link_status=$(ethtool "${ETH_INTERFACE}" | grep "Link detected:" | awk '{print $3}')

if [ "${link_status}" = "yes" ]; then
  echo "Ethernet port is connected."

  dhcp_enabled=$(uci get network.lan.proto)

  if [ "${dhcp_enabled}" != "dhcp" ]; then
    echo "Set the network interface to be a DHCP client."
    uci set network.lan.proto=dhcp
    uci commit network
    echo "DHCP client is already enabled for LAN interface."
  echo "Ethernet port is not connected."

  # Check if static is already disabled
  static_enabled=$(uci get network.lan.proto)

  if [ "${static_enabled}" != "static" ]; then

    echo "Manually setting IP address for LAN interface..."

    # Remove existing DHCP settings if present
    uci delete dhcp.lan

    # Set loopback interface configuration
    uci set network.loopback=interface
    uci set network.loopback.device='lo'
    uci set network.loopback.proto='static'
    uci set network.loopback.ipaddr=''
    uci set network.loopback.netmask=''

    # Set br-lan bridge configuration
    uci set network.lan=interface
    uci set network.lan.device='br-lan'
    uci set network.lan.proto='static'
    uci set network.lan.ipaddr=''
    uci set network.lan.netmask=''
    uci set network.lan.ip6assign='60'

    # Unspecified IPv6 assignment length
    uci set network.lan.gateway=''

    # Set switch configuration
    uci set network.switch0=switch
    uci set'switch0'
    uci set network.switch0.reset='1'
    uci set network.switch0.enable_vlan='1'

    # Set switch_vlan configuration
    uci set network.switch_vlan0=switch_vlan
    uci set network.switch_vlan0.device='switch0'
    uci set network.switch_vlan0.vlan='1'
    uci set network.switch_vlan0.ports='4 6t'

    # Set global configuration
    uci set network.globals=globals
    uci set network.globals.ula_prefix='fdbb:584a:17dd::/48'

    # Commit the changes
    uci commit network
    uci commit dhcp

    echo "Configuration updated."
    echo "IP already on manual, no need to set."

# Reboot the device if changes were made
if [ $changes_made -eq 1 ]; then
  echo "Rebooting the device..."

Welcome to the community!

I moved the topic since it's just a script is simply related to network config.

What version of OpenWrt are you using and what hardware?

ubus call system board

@lleachii thank you! I wasn't aware of that.

Hardware and OpenWrt version below -

  "kernel": "5.10.176",
        "hostname": "OpenWrt",
        "system": "MediaTek MT7628AN ver:1 eco:2",
        "model": "TP-Link TL-WR902AC v3",
        "board_name": "tplink,tl-wr902ac-v3",
        "rootfs_type": "squashfs",
        "release": {
                "distribution": "OpenWrt",
                "version": "22.03.5",
                "revision": "r20134-5f15225c1e",
                "target": "ramips/mt76x8",
                "description": "OpenWrt 22.03.5 r20134-5f15225c1e"

1 Like

I suspect you might need to consider an alternate test. If you're simply checking for a link status (Layer 1/Layer 2), you'll get a positive response if a cable is plugged in. Tests based on Layer 1/Layer 2 inherently have no logic to detect Layer 3 details.

Also, your script modifies only the LAN interface, yet your description refers to an "upstream" router; "upstream" implies WAN interface.

Not that you necessarily can't get it working entirely with the LAN interface, but you should consider which interface(s) you want to use in each scenario, and tailor your script accordingly.

I'm still [re]reading the script to match it to the description of your use case - but I've noticed these things already:

Specific to this, you never describe what Interface, etc. as the SRC that you're connecting to this IP. This leads me to:

This is invalid based on this:


Have you tried instead:

/etc/init.d/network reload


/etc/init.d/network restart


@lleachii - thanks for coming back.

I have indeed tried the reload instead, the complete reboot was a new addition to see if that helped in any way.

Should the lan gateway also be

Sorry I wasn't clear there - the devices that are connecting are a range of android devices and PC's but I can't access on any devices connected. I can when it's in the DHCP client case and if i set the static protocol from the web interface but not once set to static programatically.

Can the itself...?

No indeed, it cannot.

cat /etc/config/dhcp
cat /etc/config/network

This is gonna make it more confusing.

Can you provide more details on this part of your description?

@lleachii - also to add to the last point. I get internet access.

1 Like

I'm not even sure which point you're referencing.

Without this, quite a few things remain unclear.


  • What device gets "Internet access"
  • What network it's connected to
  • :bulb: if you were claiming the gateway was valid, what's the subnet mask
  • etc.

also feel free to include:

cat /etc/config/wireless

No. The LAN interface is the gateway. You only need to define an explicit gateway if you need to send traffic from the LAN to another gateway on the same subnet (e.g. when using as a dumb AP or as an upstream router for another network).

The old adage "a picture paints a thousand words" may be relevant here. My profile has a link to an excellent tool for creating shoddy diagrams in a hurry. If you can "draw" what you're trying to achieve, that might help your audience better understand the challenges you're facing.

1 Like

Yet this current config, already:

  • Android clients get Internet
  • They can't reach - from where? (the OP's issue)
1 Like

@iplaywithtoys - thanks for the suggestion.

Below is a drawing that hopefully clears things up a little as to what is trying to be achieved.

Obviously I can't have DHCP servers on both routers as this will end up in conflicts, but need some way to allocate IP address to devices connecting to the OpenWrt router when not connected to the upstream router with internet access.

Thanks in advance

If I take your diagram literally, then you might be able to achieve your desired goal very easily:

  • Reset OpenWRT to factory defaults
  • Enable the Wi-Fi AP on OpenWRT
  • (Optionally, configure a different SSID and/or encryption for the Wi-Fi AP)
  • Plug the WAN interface of OpenWRT in to the upstream router

Now, there is one caveat: the upstream router's subnet and OpenWRT's LAN subnet must not conflict. If both subnets are then you must change one of them.

1 Like


And does the Wifi AP on OpenWRT allow connections without an internet connection being present?

How did you render that graph so quickly?



@lleachii - just the tool @iplaywithtoys suggested. Very useful I must say!


Yes, but the devices themselves might complain about "no Internet" or "no connectivity", if they can't carry out a basic "do I have Internet access?" test.

Okay. That's not a problem!

Where in the openWrt web interface do you enable the router as a Wi-Fi AP?

In Network -> Wireless

It comes pre-configured as an AP with the SSID "OpenWrt" by default, but Wi-Fi is disabled as a security measure.

1 Like

1 Like