更改默认网关、多个 NIC

更改默认网关、多个 NIC

我有 Ubuntu 13.10(内核 3.11.0-19-generic)。启动时,网络配置如下:

dor@ubuntu:~$ sudo ifconfig -a
em1       Link encap:Ethernet  HWaddr 44:37:e6:4c:ca:0a  
          inet addr:10.1.0.102  Bcast:10.1.7.255  Mask:255.255.248.0
          inet6 addr: fe80::4637:e6ff:fe4c:ca0a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:732 errors:0 dropped:0 overruns:0 frame:0
          TX packets:79 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:68789 (68.7 KB)  TX bytes:12907 (12.9 KB)
          Interrupt:20 Memory:fe600000-fe620000 

eth5      Link encap:Ethernet  HWaddr 00:0a:5e:50:cf:1e  
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:20 Base address:0xa000 

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:101 errors:0 dropped:0 overruns:0 frame:0
          TX packets:101 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:8485 (8.4 KB)  TX bytes:8485 (8.4 KB)

p4p1      Link encap:Ethernet  HWaddr 00:15:17:0b:6d:1c  
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::215:17ff:fe0b:6d1c/64 Scope:Link
          inet6 addr: fc00::215:17ff:fe0b:6d1c/64 Scope:Global
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:41 errors:0 dropped:0 overruns:0 frame:0
          TX packets:100 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:4062 (4.0 KB)  TX bytes:15981 (15.9 KB)
          Interrupt:16 Memory:fe540000-fe560000 

dor@ubuntu:~$ sudo route -n
[sudo] password for dor: 
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0        0 p4p1
10.1.0.0        0.0.0.0         255.255.248.0   U     0      0        0 em1
192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 p4p1

互联网网关是 10.1.0.1。因此我手动执行以下操作:

dor@ubuntu:~$ sudo route del default
dor@ubuntu:~$ sudo route add default gw 10.1.0.1

这就是我上网的方法。但我该如何做才能避免每次都手动操作呢?永恒的

内容/etc/network/interfaces(删除了部分注释行):

dor@ubuntu:~$ cat /etc/network/interfaces
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto em1

iface eth5 inet static
     address 10.90.90.91
     netmask 255.255.255.0
     gateway 10.90.90.90

iface em1 inet static
     address 10.1.0.102
     netmask 255.255.248.0
     gateway 10.1.0.1

我读过interfaces和的手册route很多次,也用谷歌搜索了很多次,但还是搞不懂。谢谢。


编辑#1,2014年4月17日12:11 UTC: (回复者索布里克

我已编辑该文件/etc/network/interfaces以包含以下内容:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto em1
#iface em1 inet dhcp

iface eth5 inet static
     address 10.90.90.91
     netmask 255.255.255.0
#     gateway 10.90.90.90

iface p4p1 inet static
     address 192.168.1.32
     netmask 255.255.255.0
#     gateway 192.168.1.22

iface em1 inet static
     address 10.1.0.102
     netmask 255.255.248.0
     gateway 10.1.0.1

现在启动后立即可以访问互联网,无需执行任何命令。
问题是我无法访问网络192.168.1.X
之前可以访问。我该如何解决这个问题?一些数据:

dor@ubuntu:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.1.0.1        0.0.0.0         UG    0      0        0 em1
10.1.0.0        0.0.0.0         255.255.248.0   U     0      0        0 em1
dor@ubuntu:~$ ifconfig -a
em1       Link encap:Ethernet  HWaddr 44:37:e6:4c:ca:0a
          inet addr:10.1.0.102  Bcast:10.1.7.255  Mask:255.255.248.0
          inet6 addr: fe80::4637:e6ff:fe4c:ca0a/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:30356 errors:0 dropped:0 overruns:0 frame:0
          TX packets:329 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2752089 (2.7 MB)  TX bytes:43904 (43.9 KB)
          Interrupt:20 Memory:fe600000-fe620000

eth5      Link encap:Ethernet  HWaddr 00:0a:5e:50:cf:1e
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:20

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:97 errors:0 dropped:0 overruns:0 frame:0
          TX packets:97 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:8463 (8.4 KB)  TX bytes:8463 (8.4 KB)

p4p1      Link encap:Ethernet  HWaddr 00:15:17:0b:6d:1c
          BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)
          Interrupt:16 Memory:fe540000-fe560000

编辑 #2,2014 年 4 月 20 日 08:05 UTC:

最终我编辑了该文件/etc/network/interfaces以具有以下配置:

auto em1
iface em1 inet dhcp

auto eth5
iface eth5 inet static
     address 10.90.90.91
     netmask 255.255.255.0
     up route add -net 10.90.90.0/24 dev eth5

auto p4p1
iface p4p1 inet static
     address 192.168.1.32
     netmask 255.255.255.0
     up route add -net 192.168.1.0/24 dev p4p1

并且成功了!参见:

dor@ubuntu:~$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.1.0.1        0.0.0.0         UG    0      0        0 em1
10.1.0.0        0.0.0.0         255.255.248.0   U     0      0        0 em1
10.90.90.0      0.0.0.0         255.255.255.0   U     0      0        0 eth5
10.90.90.0      0.0.0.0         255.255.255.0   U     0      0        0 eth5
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 p4p1
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 p4p1

唯一的问题是有这些重复项。但这不会干扰任何事情,它仍然可以工作。


编辑#3,2014年4月20日 09:57 UTC:

事实上正如我所怀疑的,重复是由以下命令引起的,例如:

up route add -net 192.168.1.0/24 dev p4p1

于是我把它们移除了。现在一切都好了。

答案1

我认为这里的错误在于,您在配置文件中配置了多个默认网关。它不是那样工作的 - 它是默认的,因为任何没有已知路由被发送到那里进行传递(期望它会知道,或者有它自己的默认值)。但它不是明确地绑定到特定接口 - 隐含地会有接口可以到达默认网关,但其他接口则不能。

因此,我想您需要做的是删除“网关”行,然后添加您想要的单行 - 最好是在正确的界面节中,但我实际上认为这并不重要。

相关内容