找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
广告投放联系QQ68610888
查看: 4277|回复: 87

[虚拟机相关] PVE下使用LXC容器Alpine 安装sing-box、Mosdns、AdguardHome 记录

  [复制链接]
本帖最后由 sires 于 2024-4-25 20:39 编辑

一、序言

PVE LXC容器效率高,占用资源小, 利用Alpine模版大小只有5M大小,完全安装sing-box、Mosdns、AdguardHome 后,占用空间160M左右, 内存分配128-256随意。
主要参考了  :
charlzyx

https://charlzyx.github.io/boom/0x06clash.html

https://www.right.com.cn/forum/thread-8303306-1-1.html

但也有些坑,留作记录吧。
两点小遗憾:
1、Alpine LXC  始终没有成功打开 sing-box 的TUN模式,估计Clash也一样不行,有高手请指导。
2、(这个解决了)因为是All IN 一个LXC, 无法本机代理, 下载一些插件和附件会被墙,办法就是其他主机docker建一个文件服务器
#创建一个docker 本地服务器 拉取文件用
4月3日 更新
改思路了,还是 sing-box + AdguardHome 吧,  mosdns的分流 sing-box 已经集成的很好了。
参考


二、创建:Alpine LXC 容器。
PVE里有模版,直接拉取就行, 注意 1、要去掉无特权的 勾 ,2、不选安装防火墙  3、直接设置固定IP 记得 ip/24,网关可以先指向代理方便下载一些插件,后面可以改。

4月4日,补充 优化了DNS.json 见 32楼

https://www.right.com.cn/forum/forum.php?mod=redirect&goto=findpost&ptid=8358866&pid=20118588



三、准备 Alpine

#1、登录

root 密码是创建lxc设置的密码


#2、安装必要依赖

apk update
apk add curl nftables iptables iproutes ip6tables openssh-server openssh vim

#3、#开启SSHD 方便连接和文件传输
apk add openssh-server #上一步已经安装过
# 开放Root登录
echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config
#重启服务
rc-service sshd start
# 设置开机启动
rc-update add sshd
# 删除开机启动服务 不需
# rc-update del sshd

#4、开启流量转发 弄不弄都可以

echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
echo "net.ipv6.conf.all.forwarding" >> /etc/sysctl.conf
# 使修改生效
sysctl -p

#5、#PVE Apline 开启 Tun 网卡

这个始终没有打开sing-box tun模式,但是这步打开Tun 网卡是有效的,
# 打开宿主机 PVE 的控制台, 执行以下命令手动给 lxc 开启 tun, 修改之后重启当前容器
#cd /etc/pve/lxc && ls
#vim xxx.conf # xxx 为容器 id
#在末尾添加如下配置
lxc.cgroup.devices.allow: c 10:200 rwm
lxc.cgroup2.devices.allow: c 10:200 rwm  # PVE < 7.0
lxc.mount.entry: /dev/net dev/net none bind,create=dir

四、安装sing-box

用ShellCrash安装

一键脚本:

export url='https://fastly.jsdelivr.net/gh/juewuy/ShellCrash@master' && sh -c "$(curl -kfsSl $url/install.sh)" && source /etc/profile &> /dev/null

几点注意:

1、不要急着启动,第一步先下载sing-box核心,确定核心才能设置其他选项,我选的sing-box基础核心。
2、无法使用Tun模式, 我用的Tproxy模式。
3、关于基础DNS部分 ,不要指向 MOSDNS 会回流锁死内存。
4、要禁用 DNS 劫持。

五、安装 AdguardHome

一键脚本
"curl -s -S -L https://mirror.ghproxy.com/https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh | sh -s -- -v"

curl -sSL https://raw.githubusercontent.com/AdguardTeam/AdGuardHome/master/scripts/install.sh | sh


chmod +x /etc/AdGuardHome/AdGuardHome
/etc/AdGuardHome/AdGuardHome -s install
/etc/AdGuardHome/AdGuardHome -s start
rc-update add AdGuardHome


六、安装 Mosdns


# 创建 /etc/mosdns 工作目录
mkdir -p /etc/mosdns && cd /etc/mosdns
unzip mosdns-linux-amd64.zip
#拉取 相关集合
mkdir -p /etc/mosdns/rule
curl https://raw.githubusercontent.co ... ase/direct-list.txt > /etc/mosdns/rule/direct-list.txt && \
curl https://raw.githubusercontent.co ... elease/apple-cn.txt > /etc/mosdns/rule/apple-cn.txt && \
curl https://raw.githubusercontent.co ... lease/google-cn.txt > /etc/mosdns/rule/google-cn.txt && \
curl https://raw.githubusercontent.co ... ease/proxy-list.txt > /etc/mosdns/rule/proxy-list.txt && \
curl https://raw.githubusercontent.co ... ease/CN-ip-cidr.txt > /etc/mosdns/rule/CN-ip-cidr.txt && \
touch /etc/mosdns/rule/force-nocn.txt && \
touch /etc/mosdns/rule/hosts.txt && \
touch /etc/mosdns/rule/fake-ip-cidr.txt && \
touch /etc/mosdns/rule/force-cn.txt

配置  :/etc/mosdns/config.yaml


log:
  level: error # debug
  #file: "./log/mosdns.log"      # 记录日志到文件。
  production: true


# API 入口设置
api:
  http: "0.0.0.0:9080" # 在该地址启动 api 接口。


# 从其他配置文件载入 plugins 插件设置。
# include 的插件会比本配置文件中的插件先初始化。
include: []


plugins:


  - tag: "geosite-cn"
    type: domain_set
    args:
      files:
        - "./rule/direct-list.txt"
        - "./rule/apple-cn.txt"
        - "./rule/google-cn.txt"


  - tag: "geosite-nocn"
    type: domain_set
    args:
      files:
        - "./rule/proxy-list.txt"
        - "./rule/gfw.txt"


  - tag: "geoip-cn"
    type: ip_set
    args:
      files: "./rule/CN-ip-cidr.txt"


  - tag: "fake-ip-clash"
    type: ip_set
    args:
      files: "./rule/fake-ip-cidr.txt"


  - tag: "force-cn"
    type: domain_set
    args:
      files: "./rule/force-cn.txt"


  - tag: "force-nocn"
    type: domain_set
    args:
      files: "./rule/force-nocn.txt"


  - tag: "hosts"
    type: hosts
    args:
      files: "./rule/hosts.txt"


  - tag: "cache"
    type: "cache"
    args:
      size: 1024
      lazy_cache_ttl: 0
      dump_file: ./cache.dump
      dump_interval: 600


  # 转发至本地服务器的插件
  - tag: forward_local
    type: forward
    args:
      concurrent: 2
      upstreams:
        - addr: "https://dns.alidns.com/dns-query"
        - addr: "tls://dns.alidns.com"
        #- addr: "https://1.12.12.12/dns-query"
        #- addr: "https://120.53.53.53/dns-query"


  # 转发至远程服务器的插件
  - tag: forward_remote
    type: forward
    args:
      concurrent: 2
      upstreams:
        - addr: 127.0.0.1:1053      # 1053 是sing-box 监听的端口,如果不在同一主机 改为sing-box主机地址
        # - addr: "https://cloudflare-dns.com/dns-query"
        # - addr: "tls://1dot1dot1dot1.cloudflare-dns.com"
        # - addr: "https://dns.google/dns-query"
        # - addr: "tls://dns.google"


  - tag: "primary_forward"
    type: sequence
    args:
      - exec: $forward_local
      - exec: ttl 60-3600
      - matches:
          - "!resp_ip $geoip-cn"
          - "has_resp"
        exec: drop_resp


  - tag: "secondary_forward"
    type: sequence
    args:
      - exec: prefer_ipv4
      - exec: $forward_remote
      - matches:
          - rcode 2
        exec: $forward_local
      - exec: ttl 300-3600


  - tag: "final_forward"
    type: fallback
    args:
      primary: primary_forward
      secondary: secondary_forward
      threshold: 150
      always_standby: true


  - tag: main_sequence
    type: sequence
    args:
      - exec: $hosts
      - exec: query_summary hosts
      - matches: has_wanted_ans
        exec: accept


      - exec: $cache
      - exec: query_summary cache
      - matches: has_wanted_ans
        exec: accept


      - exec: query_summary qtype65
      - matches:
          - qtype 65
        #         exec: black_hole 127.0.0.1 ::1 0.0.0.0
        exec: reject 0


      - matches:
          - qname $geosite-cn
        exec: $forward_local
      - exec: query_summary geosite-cn
      - matches: has_wanted_ans
        exec: accept


      - matches:
          - qname $force-cn
        exec: $forward_local
      - exec: query_summary force-cn
      - matches: has_wanted_ans
        exec: accept


      - matches:
          - qname $geosite-nocn
        exec: $forward_remote
      - exec: query_summary geosite-nocn
      - matches: has_wanted_ans
        exec: accept


      - matches:
          - qname $force-nocn
        exec: $forward_remote
      - exec: query_summary force-nocn
      - matches: has_wanted_ans
        exec: accept


      - exec: $final_forward


  - tag: "udp_server"
    type: "udp_server"
    args:
      entry: main_sequence
      listen: 0.0.0.0:5233  #监听的dns端口 自定义   确定AdguardHome 上游指向这个端口


  - tag: "tcp_server"
    type: "tcp_server"
    args:
      entry: main_sequence
      listen: 0.0.0.0:5233 #监听的dns端口 自定义

七、以上在同一alpine LXC主机
需要不同IP主机或 LXC 部署的可以更改相关配置文件中指向不同IP地址 (主要是127.0.0.1 或 0.0.0.0)。
唯一遗憾 Tun模式打不开,有高手还请指导。






其实我也试过,我直接用lxc openwrt还更多插件可以用,所以我还是lxc openwrt算了。
回复

使用道具 举报

感谢楼主的分享

回复

使用道具 举报

 楼主| | 显示全部楼层
children009 发表于 2024-3-29 10:33
其实我也试过,我直接用lxc openwrt还更多插件可以用,所以我还是lxc openwrt算了。 ...

是,有啥好的op模版 推荐?  Op的问题就是很多插件会有莫名的冲突问题,稳定性也欠缺。
回复

使用道具 举报

为啥一定要tun模式?我这里反而是tproxy模式更好
回复

使用道具 举报

看看········
回复

使用道具 举报

本帖最后由 Demon-sky 于 2024-3-29 11:48 编辑

记得pve7及以上默认用cgroup2,帖子:lxc.cgroup2.devices.allow: c 10:200 rwm  # PVE < 7.0
挂载tun可尝试:
lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file
回复

使用道具 举报

看看········
回复

使用道具 举报

 楼主| | 显示全部楼层
Demon-sky 发表于 2024-3-29 11:44
记得pve7及以上默认用cgroup2,帖子:lxc.cgroup2.devices.allow: c 10:200 rwm  # PVE < 7.0
挂载tun可尝 ...

是这意思,sing-box 启动后可以看到 挂载了 tun ,也不报错,就是网络跑不通,原因不明。
回复

使用道具 举报

 楼主| | 显示全部楼层
本帖最后由 sires 于 2024-3-29 15:18 编辑
itiao 发表于 2024-3-29 11:24
为啥一定要tun模式?我这里反而是tproxy模式更好

hehe ,没啥,就是纠结还有个地方没搞明白。


pve客户机定时启动
必须将crontab中的命令补齐,不能仅用qm,如每日定时启动客户机ID 1000

0 8 * * * /usr/sbin/qm start 1000


容器lxc开启tun-br
pve7主机编辑lxc文件, vi /etc/pve/lxc/2001.conf ,最后加入


systemd.unified_cgroup_hierarchy=0
lxc.cgroup.devices.allow =
lxc.cgroup.devices.deny =

lxc.cgroup2.devices.allow = c 10:200 rwm
lxc.hook.autodev = sh -c "modprobe tun; cd ${LXC_ROOTFS_MOUNT}/dev; mkdir net; mknod net/tun c 10 200; chmod 0666 net/tun"

进入lxc执行,重启后生效

进入lxc执行,重启后生效

cd /dev
mkdir net
mknod net/tun c 10 200
chmod 0666 net/tun
reboot

回复

使用道具 举报

看看                                                  
回复

使用道具 举报

我来看看,之前安装tun就是搞不懂!!!
回复

使用道具 举报

P主机或 LXC 部署的可以更改相关配
回复

使用道具 举报

来自手机 | 显示全部楼层
谢谢老板,生意兴隆
回复

使用道具 举报

PVE下使用LXC容器Alpine 安装sing-box、Mosdns、AdguardHome
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

欢迎大家光临恩山无线论坛上一条 /1 下一条

有疑问请添加管理员QQ86788181|手机版|小黑屋|Archiver|恩山无线论坛(常州市恩山计算机开发有限公司版权所有) ( 苏ICP备05084872号 )

GMT+8, 2024-5-9 20:47

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

| 江苏省互联网有害信息举报中心 举报信箱:js12377 | @jischina.com.cn 举报电话:025-88802724 本站不良内容举报信箱:68610888@qq.com 举报电话:0519-86695797

快速回复 返回顶部 返回列表