零公网IP实现远程办公:RustDesk Server内网穿透完整方案
你是否正面临这些痛点?
- 公司防火墙限制,无法直接访问内网设备
- 没有公网IP,无法搭建传统远程控制服务器
- 第三方远程工具延迟高、安全性差
- 团队需要安全稳定的跨地域设备管理
读完本文你将获得:
- 3种内网穿透方案的部署指南与对比
- RustDesk Server无公网IP环境的完整配置
- 从0到1的加密密钥生成与客户端配置
- 企业级性能优化与常见问题解决方案
方案架构与工作原理
网络拓扑结构
核心组件说明
| 组件 | 作用 | 关键端口 | 数据流向 |
|---|---|---|---|
| hbbs | ID/会和服务器 | TCP:21115-21116 UDP:21116 | 设备注册、IP交换 |
| hbbr | 中继服务器 | TCP:21117,21119 | 数据中转、流量控制 |
| 内网穿透工具 | 端口转发 | TCP:自定义(如7000-7001) | 公网流量转发至内网 |
环境准备与依赖检查
硬件要求
| 场景 | CPU | 内存 | 存储 | 网络 |
|---|---|---|---|---|
| 个人使用 | 双核 | 2GB | 10GB SSD | 10Mbps上行 |
| 团队(10人内) | 四核 | 4GB | 20GB SSD | 50Mbps上行 |
| 企业(50人内) | 八核 | 8GB | 50GB SSD | 100Mbps上行 |
软件依赖
# 检查系统版本 (推荐Ubuntu 20.04+/Debian 11+)
lsb_release -a
# 安装必要工具
sudo apt update && sudo apt install -y curl wget docker.io docker-compose
内网穿透方案部署
方案1:FRP穿透 (推荐)
云服务器配置 (公网IP)
# 下载FRP (国内用户推荐使用镜像站)
wget https://gitee.com/fatedier/frp/releases/download/v0.51.3/frp_0.51.3_linux_amd64.tar.gz
tar xzf frp_0.51.3_linux_amd64.tar.gz
cd frp_0.51.3_linux_amd64
# 配置服务端 (frps.ini)
cat > frps.ini << EOF
[common]
bind_port = 7000 # 控制端口
token = YourSecureToken123 # 身份验证令牌
dashboard_port = 7500 # 控制台端口
dashboard_user = admin
dashboard_pwd = admin123
EOF
# 启动服务端
nohup ./frps -c frps.ini > frps.log 2>&1 &
# 设置开机自启
sudo tee /etc/systemd/system/frps.service << EOF
[Unit]
Description=FRP Server Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/frp_0.51.3_linux_amd64
ExecStart=/root/frp_0.51.3_linux_amd64/frps -c /root/frp_0.51.3_linux_amd64/frps.ini
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now frps
内网服务器配置
# 下载FRP客户端
wget https://gitee.com/fatedier/frp/releases/download/v0.51.3/frp_0.51.3_linux_amd64.tar.gz
tar xzf frp_0.51.3_linux_amd64.tar.gz
cd frp_0.51.3_linux_amd64
# 配置客户端 (frpc.ini)
cat > frpc.ini << EOF
[common]
server_addr = 47.xxx.xxx.xxx # 云服务器公网IP
server_port = 7000 # 控制端口
token = YourSecureToken123 # 与服务端一致
[rustdesk-hbbs-tcp]
type = tcp
local_ip = 127.0.0.1
local_port = 21115
remote_port = 21115 # 公网映射端口
[rustdesk-hbbs-tcp2]
type = tcp
local_ip = 127.0.0.1
local_port = 21116
remote_port = 21116
[rustdesk-hbbs-udp]
type = udp
local_ip = 127.0.0.1
local_port = 21116
remote_port = 21116
[rustdesk-hbbr-tcp1]
type = tcp
local_ip = 127.0.0.1
local_port = 21117
remote_port = 21117
[rustdesk-hbbr-tcp2]
type = tcp
local_ip = 127.0.0.1
local_port = 21119
remote_port = 21119
EOF
# 启动客户端
nohup ./frpc -c frpc.ini > frpc.log 2>&1 &
# 设置开机自启
sudo tee /etc/systemd/system/frpc.service << EOF
[Unit]
Description=FRP Client Service
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/root/frp_0.51.3_linux_amd64
ExecStart=/root/frp_0.51.3_linux_amd64/frpc -c /root/frp_0.51.3_linux_amd64/frpc.ini
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable --now frpc
方案2:NPS穿透 (企业级)
# 服务端安装 (公网服务器)
curl -fsSL https://mirrors.aliyun.com/nps/install.sh | sudo bash
# 客户端安装 (内网服务器)
./npc install -server=47.xxx.xxx.xxx:8080 -vkey=your_vkey -type=tcp
方案3:路由器端口映射 (有管理权限场景)
登录路由器管理界面配置端口转发规则:
| 应用名称 | 内部IP | 内部端口 | 外部端口 | 协议 |
|---|---|---|---|---|
| RustDesk-hbbs1 | 192.168.1.100 | 21115 | 21115 | TCP |
| RustDesk-hbbs2 | 192.168.1.100 | 21116 | 21116 | TCP+UDP |
| RustDesk-hbbr1 | 192.168.1.100 | 21117 | 21117 | TCP |
| RustDesk-hbbr2 | 192.168.1.100 | 21119 | 21119 | TCP |
RustDesk Server部署
Docker Compose部署 (推荐)
# 克隆代码仓库
git clone https://gitcode.com/gh_mirrors/ru/rustdesk-server.git
cd rustdesk-server
# 创建数据目录
mkdir -p ./data
# 编辑docker-compose.yml
cat > docker-compose.yml << EOF
version: '3'
networks:
rustdesk-net:
external: false
services:
hbbs:
container_name: hbbs
ports:
- 21115:21115
- 21116:21116
- 21116:21116/udp
- 21118:21118
image: rustdesk/rustdesk-server:latest
command: hbbs -r 47.xxx.xxx.xxx:21117 # 替换为公网IP:端口
volumes:
- ./data:/root
networks:
- rustdesk-net
depends_on:
- hbbr
restart: unless-stopped
hbbr:
container_name: hbbr
ports:
- 21117:21117
- 21119:21119
image: rustdesk/rustdesk-server:latest
command: hbbr
volumes:
- ./data:/root
networks:
- rustdesk-net
restart: unless-stopped
EOF
# 启动服务
sudo docker-compose up -d
# 查看运行状态
sudo docker-compose ps
手动编译部署
# 安装Rust环境
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
# 编译源码
git clone https://gitcode.com/gh_mirrors/ru/rustdesk-server.git
cd rustdesk-server
cargo build --release
# 生成密钥对
./target/release/rustdesk-utils genkeypair
# 启动服务
nohup ./target/release/hbbs -r 47.xxx.xxx.xxx:21117 > hbbs.log 2>&1 &
nohup ./target/release/hbbr > hbbr.log 2>&1 &
密钥生成与安全配置
生成加密密钥对
# 使用Docker生成
docker run --rm --entrypoint /usr/bin/rustdesk-utils rustdesk/rustdesk-server-s6:latest genkeypair
# 输出示例
Public Key: 8BLLhtzUBU/XKAH4mep3p+IX4DSApe7qbAwNH9nv4yA=
Secret Key: egAVd44u33ZEUIDTtksGcHeVeAwywarEdHmf99KM5ajwEsuG3NQFT9coAfiZ6nen4hfgNICl7upsDA0f2e/jIA==
强制加密连接
# 修改docker-compose.yml添加环境变量
environment:
- "ENCRYPTED_ONLY=1"
- "KEY_PRIV=你的私钥"
- "KEY_PUB=你的公钥"
客户端配置与使用
Windows客户端设置
- 下载并安装RustDesk客户端
- 点击ID右侧的菜单按钮,选择"网络"设置
- 配置自定义服务器:
- ID服务器:
47.xxx.xxx.xxx:21116 - 中继服务器:
47.xxx.xxx.xxx:21117 - 密钥:
8BLLhtzUBU/XKAH4mep3p+IX4DSApe7qbAwNH9nv4yA=
- ID服务器:
组策略批量配置 (企业版)
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\RustDesk]
"rendezvous_server"="47.xxx.xxx.xxx:21116"
"relay_server"="47.xxx.xxx.xxx:21117"
"key"="8BLLhtzUBU/XKAH4mep3p+IX4DSApe7qbAwNH9nv4yA="
性能优化与监控
带宽控制配置
# 单个连接带宽限制 (10Mbps)
docker exec hbbr sh -c "export SINGLE_BANDWIDTH=10 && kill -SIGHUP \$(pidof hbbr)"
# 总带宽限制 (100Mbps)
docker exec hbbr sh -c "export TOTAL_BANDWIDTH=100 && kill -SIGHUP \$(pidof hbbr)"
服务状态监控
# 查看连接数
netstat -tulnp | grep -E 'hbbs|hbbr' | wc -l
# 查看资源占用
docker stats
# 日志查看
docker logs -f hbbs --tail=100
Prometheus监控集成
# prometheus.yml配置
scrape_configs:
- job_name: 'rustdesk'
static_configs:
- targets: ['192.168.1.100:21118'] # hbbs metrics端口
常见问题解决方案
连接失败排查流程
典型问题解决
- NAT类型限制导致P2P失败
# 强制使用中继模式
docker exec hbbs sh -c "export ALWAYS_USE_RELAY=Y && kill -SIGHUP \$(pidof hbbs)"
- 密钥不匹配
# 确保客户端与服务端公钥一致
cat ./data/id_ed25519.pub # 服务端公钥
- 带宽占用过高
# 设置速度限制
docker update --env LIMIT_SPEED=5 hbbr # 限制为5Mbps
部署验证与压力测试
功能验证
# 检查服务端口
netstat -tulnp | grep -E '21115|21116|21117|21119'
# 验证密钥有效性
curl http://localhost:21118/health
压力测试脚本
# 安装测试工具
sudo apt install -y iperf3
# 测试中继服务器带宽
iperf3 -c 47.xxx.xxx.xxx -p 21117 -t 60 -P 4
企业级部署最佳实践
多服务器负载均衡
数据备份策略
# 创建定时备份脚本
cat > backup_rustdesk.sh << EOF
#!/bin/bash
BACKUP_DIR=/backup/rustdesk
TIMESTAMP=\$(date +%Y%m%d_%H%M%S)
mkdir -p \$BACKUP_DIR
cp -r /path/to/rustdesk-server/data \$BACKUP_DIR/data_\$TIMESTAMP
find \$BACKUP_DIR -name "data_*" -mtime +7 -delete
EOF
# 添加到crontab
echo "0 1 * * * /bin/bash /path/to/backup_rustdesk.sh" | crontab -
总结与展望
通过本文方案,您已成功实现:
✅ 无公网IP环境下的RustDesk Server部署
✅ 企业级加密远程控制通道
✅ 带宽与连接数的精细化管理
下一步建议:
- 部署RustDesk Server Pro版本获取更多企业功能
- 集成Zabbix/Prometheus实现监控告警
- 配置双机热备提高服务可用性
提示:定期关注官方仓库更新,及时修复安全漏洞。如遇技术问题,可在RustDesk GitHub Issues或社区论坛寻求帮助。
如果觉得本文有帮助,请点赞收藏并分享给需要的同事!
下期预告:《RustDesk Server与企业SSO集成方案》
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



