Featured image of post 小白也能看懂的 frp 部署教程:VPS 服务端与内网客户端

小白也能看懂的 frp 部署教程:VPS 服务端与内网客户端

家里有电脑、NAS 或小主机,想在外面通过公网访问,但家里没有公网 IP,可以用 frp 做内网穿透。

这篇只讲最简单的用法:

  • 一台有公网 IP 的 VPS 运行 frps
  • 家里的设备运行 frpc
  • 使用 IP 加端口直接访问;
  • 使用 token 密码鉴权;
  • frpc 和 frps 之间启用 TLS;
  • 不配置域名,不配置 HTTP,也不配置面板反向代理。

示例使用 frp v0.69.0,把家里设备的 SSH 端口 22 映射到 VPS 的 6000 端口。

frp 的原理其实很简单

frp 有两个程序:

  • frps:服务端,放在有公网 IP 的 VPS 上;
  • frpc:客户端,放在家里的电脑、NAS 或小主机上。

家里的设备会主动连接 VPS。以后访问 VPS公网IP:6000,VPS 就会把连接通过 frp 转发到家里设备的 22 端口。

1
2
3
4
5
6
7
8
9
外面的电脑
VPS公网IP:6000
VPS上的 frps
家里的 frpc
家里设备的 127.0.0.1:22

只要记住一句话:frps 放公网服务器,frpc 放内网设备。

没有 VPS 的先准备 VPS

如果你已经有 VPS,可以直接跳到“部署 frps 服务端”。

购买 VPS 时选择:

  • 系统:Ubuntu 22.04 或 Ubuntu 24.04;
  • 配置:1 核 1GB 就能运行 frps,想安装 1Panel 建议 2GB 内存;
  • 网络:必须有公网 IPv4;
  • 地区:选择离自己较近的机房;
  • 带宽:只远程管理要求不高,传文件则需要更高带宽和更多流量。

买好后使用厂商给出的公网 IP 和 root 密码登录:

1
ssh root@你的VPS公网IP

先更新系统并安装下载工具:

1
2
3
apt update
apt upgrade -y
apt install -y curl wget tar

这篇为了方便小白,后面的命令都直接使用 root 执行。

1Panel 是可选的

1Panel 只是一个服务器管理面板,不是 frp 的必需品。不想装可以直接跳过,不影响后面的 frp 部署。

想装的话,执行 1Panel V2 官方安装命令:

1
bash -c "$(curl -sSL https://resource.fit2cloud.com/1panel/package/v2/quick_start.sh)"

按照提示设置面板端口、入口、用户名和密码。安装完成后,终端会显示访问地址。

记得在 VPS 厂商的安全组中开放你设置的 1Panel 端口,否则浏览器打不开面板。

后面的 frp 不依赖 1Panel,继续用命令行安装最简单。

部署 frps 服务端

下面的操作全部在 VPS 上执行。

下载 frp

进入 /opt

1
cd /opt

国内访问 GitHub 较慢时,可以在原始下载地址前面加 GitHub 文件加速代理:

1
wget "https://gh-proxy.com/https://github.com/fatedier/frp/releases/download/v0.69.0/frp_0.69.0_linux_amd64.tar.gz" -O frp.tar.gz

如果这个加速地址失效,就使用官方地址:

1
wget "https://github.com/fatedier/frp/releases/download/v0.69.0/frp_0.69.0_linux_amd64.tar.gz" -O frp.tar.gz

第三方加速站只负责转发下载,软件的正式来源仍然是 frp 官方 GitHub Release。

解压并改成简单目录名:

1
2
3
tar -zxvf frp.tar.gz
mv frp_0.69.0_linux_amd64 frp
cd /opt/frp

如果你的 VPS 是 ARM64,请把下载文件名中的 linux_amd64 改为 linux_arm64

可以用下面的命令查看架构:

1
uname -m
  • 显示 x86_64:使用 linux_amd64
  • 显示 aarch64:使用 linux_arm64

写最简单的 frps.toml

编辑服务端配置:

1
nano /opt/frp/frps.toml

把内容改成:

1
2
3
4
5
6
bindPort = 7000

transport.tls.force = true

auth.method = "token"
auth.token = "请改成你自己的复杂密码"

这里只有三个重点:

  • 7000 是 frpc 连接 frps 的端口;
  • transport.tls.force = true 表示只接受 TLS 连接;
  • auth.token 是客户端和服务端共同使用的连接密码。

auth.token 两边必须完全相同。不要真的使用示例文字,建议换成一串较长的随机字母和数字。

启动 frps

先直接运行测试:

1
2
cd /opt/frp
./frps -c frps.toml

看到 frps 启动成功后,按 Ctrl+C 停止测试。

需要放到后台运行时:

1
2
cd /opt/frp
nohup ./frps -c frps.toml > frps.log 2>&1 &

查看日志:

1
tail -f /opt/frp/frps.log

Ctrl+C 退出日志查看,不会停止 frps。

可选:设置 frps 开机自启

只想先测试可以跳过。需要 VPS 重启后自动启动,再创建 systemd 服务:

1
nano /etc/systemd/system/frps.service

写入:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[Unit]
Description=frp server
After=network.target

[Service]
Type=simple
WorkingDirectory=/opt/frp
ExecStart=/opt/frp/frps -c /opt/frp/frps.toml
Restart=always

[Install]
WantedBy=multi-user.target

如果前面已经用 nohup 启动,先结束旧进程:

1
pkill frps

然后启用服务:

1
2
3
systemctl daemon-reload
systemctl enable --now frps
systemctl status frps

VPS 必须开放哪些端口

本例只需要开放两个 frp 端口:

端口 协议 用途
7000 TCP frpc 连接 frps
6000 TCP 外网访问家里 SSH 的入口

先在 VPS 厂商控制台的“安全组”或“防火墙”中开放:

1
2
TCP 7000
TCP 6000

如果 VPS 系统启用了 UFW,还要在系统内开放:

1
2
3
ufw allow 7000/tcp
ufw allow 6000/tcp
ufw status

如果没有启用 UFW,不需要为了 frp 专门开启它。云厂商安全组已经开放,但还是连不上时,再检查系统防火墙。

如果安装了 1Panel,还要额外开放安装时设置的面板端口。本文不需要开放 80443,因为没有配置网站、域名和 HTTP 转发。

部署 frpc 客户端

下面的操作在家里的 Linux 电脑、NAS 或小主机上执行,不是在 VPS 上执行。

下载 frpc

1
2
3
4
5
cd /opt
wget "https://gh-proxy.com/https://github.com/fatedier/frp/releases/download/v0.69.0/frp_0.69.0_linux_amd64.tar.gz" -O frp.tar.gz
tar -zxvf frp.tar.gz
mv frp_0.69.0_linux_amd64 frp
cd /opt/frp

如果加速地址不能用,把下载地址换成官方 GitHub 地址即可。ARM64 设备同样需要把 linux_amd64 改成 linux_arm64

写最简单的 frpc.toml

编辑客户端配置:

1
nano /opt/frp/frpc.toml

写入:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
serverAddr = "你的VPS公网IP"
serverPort = 7000

transport.tls.enable = true

auth.method = "token"
auth.token = "和frps.toml完全相同的密码"

[[proxies]]
name = "home-ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000

需要修改的只有两处:

  1. serverAddr 改成 VPS 的公网 IP;
  2. auth.token 改成和服务端完全相同的密码。

端口的意思是:

  • localPort = 22:家里设备的 SSH 端口;
  • remotePort = 6000:在 VPS 上开放给外部访问的端口。

启动 frpc

先直接测试:

1
2
cd /opt/frp
./frpc -c frpc.toml

看到登录服务端成功、代理启动成功,就说明配置正确。

需要后台运行:

1
2
cd /opt/frp
nohup ./frpc -c frpc.toml > frpc.log 2>&1 &

查看日志:

1
tail -f /opt/frp/frpc.log

可选:设置 frpc 开机自启

创建服务:

1
nano /etc/systemd/system/frpc.service

写入:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[Unit]
Description=frp client
After=network.target

[Service]
Type=simple
WorkingDirectory=/opt/frp
ExecStart=/opt/frp/frpc -c /opt/frp/frpc.toml
Restart=always

[Install]
WantedBy=multi-user.target

如果前面已经用 nohup 启动,先结束旧进程:

1
pkill frpc

然后启动服务:

1
2
3
systemctl daemon-reload
systemctl enable --now frpc
systemctl status frpc

在外面通过 IP 直接访问

现在换一个网络测试,例如让电脑连接手机热点,不要只在家里局域网测试。

执行:

1
ssh -p 6000 家里设备的用户名@你的VPS公网IP

例如:

1
ssh -p 6000 root@203.0.113.10

这里的 IP 是 VPS 公网 IP,但登录用户名和密码属于家里的设备,因为 VPS 只是帮忙转发连接。

能正常登录就说明部署完成。

如果要转发其他 TCP 服务

假设家里有一个服务运行在 192.168.1.20:8080,想通过 VPS 的 6001 端口访问,只需要在 frpc.toml 末尾增加:

1
2
3
4
5
6
[[proxies]]
name = "my-service"
type = "tcp"
localIP = "192.168.1.20"
localPort = 8080
remotePort = 6001

然后重启 frpc:

1
systemctl restart frpc

同时在 VPS 安全组中开放 TCP 6001。访问时直接使用:

1
VPS公网IP:6001

不使用这个映射时,就不要开放 6001。

最常见的几个问题

frpc 连不上 frps

先检查 VPS 的 7000 端口是否开放,再检查 IP 是否写对:

1
2
systemctl status frps
systemctl status frpc

如果使用 nohup,查看日志:

1
2
tail -n 50 /opt/frp/frps.log
tail -n 50 /opt/frp/frpc.log

提示 token 错误

检查 frps.tomlfrpc.toml 里的 auth.token,必须一字不差。

frpc 显示成功,但外面访问不了

检查三件事:

  1. VPS 安全组是否开放 6000;
  2. VPS 系统防火墙是否开放 6000;
  3. 家里设备的 SSH 是否真的运行在 22 端口。

家里设备可以执行:

1
ss -lntp | grep ':22'

VPS 重启后 frp 不见了

nohup 只能保证退出终端后继续运行,不能保证重启后自动启动。需要重启自动恢复,就按照前面的可选步骤配置 systemd。

最后再记住这四件事

  1. frps 放 VPS,frpc 放家里;
  2. 两个配置文件里的 token 必须相同;
  3. VPS 必须开放 7000 和实际映射端口 6000;
  4. 外面访问的是 VPS公网IP:6000,不需要域名,也不需要配置 HTTP。

参考资料:

  • frp 官方文档:https://gofrp.org/
  • frp 官方下载:https://github.com/fatedier/frp/releases
  • 1Panel V2 官方安装文档:https://1panel.cn/docs/v2/installation/online_installation/
使用 Hugo 构建
主题 StackJimmy 设计