在 OpenWrt 的 LXC 容器中运行 Debian 12

在 OpenWrt 上使用 LXC 容器运行 Debian 12 的完整步骤如下:


1. 准备工作

  • 确保 OpenWrt 已安装必要软件包:
  opkg update
  opkg install lxc lxc-common lxc-start lxc-attach bridge-utils

2. 配置 OpenWrt 网络

方法一:共享 br-lan(推荐)

  1. 编辑 /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'
  1. 重启网络:
   /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 容器

  1. 创建 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
  1. 创建容器目录和根文件系统:
   mkdir -p /var/lib/lxc/debian12/rootfs

4. 安装 Debian 12 根文件系统

方法一:使用 debootstrap(需 OpenWrt 能运行 debootstrap)

  1. 在另一台 Debian 主机上生成根文件系统:
   debootstrap bookworm /path/to/rootfs http://deb.debian.org/debian
  1. rootfs 打包并复制到 OpenWrt 的 /var/lib/lxc/debian12/

方法二:直接下载 LXC 镜像

  1. 下载预构建的 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. 启动容器

  1. 启动并进入容器:
   lxc-start -n debian12 -F
   lxc-attach -n debian12
  1. 容器内配置
  • 设置 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,并与主机共享网络或使用独立网桥。

No Comments

Send Comment Edit Comment


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
Previous
Next