frp配置内网穿透

编辑于 2026-06-06 00:52:57 阅读 2064

情况是这样的,公司有台内网服务器,有一天公司要求部分人员在家办公。一般来讲,在家办公的同事想连内网服务器是不可能的。为了解决这个问题内网穿透就该了解一下了

frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。

frp有很多功能,这里只介绍ssh和web服务

模拟场景

  • 一台公网服务器(Linux,47.98.227.00)
  • 一台虚拟机(Linux,相当于内网服务器)
  • ssh服务测试:宿主机通过frp访问虚拟机;公网服务器通过frp访问虚拟机
  • web服务测试:通过公网IP+端口号访问内网服务

ssh服务

公网服务器

[root@iZbp1430s16l9piu268n8rZ data]# wget https://github.com/fatedier/frp/releases/download/v0.69.1/frp_0.69.1_linux_amd64.tar.gz
[root@iZbp1430s16l9piu268n8rZ data]# tar -xvzf frp_0.69.1_linux_amd64.tar.gz 
[root@iZbp1430s16l9piu268n8rZ data]# mv frp_0.69.1_linux_amd64 frp
[root@iZbp1430s16l9piu268n8rZ data]# cd frp/
[root@iZbp1430s16l9piu268n8rZ frp]# ls
frpc  frpc.toml  frps  frps.toml  LICENSE
[root@iZbp1430s16l9piu268n8rZ frp]# cat frps.toml 
bindPort = 7000
#默认frps.toml不用修改,直接启动服务
[root@iZbp1430s16l9piu268n8rZ frp]# ./frps -c ./frps.toml

虚拟机

[root@nfsFileSystem vagrant]# wget https://github.com/fatedier/frp/releases/download/v0.69.1/frp_0.69.1_linux_amd64.tar.gz
[root@nfsFileSystem vagrant]# tar -xvzf frp_0.69.1_linux_amd64.tar.gz 
[root@nfsFileSystem vagrant]# mv frp_0.69.1_linux_amd64 frp
[root@nfsFileSystem vagrant]# cd frp/
[root@nfsFileSystem frp]# ls
frpc  frpc.toml  frps  frps.toml  LICENSE
[root@nfsFileSystem frp]# cat cat frpc.toml 
serverAddr = "47.98.227.00"
serverPort = 7000

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

#启动客户端
[root@nfsFileSystem frp]# ./frpc -c ./frpc.toml

确认客户端,服务端都起来了,开始测试

宿主机通过frp访问虚拟机

实现免密码登陆,将宿主机的public key添加的虚拟机[root@nfsFileSystem frp]# echo '宿主机的public key'>> ~/.ssh/authorized_keys

cuiwei@weideMacBook-Pro nfsFileSystem % ssh -oPort=6000 root@47.98.227.00
Last login: Thu Dec  1 13:51:32 2022
[root@nfsFileSystem ~]# ls
[root@nfsFileSystem ~]# ls /vagrant/
Vagrantfile

公网服务器通过frp访问虚拟机

实现免密码登陆,将公网服务器的public key添加的虚拟机[root@nfsFileSystem frp]# echo '公网服务器的public key'>> ~/.ssh/authorized_keys

[root@iZbp1430s16l9piu268n8rZ voice]# ssh -oPort=6000 root@47.98.227.00
Last login: Thu Dec  1 13:59:05 2022 from 127.0.0.1
[root@nfsFileSystem ~]# ls
[root@nfsFileSystem ~]# ls /vagrant/
Vagrantfile

web服务

公网服务器

frps.toml追加以下配置

vhostHTTPPort = 8999

然后,重新启动服务端

[root@iZbp1430s16l9piu268n8rZ frp]# ./frps -c ./frps.toml

虚拟机

准备一个web服务,确保可以通过localhost:8080访问

frpc.toml追加以下配置

[[proxies]]
name = "web"
type = "http"
localIP = "localhost"
localPort = 8080
customDomains = ["test1.cuiwei.net"]

然后,重新启动客户端

[root@nfsFileSystem frp]# ./frpc -c ./frpc.toml

测试一下

访问 http://test1.cuiwei.net:8999/

不出意外看到的就是虚拟机8080端口提供的服务

test1.cuiwei.net 域名需要解析到 47.98.227.00

身份认证

这个是避免ip, 端口泄漏,任意客户端都能连;有了身份认证,必须两端的token一致才能连

# frpc.toml
auth.token = "abc"

# frps.toml
bindPort = 7000
auth.token = "abc"

设置 BasicAuth 鉴权

# frpc.toml
[[proxies]]
name = "web"
type = "http"
localPort = 80
customDomains = ["test.yourdomain.com"]
httpUser = "abc"
httpPassword = "abc"

仪表盘

frps.toml追加以下配置

# 默认为 127.0.0.1,如果需要公网访问,需要修改为 0.0.0.0。
webServer.addr = "0.0.0.0"
webServer.port = 7500
# dashboard 用户名密码,可选,默认为空
webServer.user = "admin"
webServer.password = "admin"

然后,重新启动服务端

[root@iZbp1430s16l9piu268n8rZ frp]# ./frps -c ./frps.toml

测试一下

访问 http://47.98.227.00:7500/

ScreenShot_20260606_004344_106.png

提示

上面提到的端口7000,6000,8999,7500都需要在公网服务器放开

优化:上面的客户端,服务端的启动方式可以改为后台进程,交给 Supervisor 管理。可参考 https://www.cuiwei.net/p/1109683129

参考

https://github.com/fatedier/frp

广而告之,我的新作品《语音助手》上架Google Play了,欢迎下载体验