Files
note_sync/自建服务/frp内网穿透部署指南.md
T

219 lines
6.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# frp 内网穿透部署指南
> 2026-08-13 更新。目标:通过云服务器(公网 IP `58.87.87.208`)随时随地访问家里多台机器的 SSH 和 RDP,统一用域名 `hyonglin.top` + 端口区分。
---
## 背景与方案
**需求**:家里机器在 NAT 内网、没有公网 IP,想在任意地点 SSH 进去、远程桌面操作。
**选型结论**:frp(Fast Reverse Proxy),成熟的内网穿透工具。
- 家里机器(内网)主动反向连到云服务器,云服务器监听公网端口转发进来
- 一个服务端(frps)对多个客户端(frpc),加机器只需加端口,服务端不用动
- Go 编写,单文件二进制,Linux/Windows 通用,systemd 管理开机自启
### 架构
```
你(任意地点) 云服务器 frps 家里机器 frpc(可多台)
│ │ │
├─SSH hyonglin.top:2201──▶│─监听 2201───────────▶│─▶ pc01:22
├─SSH hyonglin.top:2202──▶│─监听 2202───────────▶│─▶ pc02:22
├─RDP hyonglin.top:3389──▶│─监听 3389───────────▶│─▶ pc01:3389
├─RDP hyonglin.top:3390──▶│─监听 3390───────────▶│─▶ pc02:3389
│ └─frpc 主动连 bindPort 7000◀─┘
```
> 域名只解析到 IP,**端口才是区分机器的关键**。RDP 不能共用 3389(TCP 层没有域名概念),但 frp 的 `remotePort` 独立于本机 `localPort`,本机 RDP 端口保持 3389 不用改。
---
## 端口规划
| 机器 | SSH(remotePort) | RDP(remotePort) | 本机端口 |
|---|---|---|---|
| pc01 | 2201 | 3389 | 22 / 3389 |
| pc02 | 2202 | 3390 | 22 / 3389 |
| 新增机器 | 2203 | 3391 | … |
固定端口:frps 客户端连接 `7000`、管理面板 `7500`。
---
## 一、服务端 frps 部署(云服务器)
### 下载与安装
```bash
# 下载(GitHub 慢时走镜像 ghfast.top)
wget https://ghfast.top/https://github.com/fatedier/frp/releases/download/v0.65.0/frp_0.65.0_linux_amd64.tar.gz
tar xzf frp_0.65.0_linux_amd64.tar.gz
sudo cp frp_0.65.0_linux_amd64/frps /usr/local/bin/
sudo mkdir -p /etc/frp
```
### 服务端配置 `/etc/frp/frps.toml`
```toml
bindPort = 7000 # frpc 连接端口
auth.token = "0ae2664b411c41645e5c9ab6779724d4" # 客户端认证令牌(frpc 必须一致)
# 管理面板
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "admin"
webServer.password = "Admin-0ae2664b"
# 日志
log.to = "/var/log/frps.log"
log.level = "info"
log.maxDays = 7
```
### systemd 服务 `/etc/systemd/system/frps.service`
```ini
[Unit]
Description=frp server
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/frps -c /etc/frp/frps.toml
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
```
```bash
sudo systemctl daemon-reload
sudo systemctl enable --now frps
systemctl status frps # active (running)
```
---
## 二、客户端 frpc 部署(家里机器)
### 客户端配置 `/etc/frp/frpc.toml`
以 pc01 为例:
```toml
serverAddr = "hyonglin.top"
serverPort = 7000
auth.token = "0ae2664b411c41645e5c9ab6779724d4"
# SSH:服务器 2201 → 本机 22
[[proxies]]
name = "pc01-ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 2201
# RDP:服务器 3389 → 本机 3389
[[proxies]]
name = "pc01-rdp"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3389
remotePort = 3389
```
> 加第二台(pc02):把 `name` 换成 `pc02-*`、`remotePort` 换成 `2202`/`3390`,`serverAddr`/`token` 不变即可。
### 安装(frpc 二进制 + systemd)
```bash
sudo install -m 755 ~/frpc /usr/local/bin/frpc
sudo mkdir -p /etc/frp
sudo cp ~/frpc.toml /etc/frp/frpc.toml
sudo cp ~/frpc.service /etc/systemd/system/frpc.service
sudo systemctl daemon-reload
sudo systemctl enable --now frpc
systemctl status frpc # active (running)
```
---
## 三、家里开启 SSH 和 RDP 服务
### SSH
```bash
sudo apt update && sudo apt install -y openssh-server
sudo systemctl enable --now ssh
```
### RDP(Ubuntu 内置,无需装 xrdp)
- 打开 **设置 → 系统 → 远程桌面**
- 开启 **远程登录**(Remote Desktop / RDP)开关,记下用户名密码
- 端口默认 3389
> ⚠️ Ubuntu 内置 RDP 依赖**已登录的图形会话**。重启后停在登录界面、无人登录时 RDP 连不上。若要无人值守,需换 xrdp 方案。
---
## 四、云安全组放行端口(关键坑)
**最容易漏的一步**:云服务商控制台的「防火墙/安全组」默认只开 22/80/443,需手动放行 TCP:
```
7000(frps 连接)、2201/2202(SSH 转发)、3389/3390(RDP 转发)
```
> 服务器内部 `ufw` 是 inactive,iptables 只黑名单了若干恶意 IP,不影响端口;问题出在**云控制台安全组**,与服务器内防火墙无关。
> 7500(frps 管理面板)走 `frp.hyonglin.top` HTTPS 后,建议在安全组**撤销 7500 的公网放行**,避免明文暴露。
---
## 五、验证与使用
```bash
# 服务器端确认 frpc 已登录、代理已监听
ss -tlnp | grep -E '2201|2202|3389|3390'
sudo tail -f /var/log/frps.log # 应看到 client login / new proxy success
```
连接方式(统一 hyonglin.top + 端口):
| 目标 | 命令 |
|---|---|
| pc01 SSH | `ssh 用户名@hyonglin.top -p 2201` |
| pc01 RDP | 远程桌面连 `hyonglin.top:3389` |
| pc02 SSH | `ssh 用户名@hyonglin.top -p 2202` |
| pc02 RDP | 远程桌面连 `hyonglin.top:3390` |
管理面板:`https://frp.hyonglin.top`(admin / Admin-0ae2664b)
---
## 排错记录
| 症状 | 原因 | 解决 |
|---|---|---|
| frpc 状态 active 但 frps 日志无 login 记录 | 云安全组没放行 7000 | 控制台放行 7000 及转发端口 |
| `login to server failed` | token 不一致 | 检查 frpc/frps 的 auth.token 是否相同 |
| 2201 没监听 | frpc 没启动或代理注册失败 | `journalctl -u frpc -n 50` 看报错 |
---
## 相关链接
- [[主页与Caddy反向代理]] — 主页/面板的 HTTPS 入口
- [[sshwifty网页SSH终端]] — 浏览器里连 SSH
- [[Silverbullet部署指南]] — 本服务器自建服务
- [frp官方文档](https://gofrp.org/zh-cn/)
---
*最后更新:2026-08-13*