大网数据广告活动
服务器运维问答

How to isolate two containers connected to the same bridge?

· 52CE 我爱测

I have several LXD containers which are bound to a single bridge (the default lxdbr0 or another one, it does not matter). The host itself has another two interfaces, one ultimately connecting it to Internet and another one to whi…

问题原文

原帖链接 · WoJ · CC BY-SA 3.0

I have several LXD containers which are bound to a single bridge (the default lxdbr0 or another one, it does not matter). The host itself has another two interfaces, one ultimately connecting it to Internet and another one to which a WiFi AP is attached.

The data flows are orchestrated by Shorewall (which builds the appropriate iptable rules).

The restrictions I put in place work fine for traffic between the containers to Internet or to the AP network (and between the AP network and Internet). The problem I have is that inter-container traffic does not seem to be affected by the iptable rules. Even with a plain REJECT policy for the container IP, I still manage to ping the IP of another container (and nothing else, including its own gateway - which is expected).

My understanding is that the bridge itself works on ISO/OSI layer 2. In that case firewalling would not affect the traffic.

At the same time, it has an IP, which is the gateway for the containers (the containers all have an address from the same 10.20.30.0/24 network, 10.20.30.254 being the IP of the bridge). Since this is now layer 3 communication, shouldn't the traffic go though iptables?

已采纳解答

原帖链接 · teknopaul · CC BY-SA 4.0

Instead of using a bridge, use router mode in the lxc networking. A bridge connects all the veth NICs attached to it together, by design, as if it were a physical network hub.

If you create the NICs in the containers like this.

lxc.net.0.veth.pair = vethXX
lxc.net.0.veth.mode = router

each NIC is independant.

Each container needs its own subnet, typcially lxc containers share 10.0.3.X but in this mode you need a subnet per container.

Now you can route traffic as if the veth interfaces were physical NICs and nftables firewall will work, if you do want to allow traffic between containers for something specific.

Traffic between lxc like this can be filtered with the forward chain

table ip filter {
    chain forward {
        type filter hook forward priority filter; policy drop; # defailt to block everything
                    
        # allow only what you want, e.g. ping
        icmp type echo-request accept
        icmp type echo-reply accept
    }
}

You can turn off the lxcbr0 bridge if you use router mode.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-04-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。