2017-09-04 377 views
0

我想將我的Ubuntu變成一個路由器。我想在eth0(192.168.0.0/24)接口和eth1(10.0.0.0/8)接口之間路由流量。我有以下的/ etc /網絡/接口的配置:網絡接口之間的路由Ubuntu

# The loopback network interface 
auto lo 
iface lo inet loopback 

# The primary network interface 
auto eth0 
iface eth0 inet static 
address 192.168.0.8 
netmask 255.255.255.0 
broadcast 192.168.0.255 
gateway 192.168.0.1 
dns-nameservers 8.8.8.8 8.8.4.4 
up route add -net 10.74.0.0/16 gateway 10.11.131.1 eth1 

# ETH1 
auto eth1 
iface eth1 inet static 
address 10.11.131.12 
netmask 255.0.0.0 
broadcast 10.255.255.255 
gateway 10.11.131.1 
dns-nameservers 8.8.8.8 8.8.4.4 
up route add -net 8.8.0.0/16 gateway 192.168.0.1 eth0 
up route add -net 10.74.0.0/16 gateway 10.11.131.1 eth1 

路線-n給我下面的輸出:

Kernel IP routing table 
Destination  Gateway   Genmask   Flags Metric Ref Use 
Iface 
0.0.0.0   192.168.0.1  0.0.0.0   UG 0  0  0 eth0 
10.0.0.0  0.0.0.0   255.0.0.0  U  0  0  0 eth1 
10.74.0.0  10.11.131.1  255.255.0.0  UG 0  0  0 eth1 
192.168.0.0  0.0.0.0   255.255.255.0 U  0  0  0 eth0 

執行cat/proc/SYS /網/的IPv4/IP_FORWARD返回1

我不能eth1的和爲eth0之間平:

$ ping 192.168.0.8 -I eth1 
PING 192.168.0.8 (192.168.0.8) from 10.11.131.12 eth1: 56(84) bytes of data. 
From 10.11.131.12 icmp_seq=1 Destination Host Unreachable 

$ ping 10.11.131.12 -I eth0 
PING 10.11.131.12 (10.11.131.12) from 192.168.0.8 eth0: 56(84) bytes of data. 
... no response ... 

不過,我可以來回ping通兩個以太網接口m回送。

$ ping 10.11.131.12 -I lo 
ping: Warning: source address might be selected on device other than lo. 
PING 10.11.131.12 (10.11.131.12) from 192.168.0.8 lo: 56(84) bytes of data. 
64 bytes from 10.11.131.12: icmp_seq=1 ttl=64 time=0.032 ms 

$ ping 192.168.0.8 -I lo 
ping: Warning: source address might be selected on device other than lo. 
PING 192.168.0.8 (192.168.0.8) from 192.168.0.8 lo: 56(84) bytes of data. 
64 bytes from 192.168.0.8: icmp_seq=1 ttl=64 time=0.032 ms 

我需要做什麼才能從eth0 ping eth1,反之亦然?

回答

0

除非有路由器,否則無法從一個網絡到達另一個網絡。 IP轉發適用於傳入數據包,並使您的機器成爲路由器。我的建議是,與另一臺機器連接設備,回用兩個接口

機1的eth0(192.168.0.8)<備份 - >機2的eth0(192.168.0.9)

機1的eth1(10.11。 >機器2 eth1(10.11.131.13)

啓用兩臺網絡之間的機器2中的IP轉發,您可以在兩個方向上ping網絡。

+0

我的目標是將我的Ubuntu變成一個連接10.0.0.0/8與192.168.0.0/24的路由器。我可能需要最終配置NAT,但在設置NAT之前,我應該能夠在接口之間進行ping操作。 – Ljung

+0

在這種情況下,您的配置應該工作。由於您的機器設置了IP轉發功能,因此當數據包到達時,您的機器將路由並將其發送到另一個接口,並最終出來。但是,當您從eth1 ping eth0時,您的數據包不會通過您的路由器(IP轉發模塊)而是通過線路傳輸。 – Deepaklal

+0

它可以作爲路由器使用!謝謝! – Ljung

0

您的觀察結果僅基於'ping -I'命令的輸出。但它看起來像是在兩種目標位於同一個盒子的情況下,不能正確使用作爲源提及的接口。

我有完全相同的環境,Linux主機在兩個NIC網絡之間轉變爲路由器。儘管路由功能正常工作,但接口之間的'ping -I'也不成功。此選項還需要IP地址:

-I interface; 接口可以是地址,也可以是接口名稱。

並指定作爲源不同的NIC的IP地址 - 一切正常。我可以看到,在你的環境中,這也是事實。當您嘗試使用羅作爲源平提的是,坪的一個仍在兩個以太網卡之間運行:

$ ping 10.11.131.12 -I lo 
ping: Warning: source address might be selected on device other than lo. 
PING 10.11.131.12 (10.11.131.12) from 192.168.0.8 lo: 56(84) bytes of data. 
64 bytes from 10.11.131.12: icmp_seq=1 ttl=64 time=0.032 ms 

另一個問題是,你的配置。看起來你已經在不同的時間點拍攝了它。您在答覆中提到:

我的目標是把我的Ubuntu成與192.168.0.0/24連接10.0.0.0/8 路由器。

爲此目標,您的路由表是正確的,但是/ etc/network/interfaces文件告訴不同。你應該好好打掃一下了一點 - 刪除重複的網關,DNS和路由表項,僅僅是這樣的:

# The loopback network interface 
auto lo 
iface lo inet loopback 

# The primary network interface 
auto eth0 
iface eth0 inet static 
address 192.168.0.8 
netmask 255.255.255.0 
broadcast 192.168.0.255 
gateway 192.168.0.1 
dns-nameservers 8.8.8.8 8.8.4.4 
# Here you have 192.168.0.0/24 directly connected, so no need to add route 

# ETH1 
auto eth1 
iface eth1 inet static 
address 10.11.131.12 
netmask 255.0.0.0 
broadcast 10.255.255.255 
up route add -net 10.0.0.0/8 gateway 10.11.131.1 eth1 

所以試圖通過位於本地網絡中的一個設備通過設置進行現場測試,達到另一個,當然它的網關作爲這個Linux主機的IP地址。