在 OpenWrt 上使用 LXC 容器运行 Debian 12 的完整步骤如下:
1. 准备工作
- 确保 OpenWrt 已安装必要软件包:
opkg update
opkg install lxc lxc-common lxc-start lxc-attach bridge-utils
2. 配置 OpenWrt 网络
方法一:共享 br-lan(推荐)
- 编辑
/etc/config/network,确保br-lan已正确配置:
config device
option name 'br-lan'
option type 'bridge'
list ports 'eth0' # 根据实际接口调整
config interface 'lan'
option device 'br-lan'
option proto 'static'
option ipaddr '192.168.1.1' # 你的 LAN IP
option netmask '255.255.255.0'
- 重启网络:
/etc/init.d/network restart
方法二:创建新网桥(可选)
若需独立网桥,新增 br-lxc:
config device
option name 'br-lxc'
option type 'bridge'
config interface 'lxc'
option device 'br-lxc'
option proto 'none'
重启网络后生效。
3. 配置 LXC 容器
- 创建 LXC 配置模板:
mkdir -p /etc/lxc/
cat > /etc/lxc/debian12.conf <<EOF
lxc.include = /usr/share/lxc/config/debian.common.conf
lxc.arch = linux64
lxc.rootfs.path = /var/lib/lxc/debian12/rootfs
# 网络配置(共享 br-lan)
lxc.net.0.type = veth
lxc.net.0.link = br-lan
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:XX:XX:XX # 随机生成 MAC
EOF
- 创建容器目录和根文件系统:
mkdir -p /var/lib/lxc/debian12/rootfs
4. 安装 Debian 12 根文件系统
方法一:使用 debootstrap(需 OpenWrt 能运行 debootstrap)
- 在另一台 Debian 主机上生成根文件系统:
debootstrap bookworm /path/to/rootfs http://deb.debian.org/debian
- 将
rootfs打包并复制到 OpenWrt 的/var/lib/lxc/debian12/。
方法二:直接下载 LXC 镜像
- 下载预构建的 Debian 12 LXC 镜像:
wget https://cloud-images.ubuntu.com/releases/bookworm/release/debian-12-standard-amd64.tar.gz
tar -xzf debian-12-standard-amd64.tar.gz -C /var/lib/lxc/debian12/rootfs
5. 启动容器
- 启动并进入容器:
lxc-start -n debian12 -F
lxc-attach -n debian12
- 容器内配置:
- 设置 root 密码:
bash passwd - 配置网络(若未通过 DHCP 获取 IP):
bash echo "auto eth0" >> /etc/network/interfaces echo "iface eth0 inet dhcp" >> /etc/network/interfaces /etc/init.d/networking restart
6. 验证网络
- 在容器内测试网络:
ping 8.8.8.8
- 若需静态 IP,在容器内编辑
/etc/network/interfaces:
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
7. 开机自启
在 OpenWrt 的 /etc/rc.local 添加:
lxc-start -n debian12 -d
常见问题
- 网络不通:检查
br-lan是否允许混杂模式:
ifconfig br-lan promisc
- 权限错误:确保容器配置中已禁用特权:
lxc.apparmor.profile = unconfined
通过以上步骤,你可以在 OpenWrt 的 LXC 容器中运行 Debian 12,并与主机共享网络或使用独立网桥。