0%

外地 Windows 访问家庭 Ubuntu 虚拟机:FRP+WireGuard 完整部署教程(无公网 IP 也能用)

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 软件要求

软件 版本 下载链接
Ubuntu 20.04+ https://ubuntu.com
FRP 0.50+ https://github.com/fatedier/frp
WireGuard 最新 apt install wireguard
Windows 客户端 WireGuard App https://wireguard.com/install

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
# 1. 下载 FRP
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

# 2. 创建配置文件
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

# 3. 创建 systemd 服务
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

# 4. 启动服务
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
# 1. 安装 WireGuard
apt update
apt install -y wireguard qrencode

# 2. 生成密钥
cd /etc/wireguard
wg genkey | tee privatekey | wg pubkey > publickey

# 3. 创建配置文件
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

# 家庭 Ubuntu 客户端
[Peer]
PublicKey = <HOME_PUBLIC_KEY>
AllowedIPs = 10.0.0.2/32

# Windows 客户端
[Peer]
PublicKey = <WINDOWS_PUBLIC_KEY>
AllowedIPs = 10.0.0.3/32
EOF

# 4. 启动 WireGuard
wg-quick up wg0
systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0

# 5. 查看状态
wg show

3.3 配置防火墙

1
2
3
4
5
6
# Ubuntu UFW 防火墙
ufw allow 7000/tcp # FRP
ufw allow 7500/tcp # FRP Dashboard
ufw allow 51820/udp # WireGuard
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
# 1. 下载 FRP
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

# 2. 创建配置文件
cat > frpc.ini << 'EOF'
[common]
server_addr = <VPS_PUBLIC_IP>
server_port = 7000
token = your_secure_token_123456

# SSH 穿透(可选)
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

# WireGuard 穿透
[wireguard]
type = udp
local_ip = 127.0.0.1
local_port = 51820
remote_port = 51820

# OpenClaw Web UI(可选)
[openclaw]
type = http
local_ip = 127.0.0.1
local_port = 18789
custom_domains = openclaw.yourdomain.com
EOF

# 3. 创建 systemd 服务
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

# 4. 启动服务
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
# 1. 安装 WireGuard
apt update
apt install -y wireguard

# 2. 生成密钥
cd /etc/wireguard
wg genkey | tee privatekey | wg pubkey > publickey

# 3. 创建配置文件
cat > wg0.conf << 'EOF'
[Interface]
Address = 10.0.0.2/24
PrivateKey = <HOME_PRIVATE_KEY>
ListenPort = 51820

# VPS 服务器
[Peer]
PublicKey = <VPS_PUBLIC_KEY>
Endpoint = <VPS_PUBLIC_IP>:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
EOF

# 4. 启动 WireGuard
wg-quick up wg0
systemctl enable wg-quick@wg0
systemctl start wg-quick@wg0

# 5. 查看状态
wg show

4.3 测试连接

1
2
3
4
5
6
7
8
9
# 1. 测试 FRP 连接
curl -I http://<VPS_PUBLIC_IP>:7500

# 2. 测试 WireGuard 连接
ping 10.0.0.1 # ping VPS
ping 10.0.0.3 # ping Windows 客户端(配置后)

# 3. 测试 SSH 穿透(从 VPS)
ssh -p 6000 user@localhost

五、Windows 客户端配置

5.1 安装 WireGuard

1
2
3
4
5
# 1. 下载 WireGuard
# 访问:https://download.wireguard.com/windows-client/wireguard-installer.exe

# 2. 安装并运行
# 双击安装程序,按提示安装

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
# 在 VPS 上执行
cd /etc/wireguard
wg genkey | tee windows_privatekey | wg pubkey > windows_publickey

# 编辑 wg0.conf,添加 Windows 客户端
[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

# VPS 服务器
[Peer]
PublicKey = <VPS_PUBLIC_KEY>
Endpoint = <VPS_PUBLIC_IP>:51820
AllowedIPs = 10.0.0.0/24, 192.168.1.0/24
PersistentKeepalive = 25

# 家庭 Ubuntu(通过 VPS 路由)
[Peer]
PublicKey = <HOME_PUBLIC_KEY>
AllowedIPs = 192.168.1.0/24
EOF

# 生成二维码(方便手机扫描)
qrencode -t ansiutf8 < windows_client.conf

步骤 2:导入 Windows 客户端

  1. 打开 WireGuard 应用
  2. 点击”Import tunnel(s) from file”
  3. 选择 windows_client.conf
  4. 点击”Activate”激活连接

5.3 测试连接

1
2
3
4
5
6
7
8
9
10
11
12
# 1. 测试 VPS 连接
ping 10.0.0.1

# 2. 测试家庭 Ubuntu 连接
ping 10.0.0.2
ping 192.168.1.100 # 家庭服务器内网 IP

# 3. 测试 SSH 连接
ssh user@10.0.0.2

# 4. 测试 OpenClaw Web UI
# 浏览器访问:http://10.0.0.2:18789

六、OpenClaw 远程访问配置

6.1 配置 OpenClaw 监听

1
2
3
4
5
6
7
8
9
10
# 编辑 OpenClaw 配置
vim /home/johnzok/.openclaw/openclaw.json

# 修改 gateway.bind 为 0.0.0.0
"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
# frpc.ini
[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
# frps.ini - VPS 服务端
[common]
token = your_very_secure_token_here
dashboard_port = 7500
dashboard_user = admin
dashboard_pwd = VeryStrongPassword123!

# 限制 IP(可选)
allow_ports = 7000, 7500, 6000-6100
1
2
3
4
5
6
7
8
# frpc.ini - 家庭客户端
[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
# VPS 端 wg0.conf
[Interface]
Address = 10.0.0.1/24
PrivateKey = <VPS_PRIVATE_KEY>
ListenPort = 51820

# 限制允许的 IP
[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
# VPS 防火墙(只允许特定 IP)
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
# 家庭 Ubuntu SSH 配置
vim /etc/ssh/sshd_config

# 禁用密码登录
PasswordAuthentication no
# 禁用 root 登录
PermitRootLogin no
# 限制用户
AllowUsers your_username
# 更改端口(可选)
Port 2222

八、性能优化

8.1 WireGuard 性能调优

1
2
3
4
5
6
7
# 调整 MTU(减少分片)
# wg0.conf
[Interface]
MTU = 1420

# 启用持久连接
PersistentKeepalive = 25

8.2 FRP 性能调优

1
2
3
4
5
6
7
8
9
# frpc.ini
[common]
# 连接池大小
pool_count = 5
# TCP 多路复用
tcp_mux = true
# 心跳间隔
heartbeat_interval = 30
heartbeat_timeout = 90

8.3 网络优化

1
2
3
4
5
# 家庭 Ubuntu 网络优化
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
# 检查 FRP 状态
systemctl status frps # VPS
systemctl status frpc # 家庭

# 查看日志
tail -f /opt/frp/frps.log # VPS
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
# 检查 WireGuard 状态
wg show

# 查看路由
ip route

# 测试连接
ping 10.0.0.1 # ping VPS
ping 10.0.0.2 # ping 家庭

# 重启 WireGuard
wg-quick down wg0
wg-quick up wg0

9.3 常见问题

问题 1:FRP 连接失败

1
2
3
4
5
6
7
8
9
# 检查 VPS 防火墙
ufw status

# 检查端口监听
netstat -tlnp | grep 7000

# 检查 token 是否一致
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

# 检查 NAT 规则
iptables -t nat -L -n -v

问题 3:速度慢

1
2
3
4
5
6
7
8
# 测试带宽
iperf3 -c 10.0.0.1

# 检查 MTU
ping -s 1472 -M do 10.0.0.1

# 检查 VPS 负载
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 冗余

参考资源

  1. FRP 官方文档: https://github.com/fatedier/frp
  2. WireGuard 官方文档: https://www.wireguard.com
  3. WireGuard 配置生成器: https://www.wireguard.com/quickstart
  4. FRP 配置示例: https://github.com/fatedier/frp/tree/master/conf
  5. 本教程源码: https://github.com/mtnljbydd/100-AI-Mini-Projects-One-Year-Challenge

本文是远程访问系列的第 1 篇,下一篇将介绍如何使用 Ansible 自动化部署 FRP + WireGuard 环境。