Cover

使用 Clash 为 Linux 提供网络代理服务

· Maintenance

在近期架构方案制定中,出口流量的分配成为一个显著的重要的问题,而最好的解决方式就是网络代理。通过Clash分流以实现对不同目标的流量分流,搭配NAT网关以配置更优的VPC。

关于 Clash

Dreamacro/clash

Clash 是一个由 Golang 撰写的网络代理程序,支持ShadowSocks/SSR节点,支持以http/socks5协议进行代理。 得益于 Golang 的高性能和跨平台开发能力,Clash 对于各个系统均有Release,因此你可以轻松地在Linux 终端运行和配置 Clash。

使用 Clash 代理流量

本章节将讲述详细的配置步骤。 在 CentOS 与 Ubuntu 系统均实测可正常使用,其他系统请自行根据教程步骤加以修正更改。

安装

你可以在 Clash 的 GitHub Release 页面获取编译好的 Clash 软件。

# 下载当前操作系统与 CPU 架构对应的包文件
wget -O clash.gz https://github.com/Dreamacro/clash/releases/download/v1.10.0/clash-linux-amd64-v1.10.0.gz

下载好后解压安装包中 clash 到 /usr/local/bin/ 目录下,以此在全局环境运行软件,并删除压缩包文件。

gzip -dc clash.gz > /usr/local/bin/clash
chmod +x /usr/local/bin/clash
rm -f clash.gz

创建配置文件目录,并下载 MMDB 文件。

MMDB文件可以提供IP与地理位置相对应的数据信息,可以更精确地确认IP所对应地域,以此提高网络分流的效率和质量。

mkdir /etc/clash
wget -O /etc/clash/Country.mmdb https://www.sub-speeder.com/client-download/Country.mmdb

创建 systemd 脚本,以使程序持久化运行。

vi /etc/systemd/system/clash.service

内容如下

[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

重载 systemd

systemctl daemon-reload

配置代理

首先你需要将自己的 yaml 配置文件上传至服务器的 /etc/clash/config.yaml 文件中,例如可以通过 wget 下载网络上的配置文件。

wget -O /etc/clash/config.yaml https://example.com/config.yaml

配置环境

设置系统代理,添加配置文件 /etc/profile.d/proxy.sh 并在其中写入如下内容

# 进入文件
touch /etc/profile.d/proxy.sh
vi /etc/profile.d/proxy.sh
# 写入内容
export http_proxy="127.0.0.1:7890"
export https_proxy="127.0.0.1:7890"
export no_proxy="localhost, 127.0.0.1"

重载 /etc/profile 配置

source /etc/profile

启动 Clash 服务,并设置为开机自启动

systemctl start clash
systemctl enable clash

测试

在大多数的预安装的 Linux 操作系统中均可以使用 curl ,你可以通过 curl 测试

curl www.google.com

Comments

Send Comments

Markdown supported. Please keep comments clean.