frp 配置教程

1 下载

frp 采用 Golang 编写,支持跨平台,仅需下载对应平台的二进制文件即可执行,没有额外依赖。

可以在 Github 的 Release 页面中下载到最新版本的客户端和服务端二进制文件,所有文件被打包在一个压缩包中。

2 使用

2.1 解压缩

解压下载的压缩包,Linux 通过命令tar -zxvf 包名.tar.gz,Windows 直接通过解压软件解压 zip 包。

将其中的 frpc 拷贝到内网服务所在的机器上,将 frps 拷贝到具有公网 IP 的机器上,放置在任意目录。

2.2 编写配置文件

启动服务端

配置文件示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# frps.ini
[common]
# frp监听的端口,默认是7000,可以改成其他的
bind_port = ××××
# 授权码,请改成更复杂的
token = ×××× # 这个token之后在客户端会用到

# frp管理后台端口,请按自己需求更改
dashboard_port = ××××
# frp管理后台用户名和密码,请改成自己的
dashboard_user = ××××
dashboard_pwd = ××××
enable_prometheus = true

# frp日志配置,按需配置
log_file = /var/log/frps.log
log_level = info
log_max_days = 3

配置并保存后,在当前目录下输入启动命令 ./frps -c ./frps.ini 启动服务端,观察日志文件得到如下输出:

1
2
3
2022/11/07 13:47:45 [I] [service.go:124] frps tcp listen on 0.0.0.0:7070
2022/11/07 13:47:45 [I] [service.go:166] http service listen on 0.0.0.0:8081
2022/11/07 13:47:45 [I] [root.go:204] Start frps success

启动客户端

配置文件示例如下:

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
# frpc.ini
[common]
server_addr = 服务器公网ip
server_port = ×××× # 与frps.ini的bind_port一致
token = ×××× # 与frps.ini的token一致

# 配置ssh服务
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = ×××× # 这个自定义,之后再ssh连接的时候要用

# 配置http服务,可用于小程序开发、远程调试等,如果没有可以不写下面的
[web]
type = http
local_ip = 127.0.0.1
local_port = ××××
subdomain = www.domain.com # web域名
remote_port = ×××× 自定义的远程服务器端口,例如8080

# frp日志配置,按需配置
log_file = /var/log/frpc.log
log_level = info
log_max_days = 3

配置并保存后,在当前目录下输入启动命令 ./frpc -c ./frpc.ini 启动客户端,观察日志文件得到如下输出:

1
2
3
4
2022/11/08 20:58:06 [I] [service.go:214] login to server success, get run id [7524d529f37d685e], server udp port [0]
2022/11/08 20:58:06 [I] [proxy_manager.go:137] [7524d529f37d685e] proxy added: [ssh web]
2022/11/08 20:58:06 [I] [control.go:143] [web] start proxy success
2022/11/08 20:58:06 [I] [control.go:143] [ssh] start proxy success

3 开机自启动设置

在 Linux 系统下,使用 systemd 可以方便地控制 frp 服务端 frps 的启动和停止、配置后台运行和开启自启。

3.1 服务端

使用文本编辑器,如 vim 创建并编辑 frps.service :

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

写入内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
# 服务名称,可自定义
Description = frp server
After = network.target syslog.target
Wants = network.target

[Service]
Type = simple
# 启动frps的命令
ExecStart = /path/to/frps -c /path/to/frps.ini

[Install]
WantedBy = multi-user.target

管理 frps

1
2
3
4
5
6
7
8
# 启动frp
systemctl start frps # frps就是您上面创建的文件名
# 停止frp
systemctl stop frps
# 重启frp
systemctl restart frps
# 查看frp状态
systemctl status frps

配置 frps 开机自启

1
systemctl enable frps

3.2 客户端

使用文本编辑器,如 vim 创建并编辑 frpc.service :

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

写入内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
# 服务名称,可自定义
Description = frp client
After = network.target syslog.target
Wants = network.target

[Service]
Type = simple
# 启动frps的命令
ExecStart = /path/to/frpc -c /path/to/frpc.ini

[Install]
WantedBy = multi-user.target

管理 frpc

1
2
3
4
5
6
7
8
# 启动frp
systemctl start frpc # frpc就是您上面创建的文件名
# 停止frp
systemctl stop frpc
# 重启frp
systemctl restart frpc
# 查看frp状态
systemctl status frpc

配置 frpc 开机自启

1
systemctl enable frpc

frp 配置教程
https://zdawng.github.io/posts/1c4bcd74/
作者
ZDawnG
发布于
2023年8月19日
许可协议