6

大家好,我是偶尔的 linux 用户,但我有一个项目要做,我需要一些关于桥接的帮助 :)
我已经尝试过 google,但没有解决问题。

我的任务是创建网络命名空间,因此它可以用来执行其他一些任务。

Debian 8.2 用于 Windows 7 上的 VMWare 虚拟机。我也在 Raspberry Pi 2 上尝试过同样的事情,但出现了同样的问题。

首先,我按照教程https://lwn.net/Articles/580893/创建了一对虚拟以太网接口。所以现在我在全局命名空间中有 veth0,IP 地址为 10.1.1.2/24,在 netns1 命名空间中有 veth1,IP 地址为 10.1.1.1/24。

接下来,我按照教程http://www.linuxfoundation.org/collaborate/workgroups/networking/bridge来桥接 eth0 和 veth0,所以我可以从 netns1 命名空间访问互联网。

  • 首先,我删除了 eth0 和 veth0 接口的 IP 地址,并将它们设置为 DOWN 状态。

  • 创建新的网桥 (br0) 并添加两个接口(eth0 和 veth0)。

  • 然后两个接口都设置为 UP 状态,我运行“dhclient br0”将 ip 地址分配给 br0。

现在可以从全局命名空间运行“ping google.com”,但从 netns1 命名空间我收到错误“网络无法访问”。(我想路由有问题,我尝试向 netns1 命名空间添加一些默认路由,但没有运气。我的网络知识很少,所以我正在寻求帮助。)

$ ip地址显示

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 00:0c:29:45:b6:1d brd ff:ff:ff:ff:ff:ff
4: veth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
    link/ether 86:e4:6c:02:b6:79 brd ff:ff:ff:ff:ff:ff
    inet6 fe80::84e4:6cff:fe02:b679/64 scope link 
       valid_lft forever preferred_lft forever
5: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 00:0c:29:45:b6:1d brd ff:ff:ff:ff:ff:ff
    inet 192.168.178.135/24 brd 192.168.178.255 scope global br0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe45:b61d/64 scope link 
       valid_lft forever preferred_lft forever

$路线

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.178.2   0.0.0.0         UG    0      0        0 br0
default         192.168.178.2   0.0.0.0         UG    1024   0        0 br0
192.168.178.0   *               255.255.255.0   U     0      0        0 br0

$ ip netns exec netns1 ip addr show

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
3: veth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether ee:b8:f3:47:f7:0c brd ff:ff:ff:ff:ff:ff
    inet 10.1.1.1/24 brd 10.1.1.255 scope global veth1
       valid_lft forever preferred_lft forever
    inet6 fe80::ecb8:f3ff:fe47:f70c/64 scope link 
       valid_lft forever preferred_lft forever

$ ip netns exec netns1 路由

Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.1.1.0        *               255.255.255.0   U     0      0        0 veth1

$ brctl 显示

bridge name    bridge id        STP enabled    interfaces
br0        8000.000c2945b61d    no        eth0
                            veth0

提前感谢您的帮助:)

4

1 回答 1

7

I have found solution.

Basically, ip forward was missing, along with 2 more steps (i have tried them before, but because of ip forward wasn't enabled, it wasn't working).

Here steps for future readers (after making bridge to work in global namespace ):

  • Assign ip address to veth0 in global namespace (10.1.1.2) because ip address was deleted before creating bridge (in tutorial for bridge they say: "The IP address needs to be set after the bridge has been configured.")
  • Assign default gateway in netns1 namespace to be veth0 in global namespace "ip netns exec netns1 route add default gw 10.1.1.2"
  • Enable ip forwarding "echo 1 > /proc/sys/net/ipv4/ip_forward"
于 2016-01-27T16:30:47.843 回答