通过网络接口文件设置网关地址

iga*_*gal 4 networking debian virtualbox network-interface ifconfig

这是我的问题的删节版本:为什么gateway在我的网络接口文件中设置参数对我的网络接口没有影响?

或者,为什么post-up命令在失败的地方起作用gateway以及设置gateway参数到底应该做什么?

这是我的问题的详细描述。

/etc/network/interfaces考虑运行 Debian 的 VirtualBox VM 上的以下网络接口配置文件 ( ):

# /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
Run Code Online (Sandbox Code Playgroud)

当我启动机器并运行时,route -n我得到以下输出:

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
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,该接口上没有为192.168.56.0/24网络设置网关eth1

现在考虑以下替代配置文件:

# /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
Run Code Online (Sandbox Code Playgroud)

当我使用此配置重新启动计算机并运行时,route -n我得到以下输出:

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
Run Code Online (Sandbox Code Playgroud)

因此,结果是设置默认网关的post-up/方法有效,但使用参数本身则无效。我在这里缺少什么?pre-downgateway

Chr*_*her 6

从手册(man interfaces):

gateway address
                 Default gateway (dotted quad)
Run Code Online (Sandbox Code Playgroud)

因此,gateway用于指定默认路由,而不是静态路由。静态路由用于未设计为使用默认网关的网络流量。默认网关用于所有非发往本地网络且路由表中未指定首选路由的流量。您的 DHCP 接口eth0获取默认网关。之后,您可以使用 指定静态eth1路由post-up route add default gw

进一步阅读:

分离两个网络接口上的网络流量

尝试如下。

echo '200 hostonly' >> /etc/iproute2/rt_tables
Run Code Online (Sandbox Code Playgroud)

编辑/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
Run Code Online (Sandbox Code Playgroud)

重新启动并查看它的外观。