lxc 虚拟化运行openwrt 并ppoe拨号
🎉 恭喜!历经千辛万苦,终于大功告成!
经过两天的鏖战,你从零开始,排除了网络配置错误、LXC 配置不当、权限不足、双重桥接等一系列问题,最终成功在 Armbian 上通过 LXC 运行 OpenWrt,并实现了 PPPoE 拨号上网。以下是完整的最终配置和避坑指南,希望能帮到后来人,也方便你自己回顾。
一、最终网络拓扑
光猫
│
├─ 宿主机 eth0 (物理网卡) ← 物理直通给容器
│
宿主机 ArmBIAN (192.168.2.2)
├─ br-lan 网桥
│ ├─ eth1 (物理网卡) ← 连接内网交换机
│ └─ veth 对端 (连接容器 eth1)
│
└─ OpenWrt 容器 (LXC)
├─ eth0 (物理直通) ← PPPoE 拨号
├─ eth1 (veth 连接 br-lan) ← LAN 口 (192.168.2.1)
└─ 防火墙 (iptables) ← NAT 转发
二、完整配置文件
1. 宿主机 LXC 容器配置
文件路径: /var/lib/lxc/openwrt/config
# OpenWrt LXC 容器配置
lxc.include = /usr/share/lxc/config/common.conf
lxc.arch = arm64
lxc.uts.name = openwrt
# 根文件系统
lxc.rootfs.path = dir:/var/lib/lxc/openwrt/rootfs
# WAN 口:物理直通 eth0(关键!避免网桥干扰)
lxc.net.0.type = phys
lxc.net.0.link = eth0
lxc.net.0.name = eth0
lxc.net.0.flags = up
# LAN 口:veth 连接到宿主机的 br-lan 网桥
lxc.net.1.type = veth
lxc.net.1.link = br-lan
lxc.net.1.name = eth1
lxc.net.1.flags = up
# PPP 设备授权(PPPoE 必须)
lxc.cgroup2.devices.allow = c 108:0 rwm
lxc.mount.entry = /dev/ppp dev/ppp none bind,create=file
# 基础设备挂载
lxc.mount.auto = proc:mixed sys:ro cgroup:mixed
# 自动启动
lxc.start.auto = 1
lxc.start.delay = 5
2. 宿主机网络配置
文件路径: /etc/network/interfaces.d/br-lan
# LAN 网桥配置
auto br-lan
iface br-lan inet static
bridge_ports eth1
bridge_stp off
bridge_fd 0
address 192.168.2.2/24
# 网关由 OpenWrt 提供,这里不设 gateway
dns-nameservers 114.114.114.114 223.5.5.5
注意: 不需要为
br-wan创建配置文件,因为 WAN 口已被容器独占。
3. OpenWrt 容器内网络配置
文件路径: /etc/config/network
config interface 'loopback'
option device 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'
config globals 'globals'
option ula_prefix 'fd65:f338:d77::/48'
# LAN 口:直接使用 eth1(避免双重桥接!)
config interface 'lan'
option device 'eth1'
option proto 'static'
option ipaddr '192.168.2.1'
option netmask '255.255.255.0'
option ip6assign '60'
# WAN 口:PPPoE 拨号
config interface 'wan'
option proto 'pppoe'
option username '0510110776579'
option password '787495'
option ipv6 'auto'
option device 'eth0'
option ip6assign '60'
config interface 'wan6'
option device 'eth0'
option proto 'dhcpv6'
4. OpenWrt DHCP 配置(可选)
文件路径: /etc/config/dhcp
config dnsmasq
option domainneeded '1'
option boguspriv '1'
option filterwin2k '0'
option localise_queries '1'
option rebind_protection '1'
option rebind_localhost '1'
option local '/lan/'
option domain 'lan'
option expandhosts '1'
option authoritative '1'
option leasefile '/tmp/dhcp.leases'
option resolvfile '/tmp/resolv.conf.d/resolv.conf.auto'
config dhcp 'lan'
option interface 'lan'
option start '100'
option limit '150'
option leasetime '12h'
config dhcp 'wan'
option interface 'wan'
option ignore '1'
5. OpenWrt 防火墙配置(NAT 必须)
文件路径: /etc/config/firewall
config defaults
option syn_flood 1
option input REJECT
option output ACCEPT
option forward REJECT
option flow_offloading 1
option flow_offloading_hw 1
option fullcone 1
config zone
option name lan
list network 'lan'
option input ACCEPT
option output ACCEPT
option forward ACCEPT
config zone
option name wan
list network 'wan'
list network 'wan6'
option input REJECT
option output ACCEPT
option forward REJECT
option masq 1 # 关键!启用 NAT
option mtu_fix 1
config forwarding
option src lan
option dest wan
# 基础放行规则(DHCP、ICMP 等)
config rule
option name Allow-DHCP-Renew
option src wan
option proto udp
option dest_port 68
option target ACCEPT
option family ipv4
config rule
option name Allow-Ping
option src wan
option proto icmp
option icmp_type echo-request
option target ACCEPT
注意: 如果
iptables未安装,会导致 NAT 不生效。安装方法:opkg update opkg install iptables iptables-mod-nat kmod-ipt-nat /etc/init.d/firewall restart
三、遇到的坑与解决方案
| 坑 | 现象 | 解决方案 |
|---|---|---|
| 双重桥接 | LAN 口无法访问,PPPoE 拨号失败 | 容器内 LAN 口直接使用 eth1,不再创建 br-lan 网桥 |
| WAN 口在宿主机网桥上 | PADI 广播被容器内部拦截,拨号超时 | WAN 口改用 lxc.net.0.type = phys 物理直通,不经过宿主机网桥 |
| /dev/ppp 权限不足 | PPPoE 发现成功,但最后一步 Operation not permitted |
LXC 配置中添加 lxc.cgroup2.devices.allow 和 lxc.mount.entry |
| 防火墙规则缺失 | 能拨号能 ping 外网,但内网设备无法上网 | 安装 iptables 并确认 masq=1 生效 |
| 网口 VLAN 需求 | PPPoE 发现无响应 | 测试 VLAN 41:ip link add link eth0 name eth0.41 type vlan id 41 |
| OpenWrt 默认创建 br-lan | 自动创建内部网桥导致双重桥接 | 手动删除 /etc/config/network 中的 config device 段 |
四、常用维护命令
容器管理
lxc-ls -f # 查看容器状态
lxc-attach -n openwrt # 进入容器
lxc-stop -n openwrt -k # 强制停止
lxc-start -n openwrt # 启动容器
网络诊断(容器内)
ip addr show
ip route show
logread -f | grep ppp # 查看拨号日志
pppoe-discovery -I eth0 # 测试 PPPoE 发现
iptables -t nat -L POSTROUTING -v # 查看 NAT 规则
重启网络
/etc/init.d/network restart # 容器内重启网络
/etc/init.d/firewall restart # 重启防火墙
systemctl restart networking # 宿主机重启网络
五、最终效果
- ✅ WAN 口:
eth0物理直通,PPPoE 成功拨号,获取公网 IP - ✅ LAN 口:
eth1连接宿主机br-lan,IP192.168.2.1 - ✅ 宿主机:
192.168.2.2可 SSH 管理 - ✅ 内网设备:连接
eth1口,自动获取192.168.2.100-250,网关192.168.2.1 - ✅ 外网访问:NAT 正常,内网设备可上网
- ✅ 容器上网:
opkg update正常,可安装软件
六、致谢
恭喜你坚持到了最后!这个配置不仅让你获得了一个功能完整的软路由,更重要的是,你深入理解了 LXC、网络桥接、PPPoE 协议、防火墙规则等底层知识。这份经历的价值远超一个能用的系统。
感谢你的耐心和坚持! 🎉
如果想把eth0使用veth + br-wan方式 我来详细解释一下它们的作用和注意事项:
配置文件详解
WAN 网桥配置 (br-wan)
auto br-wan # 开机自动启动
iface br-wan inet manual # 不分配 IP(因为要给容器用)
bridge_ports eth0 # 将物理网卡 eth0 加入网桥
bridge_stp off # 关闭生成树协议(单网桥不需要)
bridge_fd 0 # 关闭转发延迟
LAN 网桥配置 (br-lan)
auto br-lan # 开机自动启动
iface br-lan inet static # 静态 IP(宿主机管理地址)
bridge_ports eth1 # 将物理网卡 eth1 加入网桥
bridge_stp off # 关闭生成树协议
bridge_fd 0 # 关闭转发延迟
address 192.168.2.2/24 # 宿主机管理 IP
完整版创建网桥配置文件
# 创建 WAN 网桥配置(用于容器 WAN 口)
cat > /etc/network/interfaces.d/br-wan << 'EOF'
# WAN 网桥 - 用于容器 PPPoE 拨号
auto br-wan
iface br-wan inet manual
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
EOF
# 创建 LAN 网桥配置(用于容器 LAN 口和宿主机管理)
cat > /etc/network/interfaces.d/br-lan << 'EOF'
# LAN 网桥 - 用于容器 LAN 口和宿主机管理
auto br-lan
iface br-lan inet static
bridge_ports eth1
bridge_stp off
bridge_fd 0
bridge_maxwait 0
address 192.168.2.2/24
# 网关由 OpenWrt 提供,这里不设置 gateway
dns-nameservers 114.114.114.114 223.5.5.5
EOF
# 重启网络服务使配置生效
systemctl restart networking
# 验证网桥创建成功
echo "=== 网桥状态 ==="
brctl show
echo "=== br-lan IP ==="
ip addr show br-lan | grep inet
一键执行版(直接复制粘贴)
cat > /etc/network/interfaces.d/br-wan << 'EOF' && cat > /etc/network/interfaces.d/br-lan << 'EOF' && systemctl restart networking && echo "=== 网桥状态 ===" && brctl show && echo "=== br-lan IP ===" && ip addr show br-lan | grep inet
# WAN 网桥 - 用于容器 PPPoE 拨号
auto br-wan
iface br-wan inet manual
bridge_ports eth0
bridge_stp off
bridge_fd 0
bridge_maxwait 0
EOF
# LAN 网桥 - 用于容器 LAN 口和宿主机管理
auto br-lan
iface br-lan inet static
bridge_ports eth1
bridge_stp off
bridge_fd 0
bridge_maxwait 0
address 192.168.2.2/24
dns-nameservers 114.114.114.114 223.5.5.5
EOF
执行后,你会看到网桥状态和 br-lan 的 IP 地址,确认配置成功。
应用配置后的状态
执行完你的命令后,系统会:
- 创建两个网桥:
br-wan和br-lan - 将物理网卡加入网桥:
eth0→br-wan(WAN 口,准备给容器用)eth1→br-lan(LAN 口,宿主机管理)
- 给
br-lan配置 IP:192.168.2.2/24 - 重启网络服务使配置生效
验证配置
# 查看网桥状态
brctl show
# 应该看到:
# br-wan eth0
# br-lan eth1
# 查看 IP
ip addr show br-lan
# 应该看到 192.168.2.2/24
与 LXC 配置的配合
有了这两个网桥,你的 LXC 配置就可以这样写:
# WAN 口:连接到 br-wan 网桥
lxc.net.0.type = veth
lxc.net.0.link = br-wan
lxc.net.0.name = eth0
lxc.net.0.flags = up
# LAN 口:连接到 br-lan 网桥
lxc.net.1.type = veth
lxc.net.1.link = br-lan
lxc.net.1.name = eth1
lxc.net.1.flags = up
完美!
你现在有了:
- ✅ 完整的网桥配置
- ✅ 清晰的网络隔离
- ✅ 持久化的配置(重启不丢失)
- ✅ 宿主机管理 IP(192.168.2.2)
这就是 LXC + OpenWrt 的标准配置模板!