FRP + WireGuard 远程访问家庭服务器完整指南(无公网 IP 方案)
写在前面:没有公网 IP 也能远程访问家庭服务器!本教程详解 FRP + WireGuard 组合方案,实现安全、高速的远程访问,适合家庭实验室、远程开发、私有云等场景。
一、方案概述
1.1 核心架构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| ┌─────────────────────────────────────────────────────────────┐ │ 互联网 │ ├─────────────────────────────────────────────────────────────┤ │ │ │ 外地 Windows 客户端 │ │ ┌──────────────┐ │ │ │ WireGuard │ │ │ │ 客户端 │ │ │ └──────┬───────┘ │ │ │ WireGuard VPN 隧道 │ │ ▼ │ │ ┌──────────────┐ FRP 隧道 ┌──────────────┐ │ │ │ VPS 服务器 │◄─────────────────►│ 家庭 Ubuntu │ │ │ │ (公网 IP) │ │ 虚拟机 │ │ │ │ frps │ │ frpc │ │ │ └──────────────┘ └──────────────┘ │ │ │ └─────────────────────────────────────────────────────────────┘
|
1.2 技术组件
| 组件 |
作用 |
说明 |
| FRP |
内网穿透 |
将家庭服务器暴露到公网 VPS |
| WireGuard |
VPN 组网 |
建立加密隧道,安全访问 |
| VPS 服务器 |
中转节点 |
需要公网 IP(云服务器) |
| 家庭 Ubuntu |
目标服务器 |
运行 OpenClaw 等应用 |
| Windows 客户端 |
访问端 |
远程连接家庭服务器 |
1.3 方案优势
| 优势 |
说明 |
| 无需公网 IP |
家庭宽带无公网 IP 也能用 |
| 安全性高 |
WireGuard 加密,FRP 隧道保护 |
| 性能好 |
WireGuard 性能优于 OpenVPN |
| 配置简单 |
相比 ZeroTier 更可控 |
| 成本低 |
只需一台廉价 VPS(约$5/月) |
二、环境准备
2.1 硬件要求
| 设备 |
配置要求 |
用途 |
| VPS 服务器 |
1 核/512MB/10GB |
中转节点(阿里云/腾讯云/AWS) |
| 家庭服务器 |
2 核/4GB/50GB |
Ubuntu 20.04+ |
| Windows 客户端 |
任意配置 |
Windows 10/11 |
2.2 软件要求
2.3 网络要求
| 节点 |
网络要求 |
| VPS 服务器 |
公网 IP,开放端口 7000, 51820 |
| 家庭服务器 |
能访问互联网 |
| Windows 客户端 |
能访问互联网 |
三、VPS 服务器配置
3.1 安装 FRP 服务端(frps)
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| cd /opt wget https://github.com/fatedier/frp/releases/download/v0.50.1/frp_0.50.1_linux_amd64.tar.gz tar -xzf frp_0.50.1_linux_amd64.tar.gz mv frp_0.50.1_linux_amd64 frp cd frp
cat > frps.ini << 'EOF' [common] bind_port = 7000 bind_addr = 0.0.0.0 token = your_secure_token_123456 dashboard_port = 7500 dashboard_user = admin dashboard_pwd = admin_password log_file = ./frps.log log_level = info log_max_days = 7 EOF
cat > /etc/systemd/system/frps.service << 'EOF' [Unit] Description=frp server After=network.target
[Service] Type=simple User=root Restart=on-failure RestartSec=5s ExecStart=/opt/frp/frps -c /opt/frp/frps.ini LimitNOFILE=1048576
[Install] WantedBy=multi-user.target EOF
systemctl daemon-reload systemctl enable frps systemctl start frps systemctl status frps
|
3.2 安装 WireGuard 服务端
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 26 27 28 29 30 31 32 33 34 35 36
| apt update apt install -y wireguard qrencode
cd /etc/wireguard wg genkey | tee privatekey | wg pubkey > publickey
cat > wg0.conf << 'EOF' [Interface] Address = 10.0.0.1/24 SaveConfig = true ListenPort = 51820 PrivateKey = <VPS_PRIVATE_KEY> PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer] PublicKey = <HOME_PUBLIC_KEY> AllowedIPs = 10.0.0.2/32
[Peer] PublicKey = <WINDOWS_PUBLIC_KEY> AllowedIPs = 10.0.0.3/32 EOF
wg-quick up wg0 systemctl enable wg-quick@wg0 systemctl start wg-quick@wg0
wg show
|
3.3 配置防火墙
1 2 3 4 5 6
| ufw allow 7000/tcp ufw allow 7500/tcp ufw allow 51820/udp ufw enable ufw status
|
四、家庭 Ubuntu 服务器配置
4.1 安装 FRP 客户端(frpc)
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| cd /opt wget https://github.com/fatedier/frp/releases/download/v0.50.1/frp_0.50.1_linux_amd64.tar.gz tar -xzf frp_0.50.1_linux_amd64.tar.gz mv frp_0.50.1_linux_amd64 frp cd frp
cat > frpc.ini << 'EOF' [common] server_addr = <VPS_PUBLIC_IP> server_port = 7000 token = your_secure_token_123456
[ssh] type = tcp local_ip = 127.0.0.1 local_port = 22 remote_port = 6000
[wireguard] type = udp local_ip = 127.0.0.1 local_port = 51820 remote_port = 51820
[openclaw] type = http local_ip = 127.0.0.1 local_port = 18789 custom_domains = openclaw.yourdomain.com EOF
cat > /etc/systemd/system/frpc.service << 'EOF' [Unit] Description=frp client After=network.target
[Service] Type=simple User=root Restart=on-failure RestartSec=5s ExecStart=/opt/frp/frpc -c /opt/frp/frpc.ini LimitNOFILE=1048576
[Install] WantedBy=multi-user.target EOF
systemctl daemon-reload systemctl enable frpc systemctl start frpc systemctl status frpc
|
4.2 安装 WireGuard 客户端
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 26 27 28 29 30
| apt update apt install -y wireguard
cd /etc/wireguard wg genkey | tee privatekey | wg pubkey > publickey
cat > wg0.conf << 'EOF' [Interface] Address = 10.0.0.2/24 PrivateKey = <HOME_PRIVATE_KEY> ListenPort = 51820
[Peer] PublicKey = <VPS_PUBLIC_KEY> Endpoint = <VPS_PUBLIC_IP>:51820 AllowedIPs = 0.0.0.0/0 PersistentKeepalive = 25 EOF
wg-quick up wg0 systemctl enable wg-quick@wg0 systemctl start wg-quick@wg0
wg show
|
4.3 测试连接
1 2 3 4 5 6 7 8 9
| curl -I http://<VPS_PUBLIC_IP>:7500
ping 10.0.0.1 ping 10.0.0.3
ssh -p 6000 user@localhost
|
五、Windows 客户端配置
5.1 安装 WireGuard
5.2 配置 WireGuard 客户端
步骤 1:在 VPS 上生成客户端配置
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 26 27 28 29 30 31
| cd /etc/wireguard wg genkey | tee windows_privatekey | wg pubkey > windows_publickey
[Peer] PublicKey = <WINDOWS_PUBLIC_KEY> AllowedIPs = 10.0.0.3/32
cat > windows_client.conf << 'EOF' [Interface] Address = 10.0.0.3/24 PrivateKey = <WINDOWS_PRIVATE_KEY> DNS = 8.8.8.8
[Peer] PublicKey = <VPS_PUBLIC_KEY> Endpoint = <VPS_PUBLIC_IP>:51820 AllowedIPs = 10.0.0.0/24, 192.168.1.0/24 PersistentKeepalive = 25
[Peer] PublicKey = <HOME_PUBLIC_KEY> AllowedIPs = 192.168.1.0/24 EOF
qrencode -t ansiutf8 < windows_client.conf
|
步骤 2:导入 Windows 客户端
- 打开 WireGuard 应用
- 点击”Import tunnel(s) from file”
- 选择
windows_client.conf
- 点击”Activate”激活连接
5.3 测试连接
1 2 3 4 5 6 7 8 9 10 11 12
| ping 10.0.0.1
ping 10.0.0.2 ping 192.168.1.100
ssh user@10.0.0.2
|
六、OpenClaw 远程访问配置
6.1 配置 OpenClaw 监听
1 2 3 4 5 6 7 8 9 10
| vim /home/johnzok/.openclaw/openclaw.json
"gateway": { "port": 18789, "mode": "local", "bind": "0.0.0.0", ... }
|
6.2 配置 FRP HTTP 穿透
1 2 3 4 5 6 7 8 9 10 11 12
| [openclaw-web] type = http local_ip = 127.0.0.1 local_port = 18789 custom_domains = openclaw.yourdomain.com
[openclaw-ssh] type = tcp local_ip = 127.0.0.1 local_port = 22 remote_port = 6000
|
6.3 远程访问方式
| 方式 |
连接字符串 |
说明 |
| WireGuard 直连 |
ssh user@10.0.0.2 |
推荐,最安全 |
| FRP SSH 穿透 |
ssh -p 6000 user@<VPS_IP> |
备用方案 |
| Web UI |
http://10.0.0.2:18789 |
WireGuard 连接后访问 |
| FRP Web |
http://openclaw.yourdomain.com |
需要域名配置 |
七、安全优化
7.1 FRP 安全配置
1 2 3 4 5 6 7 8 9
| [common] token = your_very_secure_token_here dashboard_port = 7500 dashboard_user = admin dashboard_pwd = VeryStrongPassword123!
allow_ports = 7000, 7500, 6000-6100
|
1 2 3 4 5 6 7 8
| [common] token = your_very_secure_token_here
protocol = wss
heartbeat_interval = 30 heartbeat_timeout = 90
|
7.2 WireGuard 安全配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [Interface] Address = 10.0.0.1/24 PrivateKey = <VPS_PRIVATE_KEY> ListenPort = 51820
[Peer] PublicKey = <HOME_PUBLIC_KEY> AllowedIPs = 10.0.0.2/32, 192.168.1.0/24
[Peer] PublicKey = <WINDOWS_PUBLIC_KEY> AllowedIPs = 10.0.0.3/32
|
7.3 防火墙规则
1 2 3 4
| ufw allow from <HOME_PUBLIC_IP> to any port 7000 ufw allow from <WINDOWS_PUBLIC_IP> to any port 7000 ufw allow 51820/udp
|
7.4 SSH 安全加固
1 2 3 4 5 6 7 8 9 10 11
| vim /etc/ssh/sshd_config
PasswordAuthentication no
PermitRootLogin no
AllowUsers your_username
Port 2222
|
八、性能优化
8.1 WireGuard 性能调优
1 2 3 4 5 6 7
|
[Interface] MTU = 1420
PersistentKeepalive = 25
|
8.2 FRP 性能调优
1 2 3 4 5 6 7 8 9
| [common]
pool_count = 5
tcp_mux = true
heartbeat_interval = 30 heartbeat_timeout = 90
|
8.3 网络优化
1 2 3 4 5
| sysctl -w net.core.rmem_max=16777216 sysctl -w net.core.wmem_max=16777216 sysctl -w net.ipv4.tcp_rmem="4096 87380 16777216" sysctl -w net.ipv4.tcp_wmem="4096 65536 16777216"
|
九、故障排查
9.1 FRP 连接问题
1 2 3 4 5 6 7 8 9 10
| systemctl status frps systemctl status frpc
tail -f /opt/frp/frps.log tail -f /opt/frp/frpc.log
telnet <VPS_IP> 7000
|
9.2 WireGuard 连接问题
1 2 3 4 5 6 7 8 9 10 11 12 13
| wg show
ip route
ping 10.0.0.1 ping 10.0.0.2
wg-quick down wg0 wg-quick up wg0
|
9.3 常见问题
问题 1:FRP 连接失败
1 2 3 4 5 6 7 8 9
| ufw status
netstat -tlnp | grep 7000
grep token /opt/frp/frps.ini grep token /opt/frp/frpc.ini
|
问题 2:WireGuard 无法连接
1 2 3 4 5 6 7 8
| chmod 600 /etc/wireguard/privatekey
wg showconf wg0
iptables -t nat -L -n -v
|
问题 3:速度慢
1 2 3 4 5 6 7 8
| iperf3 -c 10.0.0.1
ping -s 1472 -M do 10.0.0.1
htop
|
十、成本分析
10.1 VPS 成本
| 服务商 |
配置 |
价格 |
推荐度 |
| 腾讯云轻量 |
1 核/512MB/30GB |
¥24/月 |
⭐⭐⭐⭐⭐ |
| 阿里云 ECS |
1 核/512MB/40GB |
¥35/月 |
⭐⭐⭐⭐ |
| AWS Lightsail |
1 核/512MB/20GB |
$3.5/月 |
⭐⭐⭐⭐ |
| Vultr |
1 核/512MB/25GB |
$5/月 |
⭐⭐⭐⭐ |
10.2 总体成本
| 项目 |
费用 |
| VPS 服务器 |
¥24-35/月 |
| 域名(可选) |
¥50/年 |
| 家庭服务器 |
自有设备 |
| 总计 |
¥30-40/月 |
十一、总结
11.1 方案优势
- ✅ 无需公网 IP - 家庭宽带也能用
- ✅ 安全性高 - WireGuard 加密 + FRP 隧道
- ✅ 性能好 - WireGuard 性能优于 OpenVPN
- ✅ 配置灵活 - 可根据需求调整
- ✅ 成本低 - 每月仅需¥30-40
11.2 适用场景
- 🏠 家庭实验室 - HomeLab 远程访问
- 💻 远程开发 - 访问家庭开发环境
- 🖥️ 远程桌面 - RDP/SSH 远程连接
- 📱 私有云服务 - Nextcloud/Jellyfin 等
11.3 下一步优化
- 🔧 自动化部署 - Ansible/Terraform
- 🔧 监控告警 - Prometheus + Grafana
- 🔧 自动备份 - restic/rclone
- 🔧 负载均衡 - 多 VPS 冗余
参考资源
- FRP 官方文档: https://github.com/fatedier/frp
- WireGuard 官方文档: https://www.wireguard.com
- WireGuard 配置生成器: https://www.wireguard.com/quickstart
- FRP 配置示例: https://github.com/fatedier/frp/tree/master/conf
- 本教程源码: https://github.com/mtnljbydd/100-AI-Mini-Projects-One-Year-Challenge
本文是远程访问系列的第 1 篇,下一篇将介绍如何使用 Ansible 自动化部署 FRP + WireGuard 环境。