Armbian部署LXC环境以及mihomo、openwrt
懒人初级目标
安装lxc环境
XML/HTML代码
- apt-get -y install lxc
检查
XML/HTML代码
- lxc-checkconfig

修改lxc默认的一些配置
/etc/lxc/default.conf
XML/HTML代码
- nano /etc/lxc/default.conf

可根据自己习惯,在基础配置里增加一些其他的设定
禁用lxcbr0启动/etc/default/lxc-net
XML/HTML代码
- nano /etc/default/lxc-net

创建第一个lxc
基于debian的mihomo
XML/HTML代码
- lxc-create --name mihomo --template download -- --dist debian --release bookworm --arch arm64 --server mirrors.bfsu.edu.cn/lxc-images

修改配置
XML/HTML代码
- nano /var/lib/lxc/mihomo/config

使用宿主系统网桥

基础环境
XML/HTML代码
- apt install -y openssh-server nano curl wget git apt-transport-https ca-certificates
退出该lxc
XML/HTML代码
- exit
配置LXC 内的debian 网络
XML/HTML代码
- nano /etc/systemd/network/eth0.network
XML/HTML代码
- [Match]
- Name=eth0
- [Network]
- Address=10.10.10.10/24
- DNS=10.10.10.1
- IPForward=yes
- IPv6AcceptRA=true
- [Route]
- Gateway=10.10.10.1
启、停服务
XML/HTML代码
- # 查看网络服务的状态
- systemctl status systemd-networkd
- # 启动网络服务
- systemctl start systemd-networkd
- # 停止网络服务
- systemctl stop systemd-networkd
- # 重启网络服务
- systemctl restart systemd-networkd
- # 设置开机启动网络服务
- systemctl enable systemd-networkd
- # 禁止开机启动网络服务
- systemctl disable systemd-networkd
具体操作 mihomo
XML/HTML代码
- apt -y install curl nano wget git apt-transport-https ca-certificates apt-utils
- wget https://github.com/MetaCubeX/mihomo/releases/download/Prerelease-Alpha/mihomo-linux-arm64-alpha-6399347.gz
- gzip -d mihomo-linux-arm64-alpha-6399347.gz
- chmod 777 mihomo-linux-arm64-alpha-6399347
- mv mihomo-linux-arm64-alpha-6399347 /usr/local/bin/mihomo
- mkdir /etc/mihomo
- nano /etc/mihomo/config.yaml ## 使用官方推荐配置或者自己按照官方例子填写
- git clone https://github.com/metacubex/metacubexd.git -b gh-pages /etc/mihomo/ui
- nano /etc/systemd/system/mihomo.service ###官方文档填写
- systemctl daemon-reload
- systemctl enable mihomo
- systemctl start mihomo
或者自行了解
第二个lxc openwrt

使用flippy openwrt rootfs
解压到
XML/HTML代码
- tar -zxf openwrt_lxc_template_R24.01.26-flippy-87+-rootfs.tar.gz -C /var/lib/lxc/openwrt/rootfs/
配置网络
XML/HTML代码
- nano /var/lib/lxc/openwrt/config
XML/HTML代码
- # Distribution configuration
- lxc.include = /usr/share/lxc/config/common.conf
- lxc.arch = aarch64
- # Container specific configuration
- lxc.apparmor.profile = generated
- lxc.apparmor.allow_nesting = 1
- lxc.rootfs.path = dir:/var/lib/lxc/openwrt/rootfs
- lxc.uts.name = openwrt
- # Network configuration eth0
- lxc.net.0.type = veth
- lxc.net.0.link = vmbr0
- lxc.net.0.flags = up
- # Network configuration eth1
- lxc.net.1.type = veth
- lxc.net.1.link = vmbr0
- lxc.net.1.flags = up
- # PPPoe
- lxc.cgroup2.devices.allow = c 108:0 rwm
- lxc.mount.entry = /dev/ppp dev/ppp none bind,create=file
- # TUN
- lxc.cgroup2.devices.allow = c 10:200 rwm
- lxc.mount.entry = /dev/net/tun dev/net/tun none bind,create=file
- # 启用N1 的无线网卡
- lxc.net.2.type = phys
- lxc.net.2.link = wlan0
- lxc.net.2.flags = up
- lxc.net.2.name = wlan0
启动openwrt
XML/HTML代码
- lxc-start openwrt
XML/HTML代码
- lxc-attach openwrt
退出
XML/HTML代码
- exit
XML/HTML代码
- nano /etc/config/network

配置好后
输入修改后的IP 地址
在这个rootfs内启用N1的无线网卡

配置文件中添加
XML/HTML代码
- # 启用N1 的无线网卡
- lxc.net.2.type = phys
- lxc.net.2.link = wlan0
- lxc.net.2.flags = up
- lxc.net.2.name = wlan0
保存并重启这个lxc


第三个lxc 自定义openwrt
以官方openwrt举例
下载rootfs
XML/HTML代码
- wget https://downloads.openwrt.org/releases/23.05.2/targets/armsr/armv8/openwrt-23.05.2-armsr-armv8-rootfs.tar.gz
解压到lxc目录内,oWrt自己手动创建
XML/HTML代码
- tar -xzf openwrt-23.05.2-armsr-armv8-rootfs.tar.gz -C /var/lib/lxc/oWrt/rootfs
增加配置文件
XML/HTML代码
- nano /var/lib/lxc/oWrt/config

XML/HTML代码
- # Distribution configuration
- lxc.include = /usr/share/lxc/config/common.conf
- lxc.arch = aarch64
- # Container specific configuration
- lxc.apparmor.profile = generated
- lxc.apparmor.allow_nesting = 1
- lxc.rootfs.path = dir:/var/lib/lxc/oWrt/rootfs
- lxc.uts.name = openwrt
- # Network configuration eth0
- lxc.net.0.type = veth
- lxc.net.0.link = vmbr0
- lxc.net.0.flags = up
- # Network configuration eth1 根据自身需求决定是否添加该项
- lxc.net.1.type = veth
- lxc.net.1.link = vmbr0
- lxc.net.1.flags = up
- # PPPoe 根据自身需求决定是否添加该项
- lxc.cgroup2.devices.allow = c 108:0 rwm
- lxc.mount.entry = /dev/ppp dev/ppp none bind,create=file
- # TUN 根据自身需求决定是否添加该项
- lxc.cgroup2.devices.allow = c 10:200 rwm
- lxc.mount.entry = /dev/net/tun dev/net/tun none bind,create=file
启动并进入
XML/HTML代码
- lxc-start oWrt
- lxc-attach oWrt
修改root密码
XML/HTML代码
- passwd
修改openwrt网络
XML/HTML代码
- vi /etc/config/network
重启网络
XML/HTML代码
- /etc/init.d/network restart

浏览器访问你修改的IP

如果要拨号,删除这个接口

稍微配置一下 网关、DNS 根据自己实际情况来
网络诊断一下,没问题的话进行下一步

配置基础换进
仓库里写了三个非常简陋的基础脚本,凑合用用。
官方openwrt安装中文、和设置时区和一些基础环境的,脚本。
XML/HTML代码
- wget -O - https://raw.githubusercontent.com/cooip-jm/About-openwrt/main/bash.sh | sh -s -- -v
运行完后
这个用于拨号足够了,非常轻量化
passwall 官方openwrt安装 仅限ARM
XML/HTML代码
- wget -O - https://raw.githubusercontent.com/cooip-jm/About-openwrt/main/install-passwall.sh | sh -s -- -v


openclash 官方openwrt安装
XML/HTML代码
- wget -O - https://raw.githubusercontent.com/cooip-jm/About-openwrt/main/openclash-install.sh | sh -s -- -v
额外的一些配置,比如v6 DNS
计划任务重,自动更新openwrt软件包
XML/HTML代码
- opkg update && opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade
lxc 自动启动
在配置文件里增加
XML/HTML代码
- lxc.start.auto = 1
如果要所有的lxc都自动启动
运行 lxc-autostart --all
配置
宿主系统
XML/HTML代码
- nano /etc/systemd/system/lxc-start.service
XML/HTML代码
- [Unit]
- Description="LXC autostart for lxc user"
- [Service]
- ExecStartPre=/bin/sleep 15
- ExecStart=/usr/bin/lxc-autostart --all
- [Install]
- WantedBy=default.target

启用服务
XML/HTML代码
- systemctl daemon-reload
- systemctl enable lxc-start.service
- systemctl start lxc-start.service
- systemctl status lxc-start.service

关于debian/ubuntu 或使用(systemd)journalctl在 lxc中的日志相关操作
查看日志占用
XML/HTML代码
- journalctl --disk-usage
清理方法可以采用按照日期清理,或者按照允许保留的容量清理
XML/HTML代码
- journalctl --vacuum-time=2d
- journalctl --vacuum-size=500M
如果要手工删除日志文件,则在删除前需要先轮转一次journal日志
XML/HTML代码
- systemctl kill --kill-who=main --signal=SIGUSR2 systemd-journald.service
要启用日志限制持久化配置,可以修改 /etc/systemd/journald.conf
XML/HTML代码
- SystemMaxUse=16M
- ForwardToSyslog=no
XML/HTML代码
- systemctl restart systemd-journald.service