github.com/igoogolx/clash@v1.19.8/docs/introduction/service.md (about) 1 --- 2 sidebarTitle: Clash as a Service 3 sidebarOrder: 3 4 --- 5 6 # Clash as a Service 7 8 While Clash is meant to be run in the background, there's currently no elegant way to implement daemons with Golang, hence we recommend you to daemonize Clash with third-party tools. 9 10 ## systemd 11 12 Copy Clash binary to `/usr/local/bin` and configuration files to `/etc/clash`: 13 14 ```shell 15 cp clash /usr/local/bin 16 cp config.yaml /etc/clash/ 17 cp Country.mmdb /etc/clash/ 18 ``` 19 20 Create the systemd configuration file at `/etc/systemd/system/clash.service`: 21 22 ```ini 23 [Unit] 24 Description=Clash daemon, A rule-based proxy in Go. 25 After=network-online.target 26 27 [Service] 28 Type=simple 29 Restart=always 30 ExecStart=/usr/local/bin/clash -d /etc/clash 31 32 [Install] 33 WantedBy=multi-user.target 34 ``` 35 36 After that you're supposed to reload systemd: 37 38 ```shell 39 systemctl daemon-reload 40 ``` 41 42 Launch clashd on system startup with: 43 44 ```shell 45 systemctl enable clash 46 ``` 47 48 Launch clashd immediately with: 49 50 ```shell 51 systemctl start clash 52 ``` 53 54 Check the health and logs of Clash with: 55 56 ```shell 57 systemctl status clash 58 journalctl -xe 59 ``` 60 61 Credits to [ktechmidas](https://github.com/ktechmidas) for this guide. ([#754](https://github.com/Dreamacro/clash/issues/754)) 62 63 ## Docker 64 65 We provide pre-built images of Clash and Clash Premium. Therefore you can deploy Clash with [Docker Compose](https://docs.docker.com/compose/) if you're on Linux. However, you should be advised that it's [not recommended](https://github.com/Dreamacro/clash/issues/2249#issuecomment-1203494599) to run **Clash Premium** in a container. 66 67 ::: warning 68 This setup will not work on macOS systems due to the lack of [host networking and TUN support](https://github.com/Dreamacro/clash/issues/770#issuecomment-650951876) in Docker for Mac. 69 ::: 70 71 72 ::: code-group 73 74 ```yaml [Clash] 75 services: 76 clash: 77 image: ghcr.io/dreamacro/clash 78 restart: always 79 volumes: 80 - ./config.yaml:/root/.config/clash/config.yaml:ro 81 # - ./ui:/ui:ro # dashboard volume 82 ports: 83 - "7890:7890" 84 - "7891:7891" 85 # - "8080:8080" # The External Controller (RESTful API) 86 network_mode: "bridge" 87 ``` 88 89 ```yaml [Clash Premium] 90 services: 91 clash: 92 image: ghcr.io/dreamacro/clash-premium 93 restart: always 94 volumes: 95 - ./config.yaml:/root/.config/clash/config.yaml:ro 96 # - ./ui:/ui:ro # dashboard volume 97 ports: 98 - "7890:7890" 99 - "7891:7891" 100 # - "8080:8080" # The External Controller (RESTful API) 101 cap_add: 102 - NET_ADMIN 103 devices: 104 - /dev/net/tun 105 network_mode: "host" 106 ``` 107 108 ::: 109 110 Save as `docker-compose.yaml` and place your `config.yaml` in the same directory. 111 112 ::: tip 113 Before proceeding, refer to your platform documentations about time synchronisation - things will break if time is not in sync. 114 ::: 115 116 When you're ready, run the following commands to bring up Clash: 117 118 ```shell 119 docker-compose up -d 120 ``` 121 122 You can view the logs with: 123 124 ```shell 125 docker-compose logs 126 ``` 127 128 Stop Clash with: 129 130 ```shell 131 docker-compose stop 132 ``` 133 134 ## FreeBSD rc 135 136 install clash with `ports(7)` or `pkg(8)` 137 138 copy the required files to `/usr/local/etc/clash` 139 140 ```shell 141 cp config.yaml /usr/local/etc/clash/ 142 cp Country.mmdb /usr/local/etc/clash/ 143 ``` 144 145 Create the rc configuration file at `/usr/local/etc/rc.d/clash`: 146 147 ```shell 148 #!/bin/sh 149 150 # PROVIDE: clash 151 # REQUIRE: NETWORKING DAEMON 152 # BEFORE: LOGIN 153 # KEYWORD: shutdown 154 155 . /etc/rc.subr 156 157 name=clash 158 rcvar=clash_enable 159 160 : ${clash_enable="NO"} 161 : ${clash_config_dir="/usr/local/etc/clash"} 162 163 required_dirs="${clash_config_dir}" 164 required_files="${clash_config_dir}/config.yaml ${clash_config_dir}/Country.mmdb" 165 166 command="/usr/sbin/daemon" 167 procname="/usr/local/bin/${name}" 168 pidfile="/var/run/${name}.pid" 169 start_precmd="${name}_prestart" 170 171 clash_prestart() 172 { 173 rc_flags="-T ${name} -p ${pidfile} ${procname} -d ${clash_config_dir} ${rc_flags}" 174 } 175 176 load_rc_config $name 177 run_rc_command "$1" 178 ``` 179 180 make the script executable: 181 182 ```shell 183 chmod +x /usr/local/etc/rc.d/clash 184 ``` 185 186 Launch clashd on system startup with: 187 188 ```shell 189 service clash enable 190 ``` 191 192 Launch clashd immediately with: 193 194 ```shell 195 service clash onestart 196 ``` 197 198 Check the status of Clash with: 199 200 ```shell 201 service clash status 202 ``` 203 204 You can check log in file `/var/log/daemon.log` 205 206 ::: tip 207 If you want to change the default config directory add the following lines to /etc/rc.conf : 208 ```shell 209 clash_enable (bool): Set it to YES to run clash on startup. 210 Default: NO 211 clash_config_dir (string): clash config directory. 212 Default: /usr/loca/etc/clash 213 ``` 214 :::