在服务器或桌面 Linux 上,把内核跑成一个后台服务是最省心的做法: 开机自启、崩溃自动拉起、日志集中查看。
确认架构并下载
# 查看 CPU 架构
uname -m
# x86_64 → 下载 amd64 版本
# aarch64 → 下载 arm64 版本
# 下载并解压(示例为 amd64)
wget -O clash-meta.gz https://example.com/clash-meta-linux-amd64.gz
gunzip clash-meta.gz
chmod +x clash-meta
sudo mv clash-meta /usr/local/bin/
准备目录与配置
配置目录推荐放在 /etc/clash,并单独创建一个低权限用户运行服务。
sudo mkdir -p /etc/clash
sudo cp your-config.yaml /etc/clash/config.yaml
# 创建专用用户(无登录权限)
sudo useradd --system --no-create-home --shell /usr/sbin/nologin clash
# 授权
sudo chown -R clash:clash /etc/clash
sudo chmod 600 /etc/clash/config.yaml
前台启动验证
在写服务文件之前,先手动跑一次,确认配置本身没有问题。
# 校验配置文件语法
clash-meta -d /etc/clash -t
# 前台运行,输出日志到终端
clash-meta -d /etc/clash
看到 Start initial compatible provider 与监听端口信息,说明配置生效。按 Ctrl+C 退出后继续下一步。
用 systemd 托管服务
新建服务单元文件:
sudo tee /etc/systemd/system/clash.service > /dev/null <<'EOF'
[Unit]
Description=Clash Meta Service
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=clash
Group=clash
ExecStart=/usr/local/bin/clash-meta -d /etc/clash
Restart=always
RestartSec=3
LimitNOFILE=1048576
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW
NoNewPrivileges=true
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now clash
sudo systemctl status clash
关键点:AmbientCapabilities 让进程在没有 root 的前提下获得创建 TUN 设备与绑定特权端口的能力,比直接用 root 运行更安全。
开启 IP 转发
如果内核需要作为网关为其他设备转发流量,必须打开系统转发开关,否则 TUN 与局域网共享都会失效。
sudo tee /etc/sysctl.d/99-clash.conf > /dev/null <<'EOF'
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
EOF
sudo sysctl --system
sysctl net.ipv4.ip_forward
查看日志与排错
| 命令 | 用途 |
|---|---|
systemctl status clash | 查看服务当前状态 |
journalctl -u clash -f | 实时跟踪运行日志 |
journalctl -u clash --since "10 min ago" | 回看最近十分钟日志 |
ss -lntp | grep clash | 确认监听端口是否正常 |
服务起不来的三大常见原因:配置文件语法错误、端口被占用、/etc/clash 权限不对。
用 -t 参数可以在启动前直接排除第一类问题。
如果你打算把内核跑在容器里,可以继续阅读 Docker 容器化部署实践。
安装
← 返回教程列表