banner
600
2 分钟

使用docker部署旁路由版clash

2026年3月2日

文章摘要

使用Docker部署Clash旁路由,可以满足轻需求用户的需求。首先,需要确认网卡名称,可以使用命令`ip a`或`ifconfig`查看。然后,设置网卡为混杂模式,以便Clash处理网卡流量。最后,在DockerCompose文件中配置网卡名称,包括设置容器的网络和IP地址。通过这些步骤,可以实现Clash旁路由的部署,使用Clash自带的DNS作为DNS服务器,并开启网卡混杂以处理网卡流量。

对于多数人来说,其实并不需要openwrt那样的虚拟机或者小盒子之类的机器,一些轻需求的用户其实可以选择使用docker部署一个clash旁路由,使用clash自带的dns作为dns服务器,然后开启网卡混杂以使用clash处理网卡流量,话不多说开始吧。

一、确认网卡名称

使用下列命令可以查看网卡信息

text
root@debian:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: enp6s18: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 7e:c9:c3:e5:0b:ab brd ff:ff:ff:ff:ff:ff
    inet 192.87.18.32/24 brd 192.87.18.255 scope global enp6s18
       valid_lft forever preferred_lft forever

[!TIP] 当然你也可以使用ifconfig命令

可以看到我的网卡名称是enp6s18

二、网卡设置为混杂模式

此步骤为了能使clash处理网卡流量

text
# 方法1
sudo ip link set dev enp6s18 promisc on
sudo ip link set dev enp6s18 promisc off

# 方法2
ifconfig enp6s18 promisc
ifconfig enp6s18 -promisc

# 方法3(for macos)
tcpdump -Ii enp6s18

[!TIP]

这里的enp6s18是我的网卡名称,实际根据你的网卡名称做修改

三、在docker-compose文件中配置网卡名称

text
services:
  clash-router:
    image: laohui/clash
    restart: unless-stopped
    container_name: clash-router
    hostname: clash
    privileged: true
    pid: host
    ipc: host
    cap_add:
      - ALL
    devices:
      - /dev/net/tun
    ports:
      - 10080:10080
      - 9090:9090
      - 53:53
    volumes:
      - ./clash:/etc/mihomo/
    networks:
      magic:
        ipv4_address: 192.87.18.2     # 这里则是你要获取的内网IP

networks:
  magic:
    name: clash_static
    driver: macvlan
    driver_opts:
      parent: enp6s18    # 在这里填写为你的网卡名称
    ipam:
      config:
        - subnet: "192.87.18.0/24"
          ip_range: "192.87.18.0/24"
          gateway: "192.87.18.1"
      options:
        dns: "192.87.18.1"

END

相关文章

暂无相关文章

© 2026 laohui. All Rights Reserved. / RSS / Sitemap
Powered by Tanstack Start & Flare Stack Blog