How to Permanently Change Default Route to DHCP Interface (eth1) Without Route Push in Ubuntu


26 views

Your current routing table shows eth0 with static configuration as the default route, while eth1 (DHCP) has a specific network route. Let's examine this setup:

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         RT-C0C1C0CF879F 0.0.0.0         UG    100    0        0 eth0
10.0.0.0        *               255.255.255.0   U     0      0        0 eth0
1.1.1.0         *               255.255.240.0   U     0      0        0 eth1

Method 1: Network Manager Configuration

For Ubuntu 12.04 using NetworkManager, edit the connection profile:

sudo nano /etc/NetworkManager/system-connections/Your-Eth1-Connection

Add these lines under the [ipv4] section:

[ipv4]
method=auto
never-default=false
route-metric=10

Then set eth0 to higher metric (e.g., 100):

[ipv4]
method=manual
never-default=true
address1=10.0.0.100/24,10.0.0.1
route-metric=100

Method 2: Using /etc/network/interfaces

For systems not using NetworkManager, edit:

sudo nano /etc/network/interfaces

Configure eth0 (static):

auto eth0
iface eth0 inet static
    address 10.0.0.100
    netmask 255.255.255.0
    gateway 10.0.0.1
    metric 100

Configure eth1 (DHCP) with lower metric:

auto eth1
iface eth1 inet dhcp
    metric 10

After making changes, restart networking:

sudo /etc/init.d/networking restart

Verify with:

ip route show

You should see eth1 with lower metric becoming the default route.

If you're using both interfaces for different purposes but want eth1 as default:

# Add explicit route for eth0 network
sudo ip route add 10.0.0.0/24 dev eth0 src 10.0.0.100 table main

For Ubuntu 12.04, create a post-up script:

sudo nano /etc/network/if-up.d/fixroutes

Add:

#!/bin/sh
if [ "$IFACE" = "eth1" ]; then
    ip route del default via 10.0.0.1 dev eth0
    ip route add default dev eth1
fi

Make it executable:

sudo chmod +x /etc/network/if-up.d/fixroutes



Here's my current routing table showing eth0 (static) as the default route:

# ip route show
default via RT-C0C0C0CF879F dev eth0 proto static metric 100 
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.15 
1.1.1.0/20 dev eth1 proto kernel scope link src 1.1.1.10

The default route through eth0 goes through NAT, while eth1 (DHCP) provides direct internet access. For my development environment, I need all outbound traffic to use the direct connection.

Edit /etc/network/interfaces to modify the DHCP priority:


# Configuration for eth0 (static)
auto eth0
iface eth0 inet static
    address 10.0.0.15
    netmask 255.255.255.0
    gateway 10.0.0.1
    metric 200

# Configuration for eth1 (DHCP)
auto eth1
iface eth1 inet dhcp
    metric 100

For more advanced control, create a custom routing table:


# /etc/iproute2/rt_tables
100    direct_route

# Add to /etc/network/interfaces
post-up ip route add default dev eth1 table direct_route
post-up ip rule add from 1.1.1.0/20 lookup direct_route

Check your routing table:


ip route show
ip rule show

If the route doesn't persist:
1. Check DHCP client configuration in /etc/dhcp/dhclient.conf
2. Verify NetworkManager isn't interfering (if installed)
3. Check system logs: journalctl -u networking