Linux服务器配置代理

Linux下使用Clash科学上网

在 Linux 服务器上通过 Clash 科学上网
如果出现权限不足的情况请在指令前面加上 sudo

安装Clash

  • 下载当前操作系统与 CPU 架构对应的包文件,我这儿是 X86_64 平台下的Ubuntu所以对应使用的是clash-linux-amd64-v1.6.5.gz即ok(当然取github上面找到不同的安装包只要能够对应也ok)

    1
    wget -O clash.gz https://github.com/Dreamacro/clash/releases/download/v1.6.5/clash-linux-amd64-v1.6.5.gz
  • 下载好后解压安装包中 clash 到 /usr/local/bin/ 目录下,并删除压缩包文件

    1
    2
    3
    gzip -dc clash.gz > /usr/local/bin/clash
    chmod +x /usr/local/bin/clash
    rm -f clash.gz
  • 创建配置文件目录,并下载 MMDB 文件(注意这一步很可能失败建议直接在网上找Country.mmdb文件下载并手动上传比较好) 下载网址

    1
    2
    mkdir /etc/clash
    wget -O /etc/clash/Country.mmdb https://www.sub-speeder.com/client-download/Country.mmdb
  • 创建 systemd 脚本,脚本文件路径为 /etc/systemd/system/clash.service,内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [Unit]
    Description=clash daemon

    [Service]
    Type=simple
    User=root
    ExecStart=/usr/local/bin/clash -d /etc/clash/
    Restart=on-failure

    [Install]
    WantedBy=multi-user.target
  • 重载 systemctl daemon

    1
    systemctl daemon-reload

配置代理

  • 导入已有的vpn 链接 (订阅链接啦)

    1
    wget -O /etc/clash/config.yaml [你的订阅链接]
  • 设置系统代理,添加配置文件 /etc/profile.d/proxy.sh 并在其中写入如下内容:

    1
    2
    3
    4
    export http_proxy="http://127.0.0.1:7890"
    export https_proxy="http://127.0.0.1:7890"
    export HTTP_PROXY="http://127.0.0.1:7890"
    export HTTPS_PROXY="http://127.0.0.1:7890"
  • 重载 /etc/profile 配置

    1
    source /etc/profile
  • 启动 clash 服务,并设置为开机自动启

    1
    2
    systemctl start clash
    systemctl enable clash
  • 测试 goolge.com 访问

    1
    2
    3
    4
    5
    6
    7
    # curl google.com
    <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
    <TITLE>301 Moved</TITLE></HEAD><BODY>
    <H1>301 Moved</H1>
    The document has moved
    <A HREF="http://www.google.com/">here</A>.
    </BODY></HTML>

配置web-UI

  • 克隆 clash-dashboard 项目到本地

    1
    git clone -b gh-pages --depth 1 https://github.com/Dreamacro/clash-dashboard /opt/clash-dashboard
  • 修改 clash 配置文件中 external-ui 的值为 /opt/clash-dashboard

    1
    sed -i "s/^#\{0,1\} \{0,1\}external-ui.*/external-ui: \/opt\/clash-dashboard/" /etc/clash/config.yaml
  • 重启clash服务

    1
    systemctl restart clash
  • 通过浏览器访问 localhost:9090/ui,其中 localhost 替换为 clash 部署服务器的 IP

配置定时更新订阅

使用如下脚本填写相关配置项目并放入 /etc/cron.weekly 目录下,每周自动更新订阅配置文件即可
sudo vim /etc/cron.weekly/clash.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env bash

# 订阅链接地址
SUBSCRIBE=""
# web-ui存放目录,留空则保持默认不修改
WEB_UI=""
# API 端口,留空则保持默认不修改
CONTROLLER_API_PROT=""
# API 口令,留空则保持默认不修改
SECRET=""

CLASH_CONFIG="/etc/clash/config.yaml"


if [ -z "${SUBSCRIBE}" ]; then
echo "Subscription address cannot be empty"
exit 1
fi

systemctl stop clash

wget --no-proxy -O ${CLASH_CONFIG} ${SUBSCRIBE}

if [ -n "${WEB_UI}" ]; then
sed -i "s?^#\{0,1\} \{0,1\}external-ui.*?external-ui: ${WEB_UI}?" ${CLASH_CONFIG}
fi

if [ -n "${CONTROLLER_API_PROT}" ]; then
sed -i "s?^external-controller.*?external-controller: '0.0.0.0:${CONTROLLER_API_PROT}'?" ${CLASH_CONFIG}
fi

if [ -n "${SECRET}" ]; then
sed -i "s?^secret.*?secret: '${SECRET}'?" ${CLASH_CONFIG}
fi

systemctl start clash

上述脚本写入 /etc/cron.weekly/clash.sh 并配置好相关变量后,保存退出并赋予可执行权限
chmod 0755 /etc/cron.weekly/clash.sh
至此,Linux 下 clash 配置完成啦!!