项目作者: yuanlam

项目描述 :
记录Linux下折腾Clash的步骤
高级语言:
项目地址: git://github.com/yuanlam/Clash-Linux.git
创建时间: 2020-03-15T12:43:48Z
项目社区:https://github.com/yuanlam/Clash-Linux

开源协议:

下载


Clash-Linux-折腾笔记

在这里记录下自己在LINUX下折腾Clash的基本过程和踩过得坑。本人并没有正规学过LINUX,以下内容为本人全部是在网络上面搜集的教程和TG各大佬的指导,然后整合,反复折腾出来的,亲测可用,如果有写的不对的地方,大佬别笑。。。

坦白说Clash是我用过的旁路由科学上网插件中最好用的一款(仅限本人),从Windows下的Clash for Windows到Koolshare Lede的KoolClash再到Openwrt的OpenClash,我都有深入使用过,下面是这三款软件的介绍和我的使用感受

Clash for Windows

Clash for Windows最大的特点就是简单易用,传说中的即插即用。可惜只支持Windows设备,多设备想同时享受代理的话似乎比较麻烦,这是我没接触软路由前使用的软件。但由于手头的设备比较多,所以产生了玩软路由的念头,于是有了使用下面两款软件的机会

KoolClash

KoolClash是苏大开发的一款基于Koolshare Lede系统下的一款Clash内核的科学上网插件。在刚接触软路由的时候,受油管各大UP主的影响,本人使用的就是物理机装LEDE做主路由的方案,所以KoolClash也成了我科学上网的首选。但是在我使用LEDE作为单路由的时候,KoolClash才刚刚更新支持Fake-ip,加上本人知识有限,所有在配置方面遇到诸多问题,一直不能完美的运行。所有才有了后面的主路由加旁路网关的方案。

OpenClash

OpenClash是运行在OpenWrt系统上的一款插件,其实Koolshare Lede也可以装(毕竟LEDE也是Openwrt中的一个分支)。至于为什么用OpenWrt而不再使用Koolshare Lede,一方面是受油管各UP主的洗脑,另一方面是因为我是一个追新的人,Koolshare Lede都好久没更新了,感觉已经被他们的团队放弃了。而当我使用OpenWrt的时候,已经是使用主路由加旁路网关的方案,所以在使用OpenClash上是相对比较顺利的(也许就像苏大说的那样,Fake-ip更适合作为旁路网关使用吧)。所以主路由加旁路openclash算是一个比较稳定跟简单的方案,基本已经不需要再折腾了。

Clash for Linux

至于为什么最终使用Linux下的Clash替代OpenClash,完全是因为本人水平有限,又习惯折腾。自己编译出来的Openwrt固件问题不断,用各位大佬编译好的又有些设置我不能理解,功能也有90%我用不上,所以才选择这种方案。幸好,至今还算稳定,机场线路也很给面子,所以我还是比例倾向于这种的。

废话不多说,下面正式开始

系统:Debian 10.3 (其他任何Linux系统都可以,或者更好)

官网下载的ISO镜像,Proxmox新建虚拟机安装,安装过程就不累述了,网上大把教程。新建一个普通用户,以下用本人惯用的yuanlam为例

一.修改Debian IP获取方式为静态IP(root用户)

因为Debian新系统IP获取方式默认是DHCP,这里得修改为静态,这样才能用SSH进行后续操作

root用户登录,如果没法直接用root用户登录的话,就用yuanlam登录,然后用su - root输入密码切换。
1.打开文件,用nano用vim都可以,看个人习惯
  1. nano /etc/network/interfaces

2.参照以下内容根据实际情况修改:

  1. auto lo
  2. auto eth0 #eth0为网卡,根据实际情况修改
  3. iface lo inet loopback
  4. iface eth0 inet static #static为静态,dhcp为动态
  5. address 10.10.10.97 #本机IP
  6. netmask 255.255.255.0 #子网掩码
  7. gateway 10.10.10.77 #网关(科学上网的IP)
  8. dns-nameservers 198.18.0.1 #DNS,我的科学上网方式是openwrt旁路由用的openclash,用的fake-ip,所以这里填的是198.18.0.1
  9. mtu 1492 #MTU
  10. mss 1452 #MSS

3.重启网络

  1. /etc/init.d/networking restart

4.允许root用户ssh连接

打开文件

  1. nano /etc/ssh/sshd_config

取消注释,修改

  1. PermitRootLogin yes

重启服务

  1. service sshd restart

4.重启系统

  1. reboot

二.系统调优(root用户,此步骤可做可不做)

所有东西都是参照网上修改的,究竟有没有效我也不知道

1.升级

  1. apt update && apt -y upgrade

2.安装必要组件

  1. apt -y install net-tools curl vim zip unzip yum supervisor wget nano gnupg gnupg2 gnupg1
  2. apt -y install sudo
  3. sudo apt-get install libcap2-bin

3.切换内核,开启BBR

添加源

  1. echo 'deb http://deb.xanmod.org releases main' | sudo tee /etc/apt/sources.list.d/xanmod-kernel.list && wget -qO - https://dl.xanmod.org/gpg.key | sudo apt-key add -

安装

  1. apt -y update && apt -y install linux-xanmod

在systemd(> = 217)的系统中使用CAKE队列规则

  1. echo 'net.core.default_qdisc = cake' | tee /etc/sysctl.d/90-override.conf

重启

  1. reboot

查看CAKE是否生效

  1. sysctl net.core.default_qdisc

查看可用的拥塞控制算法

  1. sysctl net.ipv4.tcp_available_congestion_control

查看当前的拥塞控制算法,应该是回显BBR,也就是说BBR是直接开启的,不需要去自己改sysctl.conf

  1. sysctl net.ipv4.tcp_congestion_control

4.优化系统参数

打开配置文件

  1. sudo nano /etc/sysctl.conf

最下填入以下参数

  1. #开启流量转发
  2. **此处是重点,后面那些可以不改,这里必须改,开启了转发,后面的动作才有意义**
  3. net.ipv4.ip_forward=1
  4. #增大打开文件数限制
  5. fs.file-max = 999999
  6. #增大所有类型数据包的缓冲区大小(通用设置,其中default值会被下方具体类型包的设置覆盖)
  7. #最大缓冲区大小为64M,初始大小64K。下同
  8. #此大小适用于一般的使用场景。如果场景偏向于传输大数据包,则可以按倍数扩大该值,去匹配单个包大小
  9. net.core.rmem_max = 67108864
  10. net.core.wmem_max = 67108864
  11. net.core.rmem_default = 6291456
  12. net.core.wmem_default = 6291456
  13. net.core.netdev_max_backlog = 65535
  14. net.core.somaxconn = 262114
  15. #增大TCP数据包的缓冲区大小,并优化连接保持
  16. net.ipv4.tcp_tw_reuse = 1
  17. net.ipv4.tcp_fin_timeout = 30
  18. net.ipv4.tcp_keepalive_time = 1200
  19. net.ipv4.tcp_max_syn_backlog = 8192
  20. net.ipv4.tcp_max_tw_buckets = 5000
  21. net.ipv4.tcp_mem = 8192 131072 67108864
  22. net.ipv4.tcp_rmem = 10240 87380 12582912
  23. net.ipv4.tcp_wmem = 10240 87380 12582912
  24. net.ipv4.tcp_mtu_probing = 1
  25. net.ipv4.tcp_notsent_lowat = 16384
  26. net.ipv4.tcp_syncookies = 1
  27. net.ipv4.tcp_max_orphans= 262114
  28. net.ipv4.tcp_fastopen = 3
  29. net.ipv4.ip_local_port_range = 1024 65000
  30. #增大UDP数据包的缓冲区大小
  31. net.ipv4.udp_mem = 8192 131072 67108864
  32. net.ipv4.udp_rmem_min = 4096
  33. net.ipv4.udp_wmem_min = 4096

保存退出,输入命令使修改生效

  1. sysctl --system

5.永久关闭ipv6

依次输入命令

  1. echo " ">>/etc/sysctl.conf
  2. echo "# made for disabled IPv6 in $(date +%F)">>/etc/sysctl.conf
  3. echo 'net.ipv6.conf.all.disable_ipv6 = 1'>>/etc/sysctl.conf
  4. echo 'net.ipv6.conf.default.disable_ipv6 = 1'>>/etc/sysctl.conf
  5. echo 'net.ipv6.conf.lo.disable_ipv6 = 1'>>/etc/sysctl.conf
  6. tail -5 /etc/sysctl.conf
  7. sysctl -p
  8. netstat -anptl

修改ssh配置,只监听IPv4地址
打开文档

  1. nano /etc/ssh/sshd_config

文件末尾添加以下参数,也可直接文档取消注释

  1. ListenAddress 0.0.0.0
  2. AddressFamily inet

重启服务

  1. service sshd restart
  2. netstat -anptl

6.取消53端口被占用的情况

其实这里可以不做,只要Clash的DNS监听端口不适用53就可以了
进入文件

  1. sudo nano /etc/systemd/resolved.conf

取消注释,修改yes为no

  1. DNSStubListener=no

保存退出

7.创建非root用户,用于安装运行Clash

创建新用户
此处如果是用IOS镜像安装,可以跳过

  1. sudo adduser yuanlam #根据提示输入密码,其他保持默认

将新用户添加到 sudo 组

  1. usermod -aG sudo yuanlam

重启系统

  1. reboot

三.安装Clash(使用yuanlam用户登录)

1.下载最新版本

  1. wget https://github.com/Dreamacro/clash/releases/download/v0.19.0/clash-linux-amd64-v0.19.0.gz

2.解压

  1. gzip -d clash-linux-amd64-v0.19.0.gz

3.移动至usr/bin/clash并重命名为clash

  1. sudo mv clash-linux-amd64-v0.19.0 /usr/bin/clash #普通用户首次使用sudo需要输入密码,输入正确没有任何反馈

4.赋予clash运行权限

  1. sudo chmod +x /usr/bin/clash

5.检查是否安装成功

  1. clash -v #如返回(Clash v0.18.0 linux amd64 Fri Feb 21 12:42:08 UTC 2020)即成功

6.为 clash 添加绑定低位端口的权限,这样运行clash的时候无需root权限

  1. sudo setcap cap_net_bind_service=+ep /usr/bin/clash

四.创建配置文件及安装控制面板(使用yuanlam用户登录)

1.创建配置文件目录

  1. mkdir -p ~/.config/clash

2.进入目录

  1. cd ~/.config/clash

3.创建配置文件

  1. touch config.yaml

4.手动编辑很麻烦,可用winscp上传到/home/用户名/.config/clash (需打开隐藏文件),然后再用命令确认

  1. nano config.yaml

一下附上部分配置参数

  1. # HTTP端口
  2. port: 7890
  3. # SOCKS5端口
  4. socks-port: 7891
  5. # redir port for Linux and macOS
  6. # redir-port: 7892
  7. allow-lan: true
  8. # Rule / Global / Direct (default is Rule)
  9. mode: Rule
  10. # set log level to stdout (default is info)
  11. # info / warning / error / debug / silent
  12. log-level: info
  13. # 控制面板端口
  14. external-controller: 0.0.0.0:9090
  15. # you can put the static web resource (such as clash-dashboard) to a directory, and clash would serve in `${API}/ui`
  16. # input is a relative path to the configuration directory or an absolute path
  17. # external-ui: dashboard
  18. # 控制面板密码
  19. secret: "123456"
  20. dns:
  21. enable: true # 启用自定义DNS
  22. ipv6: false # default is false
  23. listen: 0.0.0.0:53
  24. enhanced-mode: fake-ip #DNS模式,这里推荐使用fake-ip,因为后续的iptables规则是根据fake-ip做的
  25. fake-ip-range: 198.18.0.1/16 # if you don't know what it is, don't change it
  26. nameserver:
  27. - 114.114.114.114 #只填一个你喜欢的DNS就够了,具体可以去看CLASH官方关于fake-ip的相关文章
  28. proxies:
  29. # shadowsocks
  30. # The supported ciphers(encrypt methods):
  31. # aes-128-gcm aes-192-gcm aes-256-gcm
  32. # aes-128-cfb aes-192-cfb aes-256-cfb
  33. # aes-128-ctr aes-192-ctr aes-256-ctr
  34. # rc4-md5 chacha20-ietf xchacha20
  35. # chacha20-ietf-poly1305 xchacha20-ietf-poly1305
  36. - name: "ss1"
  37. type: ss
  38. server: server
  39. port: 443
  40. cipher: chacha20-ietf-poly1305
  41. password: "password"
  42. # udp: true
  43. # old obfs configuration format remove after prerelease
  44. - name: "ss2"
  45. type: ss
  46. server: server
  47. port: 443
  48. cipher: chacha20-ietf-poly1305
  49. password: "password"
  50. plugin: obfs
  51. plugin-opts:
  52. mode: tls # or http
  53. # host: bing.com
  54. - name: "ss3"
  55. type: ss
  56. server: server
  57. port: 443
  58. cipher: chacha20-ietf-poly1305
  59. password: "password"
  60. plugin: v2ray-plugin
  61. plugin-opts:
  62. mode: websocket # no QUIC now
  63. # tls: true # wss
  64. # skip-cert-verify: true
  65. # host: bing.com
  66. # path: "/"
  67. # mux: true
  68. # headers:
  69. # custom: value
  70. # vmess
  71. # cipher support auto/aes-128-gcm/chacha20-poly1305/none
  72. - name: "vmess"
  73. type: vmess
  74. server: server
  75. port: 443
  76. uuid: uuid
  77. alterId: 32
  78. cipher: auto
  79. # udp: true
  80. # tls: true
  81. # skip-cert-verify: true
  82. # network: ws
  83. # ws-path: /path
  84. # ws-headers:
  85. # Host: v2ray.com
  86. # socks5
  87. - name: "socks"
  88. type: socks5
  89. server: server
  90. port: 443
  91. # username: username
  92. # password: password
  93. # tls: true
  94. # skip-cert-verify: true
  95. # udp: true
  96. # http
  97. - name: "http"
  98. type: http
  99. server: server
  100. port: 443
  101. # username: username
  102. # password: password
  103. # tls: true # https
  104. # skip-cert-verify: true
  105. # snell
  106. - name: "snell"
  107. type: snell
  108. server: server
  109. port: 44046
  110. psk: yourpsk
  111. # obfs-opts:
  112. # mode: http # or tls
  113. # host: bing.com
  114. # trojan
  115. - name: "trojan"
  116. type: trojan
  117. server: server
  118. port: 443
  119. password: yourpsk
  120. # udp: true
  121. # sni: example.com # aka server name
  122. # alpn:
  123. # - h2
  124. # - http/1.1
  125. # skip-cert-verify: true
  126. proxy-groups:
  127. # relay chains the proxies. proxies shall not contain a proxy-group. No UDP support.
  128. # Traffic: clash <-> http <-> vmess <-> ss1 <-> ss2 <-> Internet
  129. - name: "relay"
  130. type: relay
  131. proxies:
  132. - http
  133. - vmess
  134. - ss1
  135. - ss2
  136. # url-test select which proxy will be used by benchmarking speed to a URL.
  137. - name: "auto"
  138. type: url-test
  139. proxies:
  140. - ss1
  141. - ss2
  142. - vmess1
  143. url: 'http://www.gstatic.com/generate_204'
  144. interval: 300
  145. # fallback select an available policy by priority. The availability is tested by accessing an URL, just like an auto url-test group.
  146. - name: "fallback-auto"
  147. type: fallback
  148. proxies:
  149. - ss1
  150. - ss2
  151. - vmess1
  152. url: 'http://www.gstatic.com/generate_204'
  153. interval: 300
  154. # load-balance: The request of the same eTLD will be dial on the same proxy.
  155. - name: "load-balance"
  156. type: load-balance
  157. proxies:
  158. - ss1
  159. - ss2
  160. - vmess1
  161. url: 'http://www.gstatic.com/generate_204'
  162. interval: 300
  163. # select is used for selecting proxy or proxy group
  164. # you can use RESTful API to switch proxy, is recommended for use in GUI.
  165. - name: Proxy
  166. type: select
  167. proxies:
  168. - ss1
  169. - ss2
  170. - vmess1
  171. - auto
  172. - name: UseProvider
  173. type: select
  174. use:
  175. - provider1
  176. proxies:
  177. - Proxy
  178. - DIRECT
  179. proxy-providers:
  180. provider1:
  181. type: http
  182. url: "url"
  183. interval: 3600
  184. path: ./hk.yaml
  185. health-check:
  186. enable: true
  187. interval: 600
  188. url: http://www.gstatic.com/generate_204
  189. test:
  190. type: file
  191. path: /test.yaml
  192. health-check:
  193. enable: true
  194. interval: 36000
  195. url: http://www.gstatic.com/generate_204
  196. rules:
  197. - DOMAIN-SUFFIX,google.com,auto
  198. - DOMAIN-KEYWORD,google,auto
  199. - DOMAIN,google.com,auto
  200. - DOMAIN-SUFFIX,ad.com,REJECT
  201. # rename SOURCE-IP-CIDR and would remove after prerelease
  202. - SRC-IP-CIDR,192.168.1.201/32,DIRECT
  203. # optional param "no-resolve" for IP rules (GEOIP IP-CIDR)
  204. - IP-CIDR,127.0.0.0/8,DIRECT
  205. - GEOIP,CN,DIRECT
  206. - DST-PORT,80,DIRECT
  207. - SRC-PORT,7777,DIRECT
  208. # FINAL would remove after prerelease
  209. # you also can use `FINAL,Proxy` or `FINAL,,Proxy` now
  210. - MATCH,auto

5.下载前端代码压缩包

  1. wget https://github.com/haishanh/yacd/archive/gh-pages.zip

6.解压

  1. unzip gh-pages.zip

7.把目录名改成 dashboard

  1. mv yacd-gh-pages/ dashboard/

五.利用systemd,设置clash开机自启动(使用yuanlam用户登录)

1.创建service文件

  1. sudo touch /etc/systemd/system/clash.service

2.编辑service文件

打开service文件

  1. sudo nano /etc/systemd/system/clash.service

填入以下内容

  1. [Unit]
  2. Description=clash daemon
  3. [Service]
  4. Type=simple
  5. User=yuanlam
  6. ExecStart=/usr/bin/clash -d /home/yuanlam/.config/clash/
  7. Restart=on-failure
  8. [Install]
  9. WantedBy=multi-user.target

保存并退出

3.重新加载 systemd 模块

  1. sudo systemctl daemon-reload

4.启动Clash

  1. sudo systemctl start clash.service

5.设置Clash开机自启动

  1. sudo systemctl enable clash.service

以下为Clash相关的管理命令

  1. ## 启动Clash ##
  2. sudo systemctl start clash.service
  3. ## 重启Clash ##
  4. sudo systemctl restart clash.service
  5. ## 查看Clash运行状态 ##
  6. sudo systemctl status clash.service
  7. ## 实时滚动状态 ##
  8. sudo journalctl -u clash.service -f

六.配置防火墙转发规则(使用root用户登录)

这个步骤是踩坑最深的地方,网上看了很多教程,然后反复尝试,一直失败,最后才成功,到现在我都不明白究竟怎么回事,还在学习中,反正用下面的方法能成功

1.创建转发规则

创建sh文件

  1. nano ip.sh

输入以下内容(10.10.10.97:53为旁路由IP与Clash的DNS监听端口,7892为Clash的redir-port,根据实际情况修改)

  1. #!/bin/bash
  2. iptables -A INPUT -i lo -j ACCEPT
  3. iptables -A OUTPUT -o lo -j ACCEPT
  4. iptables -t nat -N clash
  5. iptables -t nat -N clash_dns
  6. iptables -t nat -A PREROUTING -p tcp --dport 53 -d 198.18.0.0/24 -j clash_dns
  7. iptables -t nat -A PREROUTING -p udp --dport 53 -d 198.18.0.0/24 -j clash_dns
  8. iptables -t nat -A PREROUTING -p tcp -j clash
  9. iptables -t nat -A clash_dns -p udp --dport 53 -d 198.18.0.0/24 -j DNAT --to-destination 10.10.10.97:53
  10. iptables -t nat -A clash_dns -p tcp --dport 53 -d 198.18.0.0/24 -j DNAT --to-destination 10.10.10.97:53
  11. iptables -t nat -A clash -d 1.0.0.0/8 -j ACCEPT
  12. iptables -t nat -A clash -d 10.0.0.0/8 -j ACCEPT
  13. iptables -t nat -A clash -d 100.64.0.0/10 -j ACCEPT
  14. iptables -t nat -A clash -d 127.0.0.0/8 -j ACCEPT
  15. iptables -t nat -A clash -d 169.254.0.0/16 -j ACCEPT
  16. iptables -t nat -A clash -d 172.16.0.0/12 -j ACCEPT
  17. iptables -t nat -A clash -d 192.168.0.0/16 -j ACCEPT
  18. iptables -t nat -A clash -d 224.0.0.0/4 -j ACCEPT
  19. iptables -t nat -A clash -d 240.0.0.0/4 -j ACCEPT
  20. iptables -t nat -A clash -d 192.168.1.2/32 -j ACCEPT
  21. iptables -t nat -A clash -p tcp --dport 22 -d 10.10.10.97/32 -j ACCEPT
  22. iptables -t nat -A clash -p tcp -j REDIRECT --to-ports 7892

赋予执行权限

  1. chmod +x ip.sh

执行(如没错误,应该没返回任何东西)

  1. bash ip.sh

2.利用ipupdown保存规则

此处感谢TG墙洞群的大佬 风蚀残叶 | 御伽話の夢 定めった夢 @wzw1997007 的耐心教导
保存规则

  1. sudo iptables-save > /etc/iptables.rules

创建ifup

  1. sudo nano /etc/network/if-pre-up.d/iptables

填入以下内容并保存退出

  1. #!/bin/bash
  2. iptables-restore < /etc/iptables.rules

赋予执行权限

  1. sudo chmod a+x /etc/network/if-pre-up.d/iptables

创建ifdown

  1. sudo nano /etc/network/if-post-down.d/iptables

填入以下内容并保存退出

  1. #!/bin/bash
  2. iptables-save > /etc/iptables.rules

赋予执行权限

  1. sudo chmod a+x /etc/network/if-post-down.d/iptables

七.修改网关,指向主路由(使用root用户登录)

打开文档,修改网关

  1. nano /etc/network/interfaces

根据实际情况修改

  1. auto lo
  2. auto eth0 #eth0为网卡,根据实际情况修改
  3. iface lo inet loopback
  4. iface eth0 inet static #static为静态,dhcp为动态
  5. address 10.10.10.97 #本机IP
  6. netmask 255.255.255.0 #子网掩码
  7. gateway 10.10.10.98 #网关,主路由IP地址
  8. dns-nameservers 223.6.6.6. #这里根据实际情况,填写本地运营商最友好的DNS
  9. mtu 1492 #MTU
  10. mss 1452 #MSS

重启网卡服务

  1. /etc/init.d/networking restart

重启系统

  1. reboot

以上,LINUX下的Clash已经可以正常运行

Enjoy!

PS:需要通过Clash科学上网的设备,可以使用以下两种方式进行配置

1.手动修改设备的网关跟DNS

2.通过主路由DHCP分配,推荐

  1. 网关:10.10.10.97 #旁路由IP
  2. DNS198.18.0.1 #Fake-ip(详情可以自行搜索关于Clashde的Fake-ip模式相关的文章)