How to set up automatic WAN failover on OpenWrt
Learn how to implement internet redundancy and automatic failover on OpenWrt routers using gateway monitoring scripts and traffic balancing.
Summary
- Automatic failover redirects network traffic to a secondary connection when the primary link drops.
- Gateway monitoring scripts validate real connectivity by periodically pinging external public addresses.
- OpenWrt manages virtual network interfaces and static routing to switch links without manual intervention.
- Redundancy systems on consumer routers require rigorous testing of latency and packet loss thresholds.
- Reliable failover setups complement the stability achieved when you set up WAN link failover on pfSense step by step for larger environments.
Why your internet drops and how failover fixes it
Losing internet connectivity in the middle of an important task is one of the most frustrating digital disruptions. When an internet service provider suffers a technical outage, standard home routers simply lose access and leave us offline until service is restored. In practice, this means video calls drop, downloads halt, and smart devices lose remote accessibility.
To eliminate this single point of failure, we can use a strategy called automatic failover, which keeps two or more active internet connections running simultaneously on the same router. As long as the primary connection operates normally, all traffic flows through it. However, if the system detects that the primary line has failed, it instantly redirects data flow to the secondary connection, such as a 4G mobile network or a fiber link from another provider.
Implementing this redundancy in enterprise systems typically requires dedicated hardware, but it is entirely feasible in residential and small office environments using OpenWrt, an alternative Linux-based operating system for routers. Before diving in, remember that maintaining multiple links requires attention to the power consumption of modems and power strips, much like the care we take when we measure real PC energy consumption with a power meter to avoid electrical overloads.
Preparing the ground and connecting WAN interfaces
The first step to configure failover on OpenWrt is ensuring your router has sufficient physical ports or supports USB adapters to receive more than one internet link. Each incoming internet connection is treated as a WAN interface, meaning Wide Area Network, the gateway to the global internet. Connect the main modem to the standard WAN port and the backup connection to a converted LAN port or an USB Ethernet adapter.
In the OpenWrt administration panel, navigate to the network tab and confirm that both interfaces are receiving valid IP addresses from their respective modems. Name the primary connection as wan and the secondary as wan2. For the router to know which path to follow, we need to define routing metrics, which act as traffic signs indicating which route takes precedence over the other.
The routing metric defines a numerical priority where the lowest number wins. We assign metric 10 to the primary wan interface and metric 20 to the backup wan2 interface. Thus, the Linux network core will always choose the primary connection by default, falling back to the secondary only if the first stops advertising valid routes to the internet.
Creating the gateway monitoring script
Merely changing route metrics is not enough to detect slow drops or instability where the cable remains connected, but the provider has lost internet access. To solve this, we create a gateway monitoring script, which is a small background program that runs connectivity tests known as pings to reliable public addresses, such as Google or Cloudflare DNS servers.
If the script fails to reach these addresses through the primary interface for a predetermined number of consecutive attempts, it assumes the link has dropped and executes reconfiguration commands to remove the faulty route. In practice, the router pretends the primary interface went down, forcing traffic to migrate cleanly to the secondary link without requiring a device reboot.
We can implement this logic by creating a script file in the /etc/init.d/ directory of OpenWrt. The script uses basic Linux shell commands to check interface statuses and record event logs, allowing the administrator to pinpoint exactly when and why the router decided to switch between internet connections.
Configuring traffic balancing and failover policies carefully ensures that temporary packet loss does not trigger unnecessary switches, preserving overall connection stability during minor network jitters.
Configuring load balancing and failover step by step
To apply the configuration practically and securely, we will use the official mwan3 package, OpenWrt's standard tool for managing multiple WAN interfaces, supporting both failover and weighted load balancing. Follow the procedure below directly in the router terminal via SSH:
- Update the list of available packages in your OpenWrt software repository using the package synchronization command.
- Install the multiple WAN management package and its dependencies using the integrated opkg package manager.
- Configure traffic policy rules by editing the configuration file located in the system services directory.
opkg update && opkg install mwan3 luci-app-mwan3After installation, the package will automatically create tracking instances for your interfaces. It is crucial to adjust monitoring targets to use stable and distinct IP addresses, preventing false positives caused by the temporary failure of a single test server on the internet.
Validating redundancy and testing network resilience
With the system configured and active, the final test consists of simulating a real connection outage to verify failover effectiveness. Physically disconnect the primary internet cable and observe the behavior of your devices connected to the internal network. The transition time between connections should take just a few seconds, enough for the router to drop old routes and assume the backup link.
You can monitor the process in real time by accessing the OpenWrt graphical interface in the multi-WAN management section or checking system logs via the logread -f command in the SSH terminal. This monitoring ensures that the transition occurred smoothly and that the return to the primary link will happen automatically as soon as the first provider normalizes services.
Final considerations on network stability
Maintaining a resilient network infrastructure in home or professional environments drastically increases the reliability of services we use daily. Although OpenWrt configuration requires patience and basic IP networking knowledge, the result is a robust system capable of mitigating unexpected ISP outages without expensive commercial equipment costs.
By combining active gateway monitoring with clear routing policies and metrics, we transform an ordinary router into a high-availability concentrator. This same preventive engineering mindset can be applied across other technical fronts, ensuring our systems remain operational and ready for any unforeseen events.