1.前提准备
- 任意一台Linux主机
- Clash-for-Linux
- 请先关闭IPv6
Clash项目下载
Clash-for-Linux下载地址
这个里面的clash版本为clash-verge的内核。因为替换过后会添加一些新功能,若自己不放心的话可以自己寻找合适自己的Clash内核,能启动看到web页面即可。
**备注: 建议使用虚拟化出来的主机。否则会影响主机的网络链路导致网络错误。下面的命令以Debian 11 为例。在国内的情况下请添加国内源。如清华源或者阿里源. **
2.搭建过程
Debian安装命令
apt-get update
apt-get dist-upgrade
更新命令完成后。我们需要安装一些包。用来下载和解压我们需要操作的文件
apt-get install curl wget sudo git
操作完成后。我们开始使用命令拉取我们需要的软件包。
git clone https://github.com/EITSxiaozhai/clash-for-linux.git
后续步骤可以查看仓库中的说明文档
3.网络问题解决
当我们配置完成clash-Linux后。还需要对搭建好的服务器做网络转发。否则服务启动完成。Linux服务器还是无法对网络包进行处理。
开启 Linux 内核的转发功能
编辑配置文件 /etc/sysctl.conf 并向其中添加如下内容
net.ipv4.ip_forward=1
保存退出后,执行以下命令使修改生效
sysctl -p
查看 /proc/sys/net/ipv4/ip_forward 的内容,如果是 1 表示设置成功生效
4.旧版本Clash配置
目前较新版本的clash只需要打开ipv4转发即可正常使用了,如果你发现启动服务后,链接板块中没有链接数,代表服务没有被clash所转发,请先检查你的ipv4转发是否打开,如果打开了但是也还是没有链接数,请检查是否开启了ipv6,有时候clash无法解析到ipv6的地址,此外需要将需要翻墙的设备的dns,设置为旁路由的ip地址,除此之外如果还出现问题,那么则代表你还需要进行下面的额外设置
使用nftables进行代理转发,此方法适用与内网单独翻墙,且需要与外网进行通信的,如rdp协议
安装 nftables
apt -y install nftables
创建 nftables 配置文件扩展目录
mkdir /etc/nftables.conf.d
创建私有地址的定义文件
/etc/nftables.conf.d/private.nft,将下面的ip段写入刚刚创建的文件中
define private_list = {
0.0.0.0/8,
10.0.0.0/8,
127.0.0.0/8,
169.254.0.0/16,
172.16.0.0/12,
192.168.0.0/16,
224.0.0.0/4,
240.0.0.0/4
}
修改 nftalbes 配置文件,内容如下,注意,这个文件是另外一个,不是上方的ip段的文件,是为/etc/nftables.conf配置文件
#!/usr/sbin/nft -f
include "/etc/nftables.conf.d/private.nft"
table ip nat {
chain proxy {
ip daddr $private_list return
ip protocol tcp redirect to :7892
}
chain prerouting {
type nat hook prerouting priority 0; policy accept;
jump proxy
}
}
清空 nftalbes 规则,并使新规则生效
nft flush ruleset
nft -f /etc/nftables.conf
设置 nftalbes 开机自启动
systemctl enable nftables
配置文件写入
root@clash-server:/home/giao/clash-for-linux# ls
bin conf dashboard get-docker.sh logs mihomo-linux-amd64 README.md resolvbak.conf restart.sh scripts shutdown.sh start.sh temp tools
bin目录为内核目录,可以自行替换,Temp目录中文模板文件,当你下次启动时将会从这个目录中的 templete_config.yaml 中读取
root@clash-server:/home/giao/clash-for-linux/temp# ls
clash_config.yaml clash.yaml config.yaml proxy.txt templete_config.yaml
配置文件模板
dns:
listen: 0.0.0.0:53
enable: true
ipv6: true
enhanced-mode: redir-host
nameserver:
- 172.17.0.2
tun:
enable: true
stack: mixed
auto-route: true
auto-detect-interface: true
dns-hijack:
- any:53
strict-route: false
DNS设置以及其他错误排查
1.首先检查需要使用透明代理的主机。网络是否存在冲突。此外。DNS地址和网关地址是否指向透明代理,如果你是Linux需要设置旁路,请先了解你当前使用的系统,需要针对不同系统修改不同的网络文件
Debian系统
DNS文件
root@K8s-master:/home/k8smaster# cat /etc/resolv.conf
nameserver 1.1.1.1
网络配置文件
root@K8s-master:/home/k8smaster# cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug ens18
iface ens18 inet static
address 192.168.0.16
netmask 255.255.255.0
gateway 192.168.0.1
Ubuntu系统
DNS文件和Debian系统是一个路径,但是Ubuntu是使用Netplan进行网络管理,所以这个网络配置文件的地址是不一样的
cat /etc/netplan/00-installer-config.yaml
修改文件示例。
# This is the network config written by 'subiquity'
network:
ethernets:
ens160:
addresses:
- 192.168.8.85/24 #设置的固定IP地址
gateway4: 192.168.8.254 #网关
nameservers:
addresses:
- 192.168.8.1 #设置的DNS1
- 114.114.114.114 #设置的DNS2
version: 2
2.如果发现还不行。请检查你的DNS文件。
cat /etc/resolv.conf
如果你发现是类似于下图

那么则是因为你开启了系统解析。常见Ubuntu
sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
sudo rm /etc/resolv.conf
sudo touch /etc/resolv.conf
然后文件写入clash的ip,这样就可以修复网络和DNS的问题了。
评论区