什么是frp

frp 采用 C/S 模式,将

  1. 服务端部署在具有公网 IP 的机器上,

  2. 客户端部署在内网或防火墙内的机器上,通过访问暴露在服务器上的端口,反向代理到处于内网的服务。

在此基础上,frp 支持 TCP, UDP, HTTP, HTTPS 等多种协议,提供了加密、压缩,身份认证,代理限速,负载均衡等众多能力。此外,还可以通过 xtcp 实现 P2P 通信。

工具准备

  1. 一台公网服务器,部署服务端
  2. 一台内网机器,部署客户端
  3. frp软件:frps服务端+ frpc客户端

安装

下载frp分别上传到服务器和内网机器,根据服务器和内网机器分别下载对应架构的frp版本

  • amd相当于x86,是64位指令的操作系统架构
  • arm64是64位精简指令的操作系统架构,特点低能耗,被广泛应用小设备和移动设备

服务端

服务器解压frp

1
2
3
4
tar -zxvf frp_0.63.0_linux_amd64.tar.gz
mv frp_0.63.0_linux_amd64 /usr/local/frp
rm -f /usr/local/frp/frpc # 服务端不需要客户端了
rm -f /usr/local/frp/frpc.toml # 服务端配置文件不需要客户端了

配置文件

  1. 设置配置文件
1
2
mkdir /etc/frp
ln -s /usr/local/frp/frps.toml /etc/frp/frps.toml
  1. 配置frps.toml文件
1
vim /etc/frp/frps.toml
1
2
3
4
5
6
7
8
9
10
11
# 监听端口,记得服务器防火墙要打开
bindPort = 7000

webServer.addr = "0.0.0.0"
# 网页仪表盘端口,记得防火墙打开
webServer.port = 7500
webServer.user = "admin"
webServer.password = "密码"

auth.method="token"
auth.token = "thisisatoken"
  1. 利用systemd启动后台进程挂载

    创建service文件

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

    frps.service内容:

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

    [Service]
    Type=simple
    ExecStart = /usr/local/frp/frps -c /etc/frp/frps.toml

    ExecStop=/bin/kill $MAINPID
    Restart=always
    RestartSec=5

    [Install]
    WantedBy = multi-user.target

服务端启动

1
2
3
4
5
6
7
8
9
10
11
#重载配置
sudo systemctl daemon-reload

# 启动服务
systemctl start frps.service

# 查看服务状态
systemctl status frps.service

# 停止服务
systemctl stop frps.service

查看日志

1
2
3
4
5
6
7
8
# 查看 frps 服务的最新日志
journalctl -u frps

# 实时跟踪日志(类似 tail -f)
journalctl -u frps -f

# 查看最近 N 行日志
journalctl -u frps -n 100

image-20250725145119696

设置开机自启

1
sudo systemctl enable frps

验证开机自启是否生效

1
systemctl status frps

image-20250725145855822

1
sudo systemctl is-enabled frps  # 输出 enabled 则表示成功

客户端

内网机器解压

1
2
3
4
5
tar -zxvf frp_0.63.0_linux_arm64.tar.gz
mv frp_0.63.0_linux_arm64 /usr/local/frp

rm -f /usr/local/frp/frps # 不需要服务端了
rm -f /usr/local/frp/frps.toml # 不需要服务端了

配置文件

  1. 设置配置文件
1
2
mkdir /etc/frp
ln -s /usr/local/frp/frpc.toml /etc/frp/frpc.toml
  1. 配置frpc.toml文件
1
vim /etc/frp/frpc.toml
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
# /ect/frp/frpc.tpml

#你的云服务器公网ip
serverAddr = "公网ip"
#连接到云服务器端口,需要和frps.toml配置文件中端口一致
serverPort = 7000

auth.method = "token"
auth.token = "thisisatoken"

#内网服务器的终端连接
[[proxies]]
name = "frp-ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000


#内网服务器的nextcloud连接
[[proxies]]
name = "frp-nextcloud"
type = "tcp"
localIP = "127.0.0.1"
localPort = 40070
remotePort = 8080

remotePort端口记得服务器防火墙打开

  1. 利用systemd启动后台进程挂载
    创建service文件

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

    frps.service内容:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [Unit]
    Description=FRP Client Service
    After=network.target

    [Service]
    Type=simple
    User=frp
    Group=frp
    ExecStart=/usr/local/frp/frpc -c /etc/frp/frpc.toml
    Restart=always
    RestartSec=10

    [Install]
    WantedBy=multi-user.target

    可能发生的错误

    1. 用户 / 组是否存在
      配置中指定了 User=frpGroup=frp,需确认系统中是否真的存在 frp 用户和同名组。如果不存在,服务会因无法切换到该用户而启动失败。
      检查命令:id frp(若返回无此用户,则需创建:sudo useradd -r -s /sbin/nologin frp)。
    2. 文件权限是否匹配
      frp 用户需要对以下文件有对应权限:
      • /usr/local/frp/frpc:需有可执行权限(chmod +x)。
      • /etc/frp/frpc.toml:需有读取权限(可设置为 chmod 644 并确保所有者或组包含 frp)。
    3. 路径是否正确
      确认 ExecStartfrpc 程序路径(/usr/local/frp/frpc)和配置文件路径(/etc/frp/frpc.toml)是否与实际存放位置一致,路径错误会导致启动失败。

    客户端启动

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    #重载配置
    sudo systemctl daemon-reload

    # 启动服务
    systemctl start frpc.service

    # 查看服务状态
    systemctl status frpc.service

    # 停止服务
    systemctl stop frpc.service

    查看日志

    1
    2
    3
    4
    5
    6
    7
    8
    # 查看 frps 服务的最新日志
    journalctl -u frpc

    # 实时跟踪日志(类似 tail -f)
    journalctl -u frpc -f

    # 查看最近 N 行日志
    journalctl -u frpc -n 100

    image-20250725152322309

    设置开机自启

    1
    sudo systemctl enable frpc

    验证开机自启是否生效

    1
    systemctl status frpc

    image-20250725152426082

    1
    sudo systemctl is-enabled frps  # 输出 enabled 则表示成功

网页仪表盘

image-20250725152625076