Introduction · Clash documentation

Running Clash as a Service

Deploy the Clash binary and config.yaml as a systemd service, run daemon-reload, enable startup, inspect service status, and learn the basic Docker image workflow.

  • systemd
  • Docker
  • config.yaml
  • Daemon
  • journalctl
Introduction

Clash needs to run in the background. Because Golang does not currently provide a strong daemon implementation, we recommend using a third-party tool to create a Clash daemon.

systemd

Use the following commands to copy the Clash binary to /usr/local/bin and the configuration file to /etc/clash:

shell
cp clash /usr/local/bin
cp config.yaml /etc/clash/
cp Country.mmdb /etc/clash/

Create the systemd configuration file /etc/systemd/system/clash.service:

ini
[Unit]
Description=Clash 守护进程, Go 语言实现的基于规则的代理.
After=network-online.target

[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/clash -d /etc/clash

[Install]
WantedBy=multi-user.target

Then reload systemd with the following command:

shell
systemctl daemon-reload

Enable Clash at system startup with the following command:

shell
systemctl enable clash

Start Clash immediately with the following command:

shell
systemctl start clash

Check Clash status and logs with the following command:

shell
systemctl status clash
journalctl -xe

This guide was contributed by ktechmidas (#754).

Docker

The project provides prebuilt Docker images for Clash and Clash Premium. On Linux, you can therefore deploy Clash with Docker Compose. However, you should be aware that running Clash Premium in a container is not recommended.

WARNING

Because Docker for Mac lacks host networking and TUN support, this setup will not work on macOS.

yaml
services:
  clash:
    image: ghcr.io/dreamacro/clash
    restart: always
    volumes:
      - ./config.yaml:/root/.config/clash/config.yaml:ro
      # - ./ui:/ui:ro # 仪表盘 Volume 映射
    ports:
      - "7890:7890"
      - "7891:7891"
      # - "8080:8080" # 外部控制 (RESTful API)
    network_mode: "bridge"
yaml
services:
  clash:
    image: ghcr.io/dreamacro/clash-premium
    restart: always
    volumes:
      - ./config.yaml:/root/.config/clash/config.yaml:ro
      # - ./ui:/ui:ro # 仪表盘 Volume 映射
    ports:
      - "7890:7890"
      - "7891:7891"
      # - "8080:8080" # 外部控制 (RESTful API)
    cap_add:
      - NET_ADMIN
    devices:
      - /dev/net/tun
    network_mode: "host"

Save this as docker-compose.yaml, and place your config.yaml in the same directory.

TIP

Before continuing, consult your platform's time-synchronization documentation. Some protocols may not work correctly when the clock is out of sync.

When ready, start Clash with the following command:

shell
docker-compose up -d

View the logs with the following command:

shell
docker-compose logs

Stop Clash with:

shell
docker-compose stop