6

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.

  • It's odd that using ifconfig would cause any sort of DHCP interaction. Typically ifup will do this, by starting dhclient. Are your eth* interfaces possibly being brought up by the system boot process, say, /etc/init.d/network, or by NetworkManager? – Mark Plotnick Feb 4 '15 at 20:02
  • @MarkPlotnick: This is after I've booted and am using "ifconfig eth1 up" (or down or eth0...). I guess the simplest form of what I want to do would be to bring up eth0 without any routes being added other than to the 10.x.x.x address space. – Tango Feb 4 '15 at 20:12
4

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.

  • Okay, that makes a lot of sense. Unfortunately, the DHCP server is a DLink DNS-321, which doesn't allow many control options. It seems to require I include a gateway. I'll have to see if I can hack it somehow and edit the config file. But, one other issue: Why does it always take the gateway from the 10.x.x.x DHCP server and not always take the gateway from the other server? – Tango Feb 4 '15 at 22:52
  • I'm not sure how linux chooses between identical routes. It is probably based on the metric. Can you show your routing table when you have multiple default routes? – Sander Steffann Feb 4 '15 at 23:03
  • It won't allow multiple default routes. The frustration is that for some reason eth0 always listens to the DHCP and updates the gateway. With eth1, it only listens and uses the gateway if it's the first and only interface that's up. If eth0's gateway didn't always override eth1's, then I'd just write a script so when eth0 was brought up, eth1 was taken down, then brought up. – Tango Feb 4 '15 at 23:32
  • It almost sounds like hard coded weirdness in the DHCP client. Which one are you using? As an alternative: it might be easier to just swap eth0 and eth1 ;) – Sander Steffann Feb 4 '15 at 23:40
  • It's on a D-Link DNS-321. But I've gone through /var/logs/syslog and I can see that the DHCP for eth1 also sends in the gateway every time. I just can't figure out why eth1 always takes the gateway and adds it to the route and eth1 doesn't. I'm going to try limiting the range for the DHCP eth0 uses to start at 10.0.0.3 and then make eth0 static, using 10.0.0.2 and see if that leads it to ignore that rude DHCP server. – Tango Feb 4 '15 at 23:47
11

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.

  • This was exactly my situation, and this was exactly the solution. – Gene Aug 17 '18 at 6:54
  • Thanks, nogateway is the way to go in latest debian distros – Alessandro Dionisi Aug 30 '18 at 8:53
5

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
  • 1
    Please explain why does it fix the problem. And when it works. It will work on Debian based distros (I believe) but not on distros with different interface naming (or distro using systemd for services networking). – grochmal Aug 9 '16 at 0:45
  • 1
    More like Red Hat than Debian based systems. – ilkkachu Aug 9 '16 at 0:56
4

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_routers\n" 
     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
  • This deletes the route with the gateway after it's established, which means it's already wiped out any other route with a default gateway. Or do I misunderstand? – Tango Feb 4 '15 at 23:04
  • it will delete just the gateway the DHCP client created when it brought up eth0, it will not affect any other gateway already on the system. From your description I would assume your problem is that you are getting two simultaneous default gateways at some time (one on eth0 and one on eth1) and you need to get rid of the one for eth0 before it is even put on the routing table. If you post your route -n output at the time of the issue I might change my assumption. – Ricardo Feb 5 '15 at 0:14
1

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.