简体   繁体   中英

Route between network interfaces Ubuntu

I would like to turn my Ubuntu into a router. I want to route traffic between eth0 (192.168.0.0/24) interface and eth1 (10.0.0.0/8) interface. I have the following /etc/network/interfaces configuration:

# 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

route -n gives me the following output:

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/net/ipv4/ip_forward returns 1.

I cannot ping between eth1 and 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   ...

However, I can ping both ethernet interfaces from loopback.

$ 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

What do I need to do do be able to ping eth1 from eth0 and vice versa?

You cannot reach from one network to another unless you have a router. IP forwarding applies for incoming packets and makes your machine as router. What I suggest is, connect your machine with another machine back to back with two interfaces

Machine 1 eth0(192.168.0.8) <-> Machine 2 eth0 (192.168.0.9)

Machine 1 eth1(10.11.131.12) <-> Machine 2 eth1 (10.11.131.13)

Enable IP forwarding in Machine 2 between the two networks, you can ping the networks on both directions.

Your observations are based only on output of 'ping -I' command. But it looks like that it doesn't work correctly with interfaces mentioned as source in cases, when both destinations are on same box.

I have the exact same environment, where Linux host turns as a router between two NIC networks. And 'ping -I' between interfaces also doesn't succeeds, although routing functionality works fine. This option takes also an IP address:

-I interface; interface is either an address, or an interface name.

And with specifying as source different NIC's IP address - all works fine. I can see that in your environment this also is true. When you trying to ping using Lo as source, mention that one of the pings are still running between both Ethernet cards:

$ 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

Another question is your configuration. And it looks like that you've taken it in different points of time. You mentioned in reply:

My goal is to turn my Ubuntu into a router which connects 10.0.0.0/8 with 192.168.0.0/24.

And for that goal you routing table is correct, but the /etc/network/interfaces file tells different. You should clean it a little - remove duplicate gateway, dns and route entries, merely like this:

# 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

So try to perform live test by device located on one of the local networks to reach another, of course by setting its gateway as this Linux host's IP address.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM