一款比 Tailscale 更好的內(nèi)網(wǎng)穿透方案 Headscale
環(huán)境
- Ubuntu 18.04.3(理論上正常支持 systemctl 的 Linux 發(fā)行版本都能搭建)
- 端口 8080
- 版本為 v0.11
注意事項(xiàng)
官方的當(dāng)前時(shí)間也是本文章撰寫的時(shí)間。最新版本的配置文件不兼容版本號(hào)為 v0.12.1
搭建過(guò)程
下載二進(jìn)制文件與源代碼
文件
上傳 headscale_0.11.0_linux_amd64 文件到/usr/local/bin并重命名為headscale并賦予執(zhí)行權(quán)限
$ chmod 755 /usr/local/bin/headscale
登錄服務(wù)器創(chuàng)建目錄及文件
$ mkdir /etc/headscale
$ cd /etc/headscale
$ touch db.sqlite
$ touch config.yaml
$ touch derp.yaml
注意在 0.12 版本已不需要生成密鑰會(huì)自動(dòng)生成,或許需要安裝wireguard我們是 0.11 版本所以需要手動(dòng)生成
$ apt-get update
$ apt-get install wireguard
$ wg genkey > private.key
最后目錄結(jié)構(gòu)如下
root@10-255-1-181:/etc/headscale# tree
.
├── config.yaml
├── db.sqlite
├── derp.yaml
└── private.key
0 directories, 4 files
root@10-255-1-181:/etc/headscale#
填寫 config.yaml 內(nèi)容,內(nèi)容來(lái)源于解壓的 v0.11.0.zip 壓縮包 config-example.yaml 內(nèi)容
config-example
填寫 derp.yaml 內(nèi)容內(nèi)容來(lái)源于解壓的 v0.11.0.zip 壓縮包 derp-example.yaml 內(nèi)容
derp-example
撰寫headscale.service
# /etc/systemd/system/headscale.service
[Unit]
Description=headscale
After=network.target
[Service]
WorkingDirectory=/etc/headscale
ExecStart=/usr/local/bin/headscale serve
# Disable debug mode
Environment=GIN_MODE=release
[Install]
WantedBy=multi-user.target
重載service與設(shè)置開機(jī)啟動(dòng)
$ systemctl daemon-reload
$ systemctl enable headscale
$ systemctl start headscale
狀態(tài)
功能
首先看一下幫助文件
幫助
組網(wǎng)互聯(lián)功能
控制端命令行下創(chuàng)建命名空間myfirstnamespace可以理解用戶?
$ headscale namespaces create myfirstnamespace
客戶端執(zhí)行:
$ tailscale up --login-server YOUR_HEADSCALE_URL
##例子tailscale up --login-server http://headscale.gao4.top:8080
YOUR_HEADSCALE_URL 為服務(wù)器鏈接。
返回服務(wù)端命令行執(zhí)行下面命令,意思是加入一個(gè) myfirstnamespace 命名空間,YOURMACHINEKEY 為上面命令返回的密鑰
$ headscale -n myfirstnamespace nodes register -k YOURMACHINEKEY
子網(wǎng)路由
當(dāng)前時(shí)間段關(guān)于 headscale 中文教程谷歌幾乎搜索不到,可以參考這個(gè)英文教材:
Linux 下設(shè)置 Ipv4 與 ipv6 路由轉(zhuǎn)發(fā)
$ echo 'net.ipv4.ip_forward = 1' | tee /etc/sysctl.d/ipforwarding.conf
$ echo 'net.ipv6.conf.all.forwarding = 1' | tee -a /etc/sysctl.d/ipforwarding.conf
$ sysctl -p /etc/sysctl.d/ipforwarding.conf
客戶端通告網(wǎng)段
$ tailscale up --advertise-routes=192.168.0.0/24 --login-server=https://headscale.domain.com
查看路由開啟可以看到結(jié)果是關(guān)閉的:
$ headscale routes list --namespace $NAMESPACE $NODE_NAM
Route | Enabled
192.168.0.0/24 | false
開啟路由
$ headscale routes enable --namespace $NAMESPACE $NODE_NAME 192.168.0.0/24
查看結(jié)果
$ headscale routes list --namespace $NAMESPACE $NODE_NAME
Route | Enabled
192.168.0.0/24 | true
其他節(jié)點(diǎn)查看路由結(jié)果
$ tailscale up --accept-routes --login-server=https://headscale.domain.com