Setting Gateway Address via Network Interfaces File

20,272

Solution 1

From a networking perspective you have network gateways as a next hop versus a default gateway also known as the gateway of last resort. From your first route -n command (without post-up) you are seeing 10.0.2.2 as the gateway of last resort. When your machine has a request for a network not in its routing table it will forward the unknown network traffic to this address. The gateway setting for the interfaces setting is the next hop for traffic on that network. However since you interface is directly connected to that network it does not need the gateway to forward traffic. The network will always be known and the traffic destined for devices on that network will be forwarded directly to that device. Your post-up command is creating two gateways of last resort (which is undesirable). If you want the 192.168.56.1 to be your actual last resort you want to do a default route replace not add.

Solution 2

This gives some incite

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s1-networkscripts-static-routes.html

Your routing table provides information about how to route connections between all interfaces.

The interface's default route would be used for other purposes and only applies to that interface, not the system as a whole.

If you only had one network interface, the system would automatically set it as the default route because it is the only default gateway.

Remember the routing table works from bottom up, so the default gateway at the top will be the last route used to attempt a connection.

Solution 3

From the manual (man interfaces):

gateway address
                 Default gateway (dotted quad)

So, gateway is used to specify a default route, not a static route. Static routes are used for network traffic that is not designed to use a default gateway. The default gateway is used for all traffic which is not destined for the local network and for which no preferred route has been specified in a routing table. Your DHCP interface on eth0 acquires the default gateway. After that, you specify a static route on eth1 with post-up route add default gw.

Further reading:

Separate Network Traffic on Two Network Interfaces

Try it as follows.

echo '200 hostonly' >> /etc/iproute2/rt_tables

Edit /etc/network/interfaces.

# The loopback network interface
auto lo
iface lo inet loopback

# The VirtualBox NAT interface.
auto eth0
allow-hotplug eth0
iface eth0 inet dhcp

# The VirtualBox host-only interface.
auto eth1
allow-hotplug eth1
iface eth1 inet static
    address 192.168.56.2
    netmask 255.255.255.0
    post-up ip route add 192.168.56.0/24 dev eth1 src 192.168.56.1 table hostonly
    post-up ip route add default via 192.168.56.1 dev eth1 table hostonly
    post-up ip rule add from 192.168.56.2/32 table hostonly
    post-up ip rule add to 192.168.56.2/32 table hostonly

Reboot and see how it looks.

Share:
20,272

Related videos on Youtube

igal
Author by

igal

BY DAY: Python, Linux and Mac OS. Getting all my ducks in one basket. BY NIGHT: Sleeping. Dead as a cucumber. FOR FUN: Beer and Netflix. Taking nothing for granite. "All roads lead to moss."

Updated on September 18, 2022

Comments

  • igal
    igal over 1 year

    Here is the abridged version of my question: Why does setting the gateway parameter in my network interfaces file have no effect on my network interfaces?

    Or alternatively, why does the post-up command work where gateway fails and what exactly is setting the gateway parameter supposed to do?

    Here is a longer description of my question.

    Consider the following network-interface configuration file (/etc/network/interfaces) on a VirtualBox VM running Debian:

    # /etc/network/interfaces
    
    # Original file
    
    # The loopback network interface
    auto lo
    iface lo inet loopback
    
    # The primary network interface (NAT)
    auto eth0
    allow-hotplug eth0
    iface eth0 inet dhcp
    
    # Host-only interface (vboxnet0)
    auto eth1
    allow-hotplug eth1
    iface eth1 inet static
    address 192.168.56.2
    netmask 255.255.255.0
    network 192.168.56.0
    broadcast 192.168.56.255
    gateway 192.168.56.1
    

    When I boot the machine and run route -n I get the following output:

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
    10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
    192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
    

    To my surprise, there is no gateway set for the 192.168.56.0/24 network on the eth1 interface.

    Now consider the following alternative configuration file:

    # /etc/network/interfaces
    
    # Modified file
    
    # The loopback network interface
    auto lo
    iface lo inet loopback
    
    # The primary network interface (NAT)
    auto eth0
    allow-hotplug eth0
    iface eth0 inet dhcp
    
    # Host-only interface (vboxnet0)
    auto eth1
    allow-hotplug eth1
    iface eth1 inet static
    address 192.168.56.2
    netmask 255.255.255.0
    network 192.168.56.0
    broadcast 192.168.56.255
    # gateway 192.168.56.1
    post-up route add default gw 192.168.56.1
    pre-down route del default gw 192.168.56.1
    

    When I reboot the machine with this configuration and run route -n I get the following output instead:

    Kernel IP routing table
    Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
    0.0.0.0         192.168.56.1    0.0.0.0         UG    0      0        0 eth1
    0.0.0.0         10.0.2.2        0.0.0.0         UG    0      0        0 eth0
    10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0
    192.168.56.0    0.0.0.0         255.255.255.0   U     0      0        0 eth1
    

    So the upshot is that the post-up/pre-down approach to setting a default gateway works, but using the gateway parameter itself does not. What am I missing here?

    • Joe
      Joe over 6 years
      "post-up route add default gw 192.168.56.1" This is what adds it to your routing table
    • igal
      igal over 6 years
      @Joe Thanks for the comment. What is the gateway parameter for if not to set the gateway?
    • Joe
      Joe over 6 years
      it does but for specifically on the interface
  • igal
    igal over 6 years
    Thank you for that explanation. Could you address the function of the gateway parameter in the interfaces file? What effect does it have and how I can I measure this?
  • EnterUserNameHere
    EnterUserNameHere over 6 years
    @igal if you review the man pages for the static method the only required setting is IP address in dotted quad/netmask. If you had for example eth0 on 192.168.0.0/24 and eth1 on 192.168.1.0/24 listed in the file. When the link state for one of those interfaces changes from down to up the listed gateway would then also become the default gateway. In practice this is what happens if you have a wireless card that connects to the internet on one network and a wired that connects to the internet on a different network. The gateway used is from the up interfaces definitions.
  • igal
    igal over 6 years
    Thank you very much for this explanation and for the link (which I will read through shortly). This was definitely informative. But my question was asking about the effect of the "gateway" parameter in the interfaces file. It doesn't seem to be having any effect. What does it do?
  • Christopher
    Christopher over 6 years
    It specifies the default gateway, the first hop out on the default route.
  • igal
    igal over 6 years
    But it doesn't actually do that - the "post-up" command is setting the gateway and the "gateway" parameter has no effect. That's sort of the point of my question. Apparently I haven't phrased my question clearly, because it seems that no one is answering it in the way I would have expected.
  • Christopher
    Christopher over 6 years
    Yes... DHCP acquires the default gateway without your having to specify it. The post-up command is setting a static route.
  • igal
    igal over 6 years
    Ah, wait, I think I understand. Maybe the interfaces file will only set one gateway and having a dhcp stanza prevents subsequent "gateway" parameters from taking effect. Is that what you're saying?
  • Christopher
    Christopher over 6 years
    Yes. There can only be one, and only one, default route, which is set via DHCP or with the gateway stanza when using static addressing.
  • igal
    igal over 6 years