第二步‐开始部署LXC环境以及mihomo、openwrt - cooip-jm/About-openwrt GitHub Wiki

懒人初级目标

image

安装lxc环境

apt-get install lxc

image

检查

lxc-checkconfig

修改lxc默认的一些配置

/etc/lxc/default.conf

nano /etc/lxc/default.conf 

可根据自己习惯,在基础配置里增加一些其他的设定

禁用lxcbr0启动/etc/default/lxc-net

nano /etc/default/lxc-net

创建第一个lxc

基于debian的mihomo

lxc-create --name mihomo --template download -- --dist debian --release bookworm --arch arm64 --server mirrors.bfsu.edu.cn/lxc-images

修改配置

nano /var/lib/lxc/mihomo/config

image

使用宿主系统网桥 image

基础环境

apt install -y  openssh-server nano curl wget git apt-transport-https ca-certificates

安装mihomo

telegram-cloud-photo-size-5-6244447463142439184-y

telegram-cloud-photo-size-5-6244447463142439242-y

telegram-cloud-photo-size-5-6244447463142439243-y

退出该lxc

exit

配置LXC 内的debian 网络

nano /etc/systemd/network/eth0.network

根据自己实际情况修改

[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

启、停服务

# 查看网络服务的状态
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


apt install curl nano wget git apt-transport-https ca-certificates apt-utils -y
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

或者自行了解

https://wiki.metacubex.one/
https://github.com/MetaCubeX

第二个lxc openwrt

使用flippy openwrt rootfs

https://t.me/openwrt_flippy/4924

解压到

tar -zxf openwrt_lxc_template_R24.01.26-flippy-87+-rootfs.tar.gz -C /var/lib/lxc/openwrt/rootfs/

配置网络

image

nano /var/lib/lxc/openwrt/config 

# 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

lxc-start openwrt

进入

lxc-attach openwrt

退出

exit

###openwrt内配置网络

nano /etc/config/network 

配置好后

输入修改后的IP 地址

image

image image

在这个rootfs内启用N1的无线网卡

image

配置文件中添加

# 启用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举例

https://openwrt.org/

image

image

下载rootfs

wget https://downloads.openwrt.org/releases/23.05.2/targets/armsr/armv8/openwrt-23.05.2-armsr-armv8-rootfs.tar.gz

解压到lxc目录内,oWrt自己手动创建

tar -xzf openwrt-23.05.2-armsr-armv8-rootfs.tar.gz -C /var/lib/lxc/oWrt/rootfs

增加配置文件

nano /var/lib/lxc/oWrt/config

image

# 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

启动并进入

lxc-start oWrt
lxc-attach oWrt

修改root密码

passwd

修改openwrt网络

vi /etc/config/network

重启网络

/etc/init.d/network restart

image

浏览器访问你修改的IP

image

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

image

稍微配置一下 网关、DNS 根据自己实际情况来

image image

image

网络诊断一下,没问题的话进行下一步

image

配置基础换进

我仓库里写了三个非常简陋的基础脚本,凑合用用。

你们有更好的,欢迎PR

官方openwrt安装中文、和设置时区和一些基础环境的,脚本。

wget -O - https://raw.githubusercontent.com/cooip-jm/About-openwrt/main/bash.sh | sh -s -- -v

image

运行完后 image

这个用于拨号足够了,非常轻量化

passwall 官方openwrt安装 仅限ARM

wget -O - https://raw.githubusercontent.com/cooip-jm/About-openwrt/main/install-passwall.sh | sh -s -- -v

image image

openclash 官方openwrt安装

wget -O - https://raw.githubusercontent.com/cooip-jm/About-openwrt/main/openclash-install.sh | sh -s -- -v

上面只是为了演示

额外的一些配置,比如v6 DNS

image

image

计划任务重,自动更新openwrt软件包

opkg update && opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade

使用mihomo作为网关的话,后两个可以忽略。。。。。。。。。。。

lxc 自动启动

在配置文件里增加

lxc.start.auto = 1

如果要所有的lxc都自动启动

运行lxc-autostart --all

配置

宿主系统

nano /etc/systemd/system/lxc-start.service
[Unit]
Description="LXC autostart for lxc user"

[Service]
ExecStartPre=/bin/sleep 15
ExecStart=/usr/bin/lxc-autostart --all

[Install]
WantedBy=default.target

启用服务

systemctl daemon-reload
systemctl enable lxc-start.service
systemctl start lxc-start.service
systemctl status lxc-start.service

关于debian/ubuntu 或使用(systemd)journalctl在 lxc中的日志相关操作

查看日志占用

journalctl --disk-usage

清理方法可以采用按照日期清理,或者按照允许保留的容量清理

journalctl --vacuum-time=2d
journalctl --vacuum-size=500M

如果要手工删除日志文件,则在删除前需要先轮转一次journal日志

systemctl kill --kill-who=main --signal=SIGUSR2 systemd-journald.service

要启用日志限制持久化配置,可以修改 /etc/systemd/journald.conf

SystemMaxUse=16M
ForwardToSyslog=no

然后重启服务

systemctl restart systemd-journald.service