转载自:https://little-star.love/posts/5d083060/

安装 clash

  1. 下载 clash

    wget https://github.com/Dreamacro/clash/releases/download/v1.8.0/clash-linux-amd64-v1.8.0.gz
  2. 解压并安装 clash 二进制文件

    gzip -d clash-linux-amd64-v1.8.0.gz
    install clash-linux-amd64-v1.8.0 /usr/local/bin/clash
  3. 创建 clash 的 systemd 配置文件

    /etc/systemd/system/clash.service
    
    [Unit]
    Description=Clash daemon, A rule-based proxy in Go.
    After=network.target
    
    [Service]
    Type=simple
    Restart=always
    ExecStart=/usr/local/bin/clash -d /etc/clash
    
    [Install]
    WantedBy=multi-user.target
  4. 设置 clash 为开机自动启,并启动该服务

    systemctl enable clash
    systemctl start clash
  5. 查看 clash 服务状态

    systemctl status clash
  6. 导入 clash web 管理界面

    git clone -b gh-pages --depth 1 https://github.com/Dreamacro/clash-dashboard /opt/clash-dashboard

流量转发

开启 Linux 内核的转发功能

注意:如果不开启该功能则可能存在内网设备无法访问问题


  1. 编辑配置文件 /etc/sysctl.conf 并向其中添加如下内容

    /etc/sysctl.conf
    net.ipv4.ip_forward=1
  2. 保存退出后,执行以下命令使修改生效

    sysctl -p
  3. 查看 /proc/sys/net/ipv4/ip_forward 的内容,如果是 1 表示设置成功生效

nftables

使用 nftables 替代 iptables 做流量转发,如果不想用 nftables 也可以使用添加如下 iptables 规则

iptables -t nat -N clash
iptables -t nat -A CLASH -d 0.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 10.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 127.0.0.0/8 -j RETURN
iptables -t nat -A CLASH -d 169.254.0.0/16 -j RETURN
iptables -t nat -A CLASH -d 172.16.0.0/12 -j RETURN
iptables -t nat -A CLASH -d 192.168.0.0/16 -j RETURN
iptables -t nat -A CLASH -d 224.0.0.0/4 -j RETURN
iptables -t nat -A CLASH -d 240.0.0.0/4 -j RETURN
iptables -t nat -A clash -d 0.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 10.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 127.0.0.0/8 -j RETURN
iptables -t nat -A clash -d 169.254.0.0/16 -j RETURN
iptables -t nat -A clash -d 172.16.0.0/12 -j RETURN
iptables -t nat -A clash -d 192.168.0.0/16 -j RETURN
iptables -t nat -A clash -d 224.0.0.0/4 -j RETURN
iptables -t nat -A clash -d 240.0.0.0/4 -j RETURN
iptables -t nat -A clash -p tcp -j REDIRECT --to-port 7892
iptables -t nat -A PREROUTING -p tcp -j clash
  1. 安装 nftables

    apt -y install nftables
  2. 创建 nftables 配置文件扩展目录

    mkdir /etc/nftables.conf.d
  3. 创建私有地址的定义文件

    /etc/nftables.conf.d/private.nft
    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
    }
  4. 修改 nftalbes 配置文件,内容如下

    /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
        }
    }
  5. 清空 nftalbes 规则,并使新规则生效

    nft flush ruleset
    nft -f /etc/nftables.conf
  6. 查看 nftalbes 当前规则

    nft list ruleset
  7. 设置 nftalbes 开机自启动

    systemctl enable nftables