Can I prevent a default route being added when bringing up an interface?

I have a system with two NICs on it. This machine, and a few accompanying devices will be moved and attached to different LANs or sometimes it’ll be using dial-up.

    eth0:
    - 10.x.x.x address space
    - no internet gateway
    - only a few devices

eth1 (when used):
- 172.16.x.x or 192.168.x.x or other address spaces
- access to the gateway from LAN to internet

ppp0 (when used):
- internet access through dialup using KPPP

I’m using ifconfig to bring interfaces up or down (other than with ppp0, which is handled by KPPP).

If I bring up eth1 first, it gets an address from its DHCP and gets the gateway and that is added to routing so there’s no trouble reaching the LAN and the internet.

If I bring up eth0 first or second, it gets its address and sets the default gateway to within its address space (in the 10.x.x.x range). If I bring up eth0 first and eth1 second, the default gateway is still kept to within the 10.x.x.x range.

So no matter what I do, eth0 will override eth1 and “claim” the gateway in the routing.

Is there some way to either prevent eth0 from claiming the gateway, or to make sure eth1 (if brought up 2nd) uses its gateway? Or can I somehow prioritize a ranking of which interface’s gateway should be used over the others?

I basically want to make sure eth1’s default address space gateway is used if it’s active, and if not, then the ppp0’s default gateway is used. I’d like to be able to prevent eth0 from ever having the default gateway.

Asked By: Tango

||

The DHCP server configuration is wrong. It must not send a default gateway option when it can’t provide routing to the rest of the world. If it does send that option then any client may assume that it can send packets for any off-link destination to the specified default gateway.

So your box is right in using the default gateway from eth0 if it is told so by DHCP. The solution is to remove the bad option from your DHCP server.

Answered By: Sander Steffann

ok, so what you want is for the machine to never bring up a default gateway when it brings up eth0 and gets an its address via DHCP.

Here is the solution:

Edit file:

/etc/dhcp/dhclient-up-hooks

and populate with:

#!/bin/sh
## Prevent DHCP server on eth0 from forcing a default route on us

case ${interface} in
  eth0)
     printf "executing ip route delete default via $new_routersn" 
     ip route delete default via $new_routers
  ;;
     *)
  ;;
esac

before:

[root@centos7lab dhcp]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.4.1     0.0.0.0         UG    20     0        0 eth0
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

after ifdown eth0, ifup eth0:

[root@centos7lab dhcp]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.4.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
Answered By: Ricardo

On RHEL6/Fedora 22 the following has been tested.

In /etc/sysconfig/network-scripts/ifcfg-eth1
add the line:

DEFROUTE=no

Replace eth1 with name of interface where default routing is not wanted.

This can also be done via Network Manager GUI by checking the box “Use this connection only for resources on its network” at the bottom of the IPv4 tab.

DEFROUTE=no prevents the addition of the default route ( destination 0.0.0.0 ) to the routing table when the interface is enabled. ie. the following entry would not be added.

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         172.16.x.x      0.0.0.0         UG        0 0          0 eth1
Answered By: user3489409

I faced similar problem on Raspbian (I suppose the solution below will be applicable to Debian as well). Raspberry Pi 3 has 2 NICs integrated: Wi-Fi and Ethernet. I use both of them, they are wlan0 and eth0, respectively. wlan0 is connected to my home Wi-Fi network and internet access comes through this interface. It gets its settings via DHCP from my home router. eth0 is connected directly to my windows PC and has static IP assigned. No internet access via eth0 was available since I didn’t configure it on my windows PC.

In Raspbian, the dhcpcd daemon is responsible for configuring network interfaces. In order to set static IP to eth0 interface, there were following lines added to the end of /etc/dhcpcd.conf:

interface eth0

static ip_address=192.168.2.2/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1

With this settings, dhcpcd created 2 default routes and route via eth0 had higher priority than that via wlan0:

pi@raspberrypi:~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.2.1     0.0.0.0         UG    202    0        0 eth0
default         192.168.1.254   0.0.0.0         UG    303    0        0 wlan0
192.168.1.0     *               255.255.255.0   U     303    0        0 wlan0
192.168.2.0     *               255.255.255.0   U     202    0        0 eth0

So I had no internet access, because system tried to route it via eth0 and it didn’t have internet access, as I mentioned above.

To solve the problem, I used nogateway option in the /etc/dhcpcd.conf for eth0 interface. So eth0-specific configuration started looking like that:

interface eth0

static ip_address=192.168.2.2/24
static routers=192.168.2.1
static domain_name_servers=192.168.2.1
nogateway

After saving this configuration and rebooting, there were no default route via eth0:

pi@raspberrypi:~ $ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.1.254   0.0.0.0         UG    303    0        0 wlan0
192.168.1.0     *               255.255.255.0   U     303    0        0 wlan0
192.168.2.0     *               255.255.255.0   U     202    0        0 eth0

Internet access appeared, and the problem was solved.

Answered By: Roman Me

You can edit dhcpclient.conf file and not request for any default route from remote DHCP server.

A small sample of what i have done and it is working for my case

send host-name = "random-hostname";

request subnet-mask, broadcast-address, time-offset, interface-mtu, rfc3442-classless-static-routes, ntp-servers;

Answered By: Netboy

From a similar question, this answer was a better one for me than any of the ones so far here – to be able to use “also request” to have one interface ask for a router and the other not.

https://serverfault.com/a/674738/427704

Answered By: Michael Firth

Roman Me’s answer is great, but I was thinking, what if you one-day wanted to use eth0 for internet?

What I did instead of specifying nogateway in my /etc/dhcpcd.conf, was I added instead metric 1000

By doing this, it will still add the routes, but it will put it as a high metric (low priority) to the other routes.

Answered By: Xebozone

I had this problem using busybox’s udhcpc (alpine) on a multihomed system using DHCP services I don’t control. Solution:

$ cat /etc/udhcpc/udhcpc.conf
# space-separated list of interface names that shouldn't offer gateways
NO_GATEWAY="eth0"

You’ll need to ifdown/ifup or reboot to see the changes.

It doesn’t seem possible to configure metrics using configuration along; you’d need to modify /usr/share/udhcpc/default.script.

Answered By: dgc
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.