headscale


搭建内网穿透服务器

安装

1
2
3
1. 从https://github.com/juanfont/headscale/releases下载合适的版本
2. 修改权限 chmod +x headscale_0.20.0_linux_amd64
3. 移动到bin mv headscale_0.20.0_linux_amd64 /usr/local/bin/headscale

环境

1
2
3
4
5
6
7
8
9
10
11
# 创建配置目录
mkdir -p /etc/headscale
# 创建目录用来存储数据与证书
mkdir -p /var/lib/headscale
# 创建空的 SQLite 数据库文件
touch /var/lib/headscale/db.sqlite
# 下载 Headscale 配置文件
wget https://github.com/juanfont/headscale/raw/main/config-example.yaml -O /etc/headscale/config.yaml
# 创建用户
useradd headscale -d /home/headscale -m
chown -R headscale:headscale /var/lib/headscale

配置

1
2
3
4
5
6
服务器开放8080端口
修改/etc/headscale/config.yaml文件

server_url:改为自己的ip:8080
magic_dns:改为false
listen_addr: 0.0.0.0:8080

创建server文件

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
# /etc/systemd/system/headscale.service
[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

# Optional security enhancements
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
4
5
systemctl daemon-reload
systemctl enable --now headscale
systemctl status headscale

使用ss -tulnp|grep headscale查看端口占用是否正常

命名空间

1
2
创建: headscale namespaces create default
查看: headscale namespaces list

linux接入

1
2
3
4
5
6
7
接入: tailscale up --login-server=http://你的ip:8080 --accept-routes=true --accept-dns=false

会产生一个地址,访问地址会看到一串命令,在服务端的主机上执行命令加入成功

headscale nodes register --user 你创建的命名空间 --key nodekey:*********

服务端查看节点 headscale nodes list