🧩 第04章 · 搭建远程协助服务器
服务端 + 客户端 + 端口
搭建远程协助服务器¶
RustDesk 是开源的远程协助/远程桌面工具,自建服务器后可以自己掌控 ID 服务器和公钥,不依赖别人。本章部署它的核心组件 hbbs(中继 ID/relay)和 hbbr(中继/relay 服务器),并让客户端连上自家服务器。
本章目标:把 RustDesk 服务端跑在本机,客户端填上 gitlab.bangdream.xyz 风格的自定义 ID 服务器和公钥后,即可通过自己控制的服务器建立远程连接。
1. 了解端口与网络¶
RustDesk 需要放行一组端口,TCP 和 UDP 都要。
| 端口 | 协议 | 作用 |
|---|---|---|
| 21115 | tcp | NAT 类型测试 |
| 21116 | tcp+udp | ID 注册与中继(TCP 21116 用于 TCP 打洞,UDP 21116 用于打洞握手/ID 服务器) |
| 21117 | tcp | 中继(relay)服务 |
| 21118/21119 | tcp | 网页(web 客户端,可选) |
⚠️ 易错:很多人只放行 TCP,忘了 21116/UDP。打洞和保持连接高度依赖 UDP,UDP 不通会导致能连上但很快断,或 ID 服务器握手失败。
云安全组 + 服务器内防火墙都要放行:
# 云安全组放行: 21115-21117/tcp, 21116/udp, (可选 21118-21119/tcp)
# 服务器内 ufw
sudo ufw allow 21115:21119/tcp
sudo ufw allow 21116/udp
sudo ufw status
2. 安装 RustDesk 服务端¶
提供 .deb 包(走 systemd,简单)或静态二进制(自定义更多)。这里用官方 deb。
# 以官方 deb 为例(下载最新 amd64 包,版本以官网为准)
wget https://github.com/rustdesk/rustdesk-server/releases/download/20250124-0848/rustdesk-server_20250124-0848_amd64.deb
sudo dpkg -i rustdesk-server_*.deb
⚠️ 易错:版本号要自己查最新 release(
rustdesk-server的 release 页面)。复用这里的版本号下载可能 404。装完后确认二进制路径:which hbbs hbbr(deb 装进/usr/bin)。
用 systemd 把 hbbs 和 hbbr 托管起来:
# hbbs —— 关闭 / 放开端口的参数要一致
sudo useradd -r -s /usr/sbin/nologin rustdesk 2>/dev/null || true
# /etc/systemd/system/hbbs.service
[Unit]
Description=RustDesk ID/Relay Server (hbbs)
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/bin/hbbs -r gitlab.bangdream.xyz:21117
Restart=always
RestartSec=3
LimitNOFILE=1048576
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/hbbr.service
[Unit]
Description=RustDesk Relay Server (hbbr)
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/bin/hbbr
Restart=always
RestartSec=3
LimitNOFILE=1048576
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now hbbs hbbr
systemctl status hbbs hbbr --no-pager | grep -E 'active|listen'
⚠️ 易错:
hbbs -r的-r需要指向这台服务器的公网域名/公网 IP:21117。如果填了127.0.0.1,客户端能连上但中继数据出不去。hbbs要保持-r <你的中继地址>才能引导客户端连到正确的 relay。
3. 重启后自启与开机稳定性¶
# 确认设置
systemctl is-enabled hbbs hbbr
# 查看端口监听
ss -tulnp | grep -E '21115|21116|21117'
4. 配置客户端:ID 服务器 + 公钥¶
RustDesk 客户端需要你指定自己的服务器,否则默认连官方公共服务器(可控性差)。客户端界面(需切换到「网络」/「高级」设置):
- 打开 RustDesk 客户端 →
设置→网络 - 勾选「使用自建服务器」
- ID 服务器:填
gitlab.bangdream.xyz(你的服务器域名/IP) - Key(公钥):填从服务器上获取的
publuc_key(见下一步)
# 从服务器取公钥(hbbs 启动后在运行目录生成,默认 /root 或 /var/lib/rustdesk)
# 公钥文件通常是 id_ed25519.pub 形式
sudo find / -name "*.pub" -path "*rustdesk*" 2>/dev/null | head
sudo cat /root/.local/share/rustdesk/id_ed25519.pub 2>/dev/null \
|| sudo find / -name "id_ed25519.pub" 2>/dev/null | head -1 | xargs sudo cat
取到的那串字符串就是 Key,复制到客户端。
| # | 客户端配置项 | 值 |
|---|---|---|
| 1 | 自定义服务器 | 勾选 |
| 2 | ID 服务器 | gitlab.bangdream.xyz |
| 3 | Key | id_ed25519.pub 文件内容 |
🔒 安全:
Key是远程连接的身份凭据,别泄露。客户端和服务器端的 Key 必须匹配,否则握手失败、连不上。如果换了 Key,所有客户端都要同步更新。
5. 测试连接¶
# 服务端确认端口在监听
ss -tulnp | grep -E '2111[5-9]'
# 本地可用 hbbr 的端口扫描确认
curl -s telnet://gitlab.bangdream.xyz:21117 2>&1 | head -1 || nmap -p 21115-21117 gitlab.bangdream.xyz
在两台不同设备(如手机和电脑)上安装客户端,均填入同一服务器和 Key。用 A 设备给 B 设备发「控制」请求,看能否建立并稳定保持。
⚠️ 易错:测试别用本机连本机(localhost 掩盖了防火墙/公网问题)。必须用外网设备。只能连上但断断续续,99% 是 21116/UDP 没放行。
6. 常见故障排查¶
| 现象 | 可能的根因 | 排查命令 |
|---|---|---|
| 客户端连不上 | ID 服务器地址写错 / 端口没开 | ss -tulnp grep 2111 |
| 能连但秒断 | 21116/UDP 没放行 | 云安全组确认 udp |
| 报 Key 错误 | 客户端与服务器 Key 不匹配 | 重新复制公钥 |
| 中继慢 | hbbs -r 指向的不对 |
确认 -r 是公网地址 |
| 服务起不来 | 依赖缺失 / 磁盘满 | journalctl -u hbbs -f |
写一个健康检查脚本并定时探活:
sudo tee /usr/local/bin/check-rustdesk.sh >/dev/null <<'EOF'
#!/bin/bash
for p in 21115 21116 21117; do ss -tln | grep -q ":$p " || echo "$(date) port $p not listening"; done
EOF
sudo chmod +x /usr/local/bin/check-rustdesk.sh
7. 完成清单¶
| # | 验证点 | 命令 | 预期 |
|---|---|---|---|
| 1 | 服务运行 | systemctl status hbbs |
active |
| 2 | 端口监听 | ss -tulnp |
21115-21117 在听 |
| 3 | UDP 放行 | 外部设备连测 | 稳定不断 |
| 4 | Key 正确 | 客户端连测 | 握手成功 |
| 5 | 开机自启 | systemctl is-enabled hbbs |
enabled |
一条命令总验证:
systemctl is-active hbbs hbbr \
&& ss -tulnp | grep -cE '2111[5-7]' \
&& systemctl is-enabled hbbs
你的远程协助服务器已经就绪。任何设备填入这台服务器的地址和 Key,就能通过自己控制的中继建立安全远程连接。下一章给这台服务器搭一套 Prometheus 监控体系。