linux – 更改默认网关,多个NIC

前端之家收集整理的这篇文章主要介绍了linux – 更改默认网关,多个NIC前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有Ubuntu 13.10(内核3.11.0-19-通用).启动时,网络配置如下:
  1. dor@ubuntu:~$sudo ifconfig -a
  2. em1 Link encap:Ethernet HWaddr 44:37:e6:4c:ca:0a
  3. inet addr:10.1.0.102 Bcast:10.1.7.255 Mask:255.255.248.0
  4. inet6 addr: fe80::4637:e6ff:fe4c:ca0a/64 Scope:Link
  5. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  6. RX packets:732 errors:0 dropped:0 overruns:0 frame:0
  7. TX packets:79 errors:0 dropped:0 overruns:0 carrier:0
  8. collisions:0 txqueuelen:1000
  9. RX bytes:68789 (68.7 KB) TX bytes:12907 (12.9 KB)
  10. Interrupt:20 Memory:fe600000-fe620000
  11.  
  12. eth5 Link encap:Ethernet HWaddr 00:0a:5e:50:cf:1e
  13. BROADCAST MULTICAST MTU:1500 Metric:1
  14. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  15. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  16. collisions:0 txqueuelen:1000
  17. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  18. Interrupt:20 Base address:0xa000
  19.  
  20. lo Link encap:Local Loopback
  21. inet addr:127.0.0.1 Mask:255.0.0.0
  22. inet6 addr: ::1/128 Scope:Host
  23. UP LOOPBACK RUNNING MTU:65536 Metric:1
  24. RX packets:101 errors:0 dropped:0 overruns:0 frame:0
  25. TX packets:101 errors:0 dropped:0 overruns:0 carrier:0
  26. collisions:0 txqueuelen:0
  27. RX bytes:8485 (8.4 KB) TX bytes:8485 (8.4 KB)
  28.  
  29. p4p1 Link encap:Ethernet HWaddr 00:15:17:0b:6d:1c
  30. inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
  31. inet6 addr: fe80::215:17ff:fe0b:6d1c/64 Scope:Link
  32. inet6 addr: fc00::215:17ff:fe0b:6d1c/64 Scope:Global
  33. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  34. RX packets:41 errors:0 dropped:0 overruns:0 frame:0
  35. TX packets:100 errors:0 dropped:0 overruns:0 carrier:0
  36. collisions:0 txqueuelen:1000
  37. RX bytes:4062 (4.0 KB) TX bytes:15981 (15.9 KB)
  38. Interrupt:16 Memory:fe540000-fe560000
  39.  
  40. dor@ubuntu:~$sudo route -n
  41. [sudo] password for dor:
  42. Kernel IP routing table
  43. Destination Gateway Genmask Flags Metric Ref Use Iface
  44. 0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 p4p1
  45. 10.1.0.0 0.0.0.0 255.255.248.0 U 0 0 0 em1
  46. 192.168.1.0 0.0.0.0 255.255.255.0 U 1 0 0 p4p1

互联网的门户是10.1.0.1.所以我手动执行以下内容

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

这就是我设法上网的方式.
但是,不是每次手动执行此操作,我如何使其永久化?

/ etc / network / interfaces的内容(删除了一些注释行):

  1. dor@ubuntu:~$cat /etc/network/interfaces
  2. # The loopback network interface
  3. auto lo
  4. iface lo inet loopback
  5.  
  6. # The primary network interface
  7. auto em1
  8.  
  9. iface eth5 inet static
  10. address 10.90.90.91
  11. netmask 255.255.255.0
  12. gateway 10.90.90.90
  13.  
  14. iface em1 inet static
  15. address 10.1.0.102
  16. netmask 255.255.248.0
  17. gateway 10.1.0.1

我已经阅读了很多次接口和路由手册以及许多谷歌搜索结果,但无法弄清楚这一点.谢谢.

编辑#1,17 Apr 2014 12:11 UTC:
(回应响应者Sobrique)

我编辑文件/ etc / network / interfaces以获得以下内容

  1. # This file describes the network interfaces available on your system
  2. # and how to activate them. For more information,see interfaces(5).
  3.  
  4. # The loopback network interface
  5. auto lo
  6. iface lo inet loopback
  7.  
  8. # The primary network interface
  9. auto em1
  10. #iface em1 inet dhcp
  11.  
  12. iface eth5 inet static
  13. address 10.90.90.91
  14. netmask 255.255.255.0
  15. # gateway 10.90.90.90
  16.  
  17. iface p4p1 inet static
  18. address 192.168.1.32
  19. netmask 255.255.255.0
  20. # gateway 192.168.1.22
  21.  
  22. iface em1 inet static
  23. address 10.1.0.102
  24. netmask 255.255.248.0
  25. gateway 10.1.0.1

现在启动时可立即访问Internet,无需执行任何命令.
问题是我无法访问网络192.168.1.X.
早些时候有一个访问权限.我怎样才能解决这个问题?一些数据:

  1. dor@ubuntu:~$route -n
  2. Kernel IP routing table
  3. Destination Gateway Genmask Flags Metric Ref Use Iface
  4. 0.0.0.0 10.1.0.1 0.0.0.0 UG 0 0 0 em1
  5. 10.1.0.0 0.0.0.0 255.255.248.0 U 0 0 0 em1
  6. dor@ubuntu:~$ifconfig -a
  7. em1 Link encap:Ethernet HWaddr 44:37:e6:4c:ca:0a
  8. inet addr:10.1.0.102 Bcast:10.1.7.255 Mask:255.255.248.0
  9. inet6 addr: fe80::4637:e6ff:fe4c:ca0a/64 Scope:Link
  10. UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  11. RX packets:30356 errors:0 dropped:0 overruns:0 frame:0
  12. TX packets:329 errors:0 dropped:0 overruns:0 carrier:0
  13. collisions:0 txqueuelen:1000
  14. RX bytes:2752089 (2.7 MB) TX bytes:43904 (43.9 KB)
  15. Interrupt:20 Memory:fe600000-fe620000
  16.  
  17. eth5 Link encap:Ethernet HWaddr 00:0a:5e:50:cf:1e
  18. BROADCAST MULTICAST MTU:1500 Metric:1
  19. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  20. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  21. collisions:0 txqueuelen:1000
  22. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  23. Interrupt:20
  24.  
  25. lo Link encap:Local Loopback
  26. inet addr:127.0.0.1 Mask:255.0.0.0
  27. inet6 addr: ::1/128 Scope:Host
  28. UP LOOPBACK RUNNING MTU:65536 Metric:1
  29. RX packets:97 errors:0 dropped:0 overruns:0 frame:0
  30. TX packets:97 errors:0 dropped:0 overruns:0 carrier:0
  31. collisions:0 txqueuelen:0
  32. RX bytes:8463 (8.4 KB) TX bytes:8463 (8.4 KB)
  33.  
  34. p4p1 Link encap:Ethernet HWaddr 00:15:17:0b:6d:1c
  35. BROADCAST MULTICAST MTU:1500 Metric:1
  36. RX packets:0 errors:0 dropped:0 overruns:0 frame:0
  37. TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
  38. collisions:0 txqueuelen:1000
  39. RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
  40. Interrupt:16 Memory:fe540000-fe560000

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

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

  1. auto em1
  2. iface em1 inet dhcp
  3.  
  4. auto eth5
  5. iface eth5 inet static
  6. address 10.90.90.91
  7. netmask 255.255.255.0
  8. up route add -net 10.90.90.0/24 dev eth5
  9.  
  10. auto p4p1
  11. iface p4p1 inet static
  12. address 192.168.1.32
  13. netmask 255.255.255.0
  14. up route add -net 192.168.1.0/24 dev p4p1

它奏效了!看到:

  1. dor@ubuntu:~$route -n
  2. Kernel IP routing table
  3. Destination Gateway Genmask Flags Metric Ref Use Iface
  4. 0.0.0.0 10.1.0.1 0.0.0.0 UG 0 0 0 em1
  5. 10.1.0.0 0.0.0.0 255.255.248.0 U 0 0 0 em1
  6. 10.90.90.0 0.0.0.0 255.255.255.0 U 0 0 0 eth5
  7. 10.90.90.0 0.0.0.0 255.255.255.0 U 0 0 0 eth5
  8. 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 p4p1
  9. 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 p4p1

唯一的问题是那些重复.但它不会干扰任何事情,它仍然有效.

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

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

  1. up route add -net 192.168.1.0/24 dev p4p1

所以我删除了它们.现在一切都很完美.

解决方法

我认为这里的错误是你的配置文件中配置了多个默认网关.它不起作用 – 这是默认的,因为任何没有已知路由的流量都会被发送到那里传递(期望它会知道,或者拥有它自己的默认值).但它没有明确地绑定到特定的接口 – 隐含地会有接口可以到达默认网关,但其他接口不能.

所以我想你需要做的就是删除你的’网关’线,并添加你想要的单一线 – 理想情况下在正确的接口节,但我实际上并不认为它应该重要.

猜你在找的Linux相关文章