Headscale与tailscale简单使用体验

前言

通过Headscale和tailscale可以实现远程组建局域网,远程访问家中资源的目的。

或者在A地访问B地资源较慢的时候,可以通过Headscale+tailscale组件局域网,加速访问资源。

Or可以让多台设备加入一个公网VPS,让不同地点的用户访问对方的本地资源等。

准备

1,准备一个具有公网ip的vps,一个主机(个人设备,用于接入VPS搭建的局域网中)。

2,在vps上安装headscale,在主机上安装tailscale。

安装Headscale

按照官网的方式进行安装:https://headscale.net/running-headscale-linux/#installation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# 更新
sudo apt update
sudo apt upgrade
# 下载安装包,注意修改flag里面的标志,这里的版本时根据repo中的最新版本修改
wget --output-document=headscale.deb https://github.com/juanfont/headscale/releases/download/v0.22.3/[flag:headscale_0.xx.xx_linux_amd64.deb]
# 安装deb包
sudo dpkg --install headscale.deb
systemctl daemon-reload  # 重新载入配置文件
systemctl enable --now headscale  # 开机自动启动
sudo systemctl enable headscale
sudo systemctl start headscale

修改配置文件内容

1
2
3
4
5
# 编辑配置文件
vim /etc/headscale/config.yaml
# 修改以下内容
server_url:http://[flag:your vps public ip address]:8080
listen_url:0.0.0.0:8080
1
2
# 新建命名空间
headscale users create [flag:namespace]  # 最后参数为命名空间的名字

一些安装信息

1
2
3
4
5
6
# 数据库安装位置
/var/lib/headscale/db.sqlite
# 如果配置文件出错,重新下载配置文件
wget https://github.com/juanfont/headscale/raw/main/config-example.yaml -O /etc/headscale/config.yaml  
# 如果报错,可能需要修改配置文件中的
unix_socket: /var/lib/headscale/headscale.sock

安装Tailscale

如果想要实现在VPS上搭建局域网用于多用户实现访问某个资源的ip看起来是一样的,需要在VPS上安装Headscale和Tailscale,即同时作为服务端和一个节点。

Linux

下载客户端:https://tailscale.com/kb/1039/install-ubuntu-2004/

1
2
3
4
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/focal.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list
sudo apt-get update
sudo apt-get install tailscale

如果无法直接弹出注册的token就按照提示命令,直到弹出nodekey

1
2
3
4
# 等号后面的是安装headscale的主机,默认端口8080 回显会有..... nodekey:...
tailscale up --login-server=http://[flag: your vps public ip address]:8080 
# 接受注册 把nodekey按照下面的格式在安装了headscale的主机上运行bash
headscale --user [flag:name of registed user] nodes register --key nodekey:xxxxx

Windows

访问http://[flag: your vps public ip address]:8080/windows按照步骤,添加注册表,输入命令,在VPS注册节点。

外部节点(Exit node)

添加外部节点

将部署有headscale的机器作为外部节点,也就是将vps本机加入node的方式,实现通过外部节点访问互联网的目的(比如从局域网)。

1
2
3
4
5
6
7
sudo tailscale set --exit-node [flag: VPS hostname]
tailscale up --advertise-exit-node --login-server=http://[flag: your vps public ip address]:8080
sudo tailscale up --advertise-exit-node
 # 运行完上面的命令之后,在vps上查看路由
headscale routes list
 # 开启路由:标志位是上面命令现实出来的第一列参数:ID,在一个外部节点的情况下直接enable前两个路由
 headscale routes enable -r [flag]
1
sudo tailscale set --exit-node 外部节点名称

删除节点

1
headscale node delete -i 

如果需要将Windows机器作为内网转发的机器,需要设置Windows的转发规则,并login到server。

更改节点

1
2
3
4
5
6
# 删除节点
headscale nodes delete [flags]
# x 这里指代的是第一列标志的ID序号
headscale nodes delete -i x
# 启动路由
headscale node route enable -a -i

启动代理

访问外部网络的主机上(这里是linux),如在局域网 使用exit node:

1
 sudo tailscale set --exit-node [flag: exit node ] 

切换服务端

windows平台先修改注册表,然后访问新的注册网址。

访问新的内网ip对应端口的windows,:

1
http://[flag: your vps public ip address]:8080/windows

HKLM\Software\Tailscale IPN 的值改为新的ip地址的值,重新用注册机器的方式注册一遍。

Headscale常用状态检测命令

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 查看所有命名空间
headscale namespace list
# 列出所有节点信息
headscale node list
# 查看部署日志
journalctl -u headscale
# 查看运行状态
systemctl status headscale
# 重启服务
service headscale restart

参考文章

https://headscale.net/

https://github.com/juanfont/headscale

https://icloudnative.io/posts/how-to-set-up-or-migrate-headscale

comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy