内容具有时效性,一切以官方文档 为准
讲在前面 Headscale
是 Tailscale
的开源版本,前者的免费使用有一定的限制,当然也能满足一般的需求 如果你不想折腾,也没有太多的需求,Tailscale
或许是更好的选择 另外这也是一篇基础教程,更多的使用请阅读官方文档
服务端 首先肯定需要把服务跑起来,只有一个要求,就是有公网 IP 另外推荐使用 Linux
部署服务,Windows
的话可以试试 Docker
部署 文章服务端环境:Debian 11
腾讯云轻量应用服务器
一些准备工作 准备好一个域名,SSL
证书(可选)
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 wget --output-document=/usr/local/bin/headscale \ https://github.com/juanfont/headscale/releases/download/v0/headscale_0_linux_amd64 chmod +x /usr/local/bin/headscalemkdir -p /etc/headscalemkdir -p /var/lib/headscaletouch /var/lib/headscale/db.sqlitetouch /etc/headscale/config.yamlwget --output-document=/etc/headscale/config.yaml \ https://github.com/juanfont/headscale/raw/main/config-example.yaml
修改配置文件 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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 server_url: http://test.domain.com:8080 listen_addr: 0.0 .0 .0 :8080 metrics_listen_addr: 127.0 .0 .1 :9090 grpc_listen_addr: 0.0 .0 .0 :50443 grpc_allow_insecure: false private_key_path: /var/lib/headscale/private.key ip_prefixes: - fd7a:115c:a1e0::/48 - 100.64 .0 .0 /10 disable_check_updates: false ephemeral_node_inactivity_timeout: 30m log_level: info acl_policy_path: "" unix_socket: /var/run/headscale.sock unix_socket_permission: "0770" derp: server: enabled: true region_id: 999 region_code: "headscale" region_name: "Headscale Embedded DERP" stun_listen_addr: "0.0.0.0:3478" urls: [] paths: [] auto_update_enabled: true update_frequency: 24h db_type: sqlite3 db_path: /var/lib/headscale/db.sqlite acme_url: https://acme-v02.api.letsencrypt.org/directory acme_email: "[email protected] " tls_letsencrypt_hostname: "test.domain.com" tls_client_auth_mode: disabled tls_letsencrypt_cache_dir: /var/lib/headscale/cache tls_letsencrypt_challenge_type: HTTP-01 tls_letsencrypt_listen: ":http" tls_key_path: "/var/lib/headscale/cache/key.pem" tls_cert_path: "/var/lib/headscale/cache/cert.pem" dns_config: nameservers: - 1.1 .1 .1 domains: [] magic_dns: false base_domain: example.com
启动 1 2 3 4 5 6 7 8 9 headscale serve curl http://127.0.0.1:9090/metrics headscale namespaces create myfirstnamespace
注意需要将服务器端口加入防火墙规则
连接服务器 需要先下载 Tailscale
下载地址
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 tailscale up --login-server YOUR_HEADSCALE_URL --accept-dns=false headscale --namespace myfirstnamespace nodes register --key <YOU_+MACHINE_KEY> headscale --namespace myfirstnamespace \ preauthkeys create --reusable --expiration 24h tailscale up --login-server <YOUR_HEADSCALE_URL> \ --authkey <YOUR_AUTH_KEY> --accept-dns=false
将 Headscale
注册为服务 创建 headscale
用户 1 2 useradd headscale -d /home/headscale -m chown headscale:headscale /var/lib/headscale
修改 config.yaml
1 unix_socket: /var/run/headscale/headscale.sock
添加 service
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 [Unit] Description=headscale controller After=syslog.target After=network.target [Service] Type=simple User=headscale Group=headscale ExecStart=/usr/local/bin/headscale serve Restart=always RestartSec=5 NoNewPrivileges=yes PrivateTmp=yes ProtectSystem=strict ProtectHome=yes ReadWritePaths=/var/lib/headscale /var/run/headscale AmbientCapabilities=CAP_NET_BIND_SERVICE RuntimeDirectory=headscale [Install] WantedBy=multi-user.target
启动服务 1 2 3 systemctl daemon-reload systemctl enable --now headscale systemctl status headscale
其他 一些简单的命令 1 2 3 4 5 6 headscale namespaces list headscale nodes list headscale routes list -i 6 tailscale status tailscale ping xx.xx.xx.xx
子网路由 可能存在将整个局域网连接进去,需要添加子网路由
1 2 3 4 5 6 7 8 9 10 11 12 13 echo 'net.ipv4.ip_forward = 1' | tee /etc/sysctl.d/ipforwarding.confecho 'net.ipv6.conf.all.forwarding = 1' | tee -a /etc/sysctl.d/ipforwarding.confsysctl -p /etc/sysctl.d/ipforwarding.conf tailscale up --login-server YOUR_HEADSCALE_URL \ --accept-dns=false --advertise-routes=192.168.100.0/24 headscale nodes list headscale routes list -i x headscale routes enable -i x -r "192.168.100.0/24" headscale routes list -i x
之后就可以 ping 到另一个子网的所有主机了
Docker
方式1 2 3 4 5 6 7 8 9 docker run \ --name headscale \ --detach \ --rm \ --volume $(pwd ):/etc/headscale/ \ --publish 0.0.0.0:8080:8080 \ --publish 127.0.0.1:9090:9090 \ headscale/headscale:0.15.0 \ headscale serve
参考