两个网络接口位于不同的网络上,但只有一个可以访问互联网

两个网络接口位于不同的网络上,但只有一个可以访问互联网

我有两张网卡:
enp4s0有一个公共静态ip(我已经在下面编辑了ip)。
enp2s0有一个本地静态IP(在我的路由器后面,192.168.0.1)。

所有请求都应默认通过enp4s0以及来自以下的所有请求enp2s0应归还enp2s0

enp4s0发送或接收请求没有问题。
enp2s0另一方面无法发送请求但接收 SSH 连接和 ping 仍然可以。

$ ping -I enp2s0 8.8.8.8
$ ping -I enp2s0 127.0.0.1

导致超时和

$ ping -I enp2s0 localhost
connect: Network is unreachable

但是,对路由器或任何其他本地设备进行 ping 操作都是可行的。

$ ping -I enp2s0 192.168.0.1
$ ping -I enp2s0 192.168.0.3

/etc/网络/接口

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto enp4s0
iface enp4s0 inet dhcp

# The secondary network interface
allow-hotplug enp2s0
iface enp2s0 inet static
address 192.168.0.2
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255

post-up ip route add 192.168.0.0/24 dev enp2s0 src 192.168.0.2 table routetable
post-up ip route add default via 192.168.0.1 dev enp2s0 table routetable
post-up ip rule add from 192.168.0.2/32 table routetable
post-up ip rule add to 192.168.0.2/32 table routetable

$ /sbin/route -n

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.11.136.1     0.0.0.0         UG    0      0        0 enp4s0
10.11.136.0     0.0.0.0         255.255.252.0   U     0      0        0 enp4s0
192.168.0.0     0.0.0.0         255.255.255.0   U     0      0        0 enp2s0

$ ip 路由

default via 10.11.136.1 dev enp4s0
10.11.136.0/22 dev enp4s0 proto kernel scope link src 10.11.136.13
192.168.0.0/24 dev enp2s0 proto kernel scope link src 192.168.0.2

/etc/iproute2/rt_tables

#
# reserved values
#
255     local
254     main
253     default
0       unspec
#
# local
#
#1      inr.ruhep
1 routetable

$ ifconfig

enp2s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 192.168.0.2  netmask 255.255.255.0  broadcast 192.168.0.255
    inet6 fe80::222:15ff:fe3c:5d29  prefixlen 64  scopeid 0x20<link>
    ether 00:22:15:3c:5d:29  txqueuelen 1000  (Ethernet)
    RX packets 344  bytes 52868 (52.8 KB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 292  bytes 43536 (43.5 KB)
    TX errors 0  dropped 0 overruns 0  carrier 2  collisions 0

enp4s0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
    inet 10.11.136.13  netmask 255.255.252.0  broadcast 10.11.139.255
    inet6 fe20::2ade:d0ff:fe01:4e4  prefixlen 64  scopeid 0x20<link>
    ether 93:de:d0:03:02:e4  txqueuelen 1000  (Ethernet)
    RX packets 2609  bytes 450767 (450.7 KB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 2949  bytes 494772 (494.7 KB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
    inet 127.0.0.1  netmask 255.0.0.0
    inet6 ::1  prefixlen 128  scopeid 0x10<host>
    loop  txqueuelen 1000  (Local Loopback)
    RX packets 848  bytes 68061 (68.0 KB)
    RX errors 0  dropped 0  overruns 0  frame 0
    TX packets 848  bytes 68061 (68.0 KB)
    TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

$ sudo ifdown enp2s0 && sudo ifup -v enp2s0

ifdown: interface enp2s0 not configured

Configuring interface enp2s0=enp2s0 (inet)
/bin/run-parts --exit-on-error --verbose /etc/network/if-pre-up.d
run-parts: executing /etc/network/if-pre-up.d/ethtool
run-parts: executing /etc/network/if-pre-up.d/ifenslave
+ [ inet = meta ]
+ IF_BOND_SLAVES=
+ [  ]
+ [  ]
+ [ -z  ]
+ exit
run-parts: executing /etc/network/if-pre-up.d/vlan
/bin/ip addr add 192.168.0.2/255.255.255.0 broadcast 192.168.0.255        dev     enp2s0 label enp2s0
RTNETLINK answers: File exists
Failed to bring up enp2s0.

答案1

我建议 enp2s0 节如下:

# The secondary network interface
allow-hotplug enp2s0
iface enp2s0 inet static
address 192.168.0.2
netmask 255.255.255.0
gateway 192.168.0.1
dns-nameservers 192.168.0.1 8.8.8.8

重启界面:

sudo ifdown enp2s0 && sudo ifup -v enp2s0

查看:

ping -c3 www.ubuntu.com

相关内容