搭建 headscale 异地组网完整教程

普通版

  1. 部署Headscale控制服务器

可以直接下载安装包安装(新版本去github查看)

apt -y update
apt -y install wget
wget https://github.com/juanfont/headscale/releases/download/v0.22.3/headscale_0.22.3_linux_amd64.deb
dpkg -i headscale_0.22.3_linux_amd64.deb
systemctl enable headscale
  1. 编辑Headscale的配置文件:

vim /etc/headscale/config.yaml

修改内容:

server_url: https://headscale.example.com # 修改为你的域名,无域名可直接使用IP
listen_addr: 0.0.0.0:8080
metrics_listen_addr: 0.0.0.0:9090

启动并检查服务

systemctl start headscale
systemctl status headscale
  1. 反向代理

安装nginx和certbot

apt -y install nginx python3-certbot-nginx

新建NGINX站点配置文件:

vim /etc/nginx/sites-available/headscale

写入配置

map $http_upgrade $connection_upgrade {
    default      keep-alive;
    'websocket'  upgrade;
    ''           close;
}

server {
    listen 80;
    server_name headscale.example.com; # 修改为你的域名

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $server_name;
        proxy_redirect http:// https://;
        proxy_buffering off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
        add_header Strict-Transport-Security "max-age=15552000; includeSubDomains" always;
    }

    error_log /var/log/nginx/headscale-proxy-error.log;
    access_log /var/log/nginx/headscale-proxy-access.log;
}

启用站点:

ln -s /etc/nginx/sites-available/headscale /etc/nginx/sites-enabled/headscale

签发SSL证书:

certbot --nginx
  1. 使用Headscale

首先需要在Headscale控制服务器创建一个用户:

headscale users create meow
  • 特殊指令
headscale --user user001 preauthkeys create --reusable --expiration 10000d

为user001用户[user]生成一个可重复使用[reusable]的10000天[expiration]的密钥(可在客户端自动连接服务端)

headscale -n default preauthkeys list

查看已生成的密钥

随后用Tailscale客户端接入即可
Windows客户端下载:https://pkgs.tailscale.com/stable/tailscale-setup-latest.exe
其他平台的客户端下载:https://tailscale.com/download/

接入命令(除手机和MAC命令方法基本一致):
在客户端登录到我们自建的Headscale:

tailscale login --login-server https://headscale.example.com

随后会获得一串密钥,在服务端使用命令授权注册:

headscale nodes register--user meow --key nodekey:xxxxx

可使用命令查看客户端加入情况:

headscale nodes list
  • 安卓版使用自定服务器,需要在软件首页点开右上方的三点,选择About,然后关闭,重复操作5-6次,即可出现change server填写自定服务器
  • 特殊指令
tailscale login --login-server https://123.com --authkey 3e94776e9a1ddfd25eac0fxd5e8ccd1de98e89d746e61e91 --advertise-routes=100.64.0.0/10 --accept-routes=true --accept-dns=false
  • --advertise-routes=100.64.0.0/10 [打通当前内网设备路由使所有客户端可直接访问]
  • --accept-routes=true [是否接受服务端下发的用于路由到其他客户端内网的路由规则]
  • --accept-dns=false [是否使用服务端下发的 DNS 相关配置]
  • --reset [重置连接信息,连不上的时候可以使用]

    Headscale 的部署到此基本结束