Ubuntu

在 Ubuntu 上為兩個外部 IP 配置雙介面

  • October 5, 2016

我正在嘗試為我們的 SIP 電話將第二個 ISP 連接連接到我的伺服器。

eth0在192.168.1.x上有我們預設的 25Mb 服務,我已經配置了

eth1新的 6Mb 服務為10.10.1.x

兩台路由器都配置了具有唯一靜態 IP 的 pppoe,我有單獨的交換機,以及一台帶有兩個乙太網埠的 Ubuntu 14Lts 伺服器。單獨連接的任何一個都可以正常工作,但是兩者都可以,我只能ping -I eth[0|1] 8.8.8.8 -c 3在一個介面上但是ping每個路由器沒有問題。

如果有人可以幫助我讓 Nic 啟動並執行並預設主要流量通過eth0和 Just Asterisk/Sip 流量通過eth1將節省我的一天。

一些資訊輸出:

# cat /etc/network/interfaces

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.100
gateway 192.168.1.1
netmask 255.255.255.0

auto eth1
iface eth1 inet static
address 10.10.1.101
#gateway 10.10.1.1
netmask 255.255.255.0

$ sudo route

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         home.Home       0.0.0.0         UG    0      0        0 eth1
10.10.1.0       *               255.255.255.0   U     1      0        0 eth1
link-local      *               255.255.0.0     U     1000   0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth0
192.168.122.0   *               255.255.255.0   U     0      0        0 virbr0

$ sudo ifdown eth0 && sudo ifup eth0

RTNETLINK answers: No such process
RTNETLINK answers: File exists
Failed to bring up eth0.

$ sudo ifdown eth1 && sudo ifup eth1

ifdown: interface eth1 not configured
RTNETLINK answers: File exists
Failed to bring up eth1.

$ ifconfig

eth0      Link encap:Ethernet  HWaddr 00:25:90:11:c8:44  
         inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0
         inet6 addr: fe80::225:90ff:fe11:c844/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:2316517 errors:0 dropped:0 overruns:0 frame:0
         TX packets:3877952 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:1881181270 (1.8 GB)  TX bytes:5573716194 (5.5 GB)
         Interrupt:16 Memory:faee0000-faf00000 

eth1      Link encap:Ethernet  HWaddr 00:25:90:11:c8:45  
         inet addr:10.10.1.101  Bcast:10.10.1.255  Mask:255.255.255.0
         inet6 addr: fe80::225:90ff:fe11:c845/64 Scope:Link
         UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
         RX packets:214622 errors:0 dropped:0 overruns:0 frame:0
         TX packets:9763 errors:0 dropped:0 overruns:0 carrier:0
         collisions:0 txqueuelen:1000 
         RX bytes:13988405 (13.9 MB)  TX bytes:890576 (890.5 KB)
         Interrupt:16 Memory:fafe0000-fb000000 

======================編輯1: ========================= ==

感謝 Peter Zhabin 的 iproute2 說明

apt-get install iproute2
echo "1 link2" >> /etc/iproute2/rt_tables
ip route add 10.10.1.0/24 dev eth1 src 10.10.1.101 table link2
ip route add default via 10.10.1.1 dev eth1 table link2
ip rule add from 10.10.1.101/32 table link2
ip rule add to 10.10.1.101/32 table link2

我第一次嘗試時有點奇怪sudo echo "1 link2" >> /etc/iproute2/rt_tables-bash: /etc/iproute2/rt_tables: Permission denied 但我切換到 Root 以避免需要 sudo 並且沒有進一步的投訴。

======================編輯2: ========================= ==

# ping -I 10.10.1.101 8.8.8.8 -c 3

PING 8.8.8.8 (8.8.8.8) from 10.10.1.101 : 56(84) bytes of data.

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 2014ms

======================編輯3: ======================== ==

# ip rule show

0:      from all lookup local 
32764:  from all to 10.10.1.101 lookup link2 
32765:  from 10.10.1.101 lookup link2 
32766:  from all lookup main 
32767:  from all lookup default 

# ip route show

default via 192.168.1.1 dev eth0 
10.10.1.0/24 dev eth1  proto kernel  scope link  src 10.10.1.101 
169.254.0.0/16 dev eth1  scope link  metric 1000 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.100 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 

# ip route show table link2

default via 10.10.1.1 dev eth1 
10.10.1.0/24 dev eth1  scope link  src 10.10.1.101 

顯示link2 表奇怪的是有點碰巧。我不明白為什麼它並不總是列印。好消息是通過 IP 進行 Ping 工作,儘管 eth1 沒有。為了完成,我還包括完整的展示表

# ip route show table all

default via 10.10.1.1 dev eth1  table link2 
10.10.1.0/24 dev eth1  table link2  scope link  src 10.10.1.101 
default via 192.168.1.1 dev eth0 
10.10.1.0/24 dev eth1  proto kernel  scope link  src 10.10.1.101 
169.254.0.0/16 dev eth1  scope link  metric 1000 
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.100 
192.168.122.0/24 dev virbr0  proto kernel  scope link  src 192.168.122.1 
broadcast 10.10.1.0 dev eth1  table local  proto kernel  scope link  src 10.10.1.101 
local 10.10.1.101 dev eth1  table local  proto kernel  scope host  src 10.10.1.101 
broadcast 10.10.1.255 dev eth1  table local  proto kernel  scope link  src 10.10.1.101 
broadcast 127.0.0.0 dev lo  table local  proto kernel  scope link  src 127.0.0.1 
local 127.0.0.0/8 dev lo  table local  proto kernel  scope host  src 127.0.0.1 
local 127.0.0.1 dev lo  table local  proto kernel  scope host  src 127.0.0.1 
broadcast 127.255.255.255 dev lo  table local  proto kernel  scope link  src 127.0.0.1 
broadcast 192.168.1.0 dev eth0  table local  proto kernel  scope link  src 192.168.1.100 
local 192.168.1.100 dev eth0  table local  proto kernel  scope host  src 192.168.1.100 
broadcast 192.168.1.255 dev eth0  table local  proto kernel  scope link  src 192.168.1.100 
broadcast 192.168.122.0 dev virbr0  table local  proto kernel  scope link  src 192.168.122.1 
local 192.168.122.1 dev virbr0  table local  proto kernel  scope host  src 192.168.122.1 
broadcast 192.168.122.255 dev virbr0  table local  proto kernel  scope link  src 192.168.122.1 
fe80::/64 dev eth0  proto kernel  metric 256 
fe80::/64 dev eth1  proto kernel  metric 256 
unreachable default dev lo  table unspec  proto kernel  metric 4294967295  error -101
local ::1 dev lo  table local  proto none  metric 0 
local fe80::225:90ff:fe11:c844 dev lo  table local  proto none  metric 0 
local fe80::225:90ff:fe11:c845 dev lo  table local  proto none  metric 0 
ff00::/8 dev eth0  table local  metric 256 
ff00::/8 dev eth1  table local  metric 256 
unreachable default dev lo  table unspec  proto kernel  metric 4294967295  error -101

# ping -I eth1 8.8.8.8 -c 3

PING 8.8.8.8 (8.8.8.8) from 10.10.1.101 eth1: 56(84) bytes of data.
From 10.10.1.101 icmp_seq=1 Destination Host Unreachable
From 10.10.1.101 icmp_seq=2 Destination Host Unreachable
From 10.10.1.101 icmp_seq=3 Destination Host Unreachable

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2015ms
pipe 3

# ping -I 10.10.1.101 8.8.8.8 -c 3

PING 8.8.8.8 (8.8.8.8) from 10.10.1.101 : 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=60 time=29.4 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=60 time=29.3 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=60 time=28.8 ms

--- 8.8.8.8 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 28.804/29.192/29.433/0.310 ms

首先,您必須為通過這些網關入站的數據包正確設置返迴路徑。這意味著如果有一個數據包通過 eth1 進來,響應也應該通過 eth1 而不是通過預設路由出去。

要讓它工作,您需要 iproute2,它允許您擁有多個路由表和多個網關。首先,您應該在網路/介面下只設置一個網關地址,假設它是 192.168.1.100 網路,並且您希望來自 10.10.1.101 的流量通過 eth1 通過 10.10.1.1 路由器流出。

然後其餘的將是這樣的(根據需要添加 sudo):

apt-get install iproute2
echo "1 link2" >> /etc/iproute2/rt_tables
ip route add 10.10.1.0/24 dev eth1 src 10.10.1.101 table link2
ip route add default via 10.10.1.1 dev eth1 table link2
ip rule add from 10.10.1.101/32 table link2
ip rule add to 10.10.1.101/32 table link2

這將使您的返回流量執行到正確的路由器,並且您還可以 ping -I 沒有問題的兩個介面。上面確實創建了第二個路由表(link2),為其設置了預設路由和範圍,並添加了兩條規則來處理入站和出站流量。

如果您的手機從外部連接到此伺服器,那就足夠了。如果它們在內部某個地方,請評論有關您的內部網路設置的更多詳細資訊。

引用自:https://serverfault.com/questions/806456