Change Default Gateway Linux: Fast Fix
In this article
To change your default gateway on Linux and get back online fast, you first remove the old route and then add the new one with the ip command. The quickest way is: sudo ip route replace default via <NEW_GATEWAY_IP>. This guide walks you through the full process, including making the change permanent and fixing common issues.
What You'll Need
- A Linux system with sudo or root access.
- The IP address of the new gateway (usually your router's LAN IP, like 192.168.1.1).
- Your network interface name (e.g., eth0, wlan0, enp3s0). Find it with
ip linkorip addr. - Optional: The subnet mask and DNS server IPs if you need to reconfigure the interface fully.
Step-by-Step
- Check the current gateway and routes
Runip route showto see the existing default route. It will look likedefault via 192.168.1.1 dev eth0. Note the interface name and the current gateway IP. - Add the new gateway temporarily
Use theipcommand to replace the default route. For example, to set the gateway to 192.168.1.254 on interface eth0:sudo ip route replace default via 192.168.1.254 dev eth0
If you get an error like "RTNETLINK answers: Network is unreachable", it means the gateway IP is not on the same subnet as your interface. You'll need to assign a correct IP to the interface first (see step 3). - If the gateway is on a different subnet, update the interface IP
If your new gateway is not in the same subnet as your current IP, you must change the interface's IP. For example, if the gateway is 10.0.0.1 and your interface is on 192.168.1.x, you need to set a new IP. Use:sudo ip addr add 10.0.0.100/24 dev eth0
Then remove the old IP (if any) with:sudo ip addr del 192.168.1.100/24 dev eth0
Now try the route replace again. - Verify connectivity
Check the route withip route showto confirm the new default is active. Then ping the gateway:ping -c 4 <NEW_GATEWAY_IP>. If that works, try pinging an external address likeping 8.8.8.8. If that fails, you may have a DNS issue or the gateway is not providing internet access. - Make the change permanent (if needed)
Theipcommand change is lost on reboot. To make it permanent, you need to edit your network configuration. The method depends on your distribution and network manager. For most modern distros using NetworkManager, you can usenmcli:nmcli con mod "YourConnectionName" ipv4.gateway 192.168.1.254
Then restart the connection:nmcli con up "YourConnectionName".
For systems using netplan (Ubuntu server), edit the YAML file in/etc/netplan/and set thegateway4orroutessection. For example:network: version: 2 ethernets: eth0: dhcp4: no addresses: [192.168.1.100/24] routes: - to: default via: 192.168.1.254
Then apply withsudo netplan apply.
If you use a static config file (like/etc/network/interfaceson Debian), edit it to addgateway 192.168.1.254under the interface and restart networking. - Update DNS if needed
If the new gateway also serves DNS, it's usually fine. But if you need to change DNS, edit/etc/resolv.conf(temporary) or set it in your network manager. For NetworkManager:nmcli con mod "YourConnectionName" ipv4.dns "8.8.8.8 8.8.4.4".
Common Mistakes
- Using the wrong interface name – Always verify with
ip link; don't guess. - Gateway IP not on the same subnet – Your interface must have an IP in the same subnet as the gateway, or the route will be unreachable.
- Forgetting to replace vs. add – Using
ip route addwhen a default route already exists gives an error. Usereplaceor delete the old one first. - Not making it permanent – If you only use the
ipcommand, the change is lost on reboot. Edit your network config for persistence. - Changing the gateway but not the DNS – If the new gateway doesn't provide DNS, you'll get 'name resolution' errors even with internet.
Tips
- Use the full command with interface – Always specify the device to avoid ambiguity:
ip route replace default via <IP> dev <interface>. - Test with a simple ping – Ping the gateway first, then an external IP, then a domain name to isolate the problem.
- Backup your config – Before editing network config files, copy them (e.g.,
sudo cp /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.yaml.bak). - Check if you have multiple default routes – Sometimes a VPN or other service adds a default route; use
ip route show table allto see them. - Use a static IP for reliability – If you're changing the gateway, you likely have a static IP setup. Make sure your IP and gateway are consistent.
Troubleshooting
If you still can't get online after changing the gateway:
- Restart your network interface – Sometimes the interface needs a refresh. Use
sudo ip link set eth0 downthensudo ip link set eth0 up. Or usesudo systemctl restart NetworkManager(if using NetworkManager). - Check if the gateway is reachable – Ping the gateway IP. If it doesn't respond, the gateway may be down or your interface is not properly configured. Check your physical connection.
- Verify you have an IP address – Run
ip addr show eth0. If you have no IP, you may need to enable DHCP temporarily:sudo dhclient eth0(or use your network manager). - Check for firewall rules – Sometimes firewalld or iptables blocks traffic. Temporarily disable with
sudo systemctl stop firewalld(if using firewalld) to test. - Look at the routing table – Ensure there's only one default route. If there are multiple, delete the extra ones:
sudo ip route del default via <OLD_GATEWAY>. - Check DNS – If ping to an IP works but domain names don't, your DNS is wrong. Edit
/etc/resolv.confto add a public DNS like 8.8.8.8 temporarily. - If all else fails, reboot – This will revert to your permanent config, which might be the old gateway. If that works, then the problem is in your permanent config.
Frequently Asked Questions
How do I find my current default gateway?
Run ip route show and look for the line starting with default. The IP after via is your gateway.
What if I don't know the new gateway IP?
Check your router's documentation or the label on it. Often it's 192.168.1.1 or 192.168.0.1. You can also ask your network administrator.
Can I change the gateway without root?
No, changing routes requires root privileges. Use sudo or log in as root.
Why does my change revert after reboot?
Because the ip command changes are temporary. You need to edit your network configuration files or use NetworkManager to make it permanent.
What is the difference between ip route add and ip route replace?
add will fail if a route already exists. replace will overwrite an existing matching route. For changing the default gateway, use replace.
I have a VPN; will changing the default gateway affect it?
Yes, a VPN often adds its own default route. Changing the system default may interfere. You may need to adjust the VPN's routing settings or use policy-based routing.