203 lines
6.5 KiB
Markdown
Executable File
203 lines
6.5 KiB
Markdown
Executable File
# frp内网穿透配置
|
||
|
||
[TOC]
|
||
> frp的使用场景是,外网希望访问到内网,但是内网又无法或没有权限映射端口到公网IP的情况。frp需要借助公网的一台服务器进行流量转发。如果不希望转发或没有公网服务器,但可以映射端口,可参考 [9.动态IP情况下如何绑定域名](:/e4759c46346547dfa0775fa7ac3253e3) ;但如果既没有公网服务器,又无法端口映射,则可以参考这个:[4.内容同步和照片同步相关配置](:/8313850c5003427a936bbcea54521bc5) 。
|
||
|
||
## 前言
|
||
|
||
本文仅介绍访问放置在内网环境下的linux设备。其他不予考虑。
|
||
|
||
## frp介绍
|
||
|
||
1. frp需要在内网服务器(frpc,简称客户端)和公网服务器(frps简称服务端)分别部署;
|
||
2. 需要开多个端口;
|
||
3. 用户通过访问服务端frps,服务端连接到客户端frpc,进行访问内网服务器;不管是ssh还是其他web应用,都可以;
|
||
4. 如果有多台内网设备,多个地方的内网设备,也只需要有一个frps服务端,即可工作;
|
||
5. 后续如果要访问不同的应用,则需要设置不同的域名绑定。
|
||
|
||
## frp应用下载
|
||
|
||
地址在这里:[https://github.com/fatedier/frp/releases](https://github.com/fatedier/frp/releases)
|
||
|
||
解压以后分两部分内容:
|
||
|
||
```bash
|
||
-rwxr-xr-x 1 lighthouse memcache 10936320 May 27 16:31 frpc*
|
||
-rw-r--r-- 1 lighthouse memcache 10934 May 27 16:35 frpc_full.ini
|
||
-rw-r--r-- 1 lighthouse memcache 126 May 27 16:35 frpc.ini
|
||
-rwxr-xr-x 1 lighthouse memcache 14032896 May 27 16:31 frps*
|
||
-rw-r--r-- 1 lighthouse memcache 5560 May 27 16:35 frps_full.ini
|
||
-rw-r--r-- 1 lighthouse memcache 26 May 27 16:35 frps.ini
|
||
-rw-r--r-- 1 lighthouse memcache 11358 May 27 16:35 LICENSE
|
||
```
|
||
|
||
|
||
其中:frpc和frpc.ini 是属于客户端的,也就是装在内网服务器的;frps和frps.ini 属于服务端,装在公网vps或服务器上。
|
||
|
||
## 服务端安装
|
||
|
||
1. 把frps和frps.ini 放到某个目录。例:/opt/frp
|
||
2. 修改frps.ini 配置文件。frps_full.ini里面有详细的解释,我把我的贴出来:
|
||
|
||
```bash
|
||
1 [common]
|
||
2 bind_port = 7890 # 这个是服务器和客户端通信的主端口
|
||
3 dashboard_port = 7891 #这个端口是服务器的管理面板端口,可以查看frps服务器的情况
|
||
4 dashboard_user = maxwell #监控面板的用户名
|
||
5 dashboard_pwd = 123456 #密码
|
||
6 enable_prometheus = true #开启监控
|
||
7 vhost_http_port = 18083 #开放出去的,供外人访问的端口
|
||
8 token = big_brother #如果为了安全,可设置token,也可不射。一单设置,则在客户端也应设
|
||
```
|
||
|
||
3. 如果要让systemd来管理,那就造一个service:
|
||
|
||
```bash
|
||
1 [Unit]
|
||
2 Description=Frp Server Service
|
||
3 After=network.target
|
||
4
|
||
5 [Service]
|
||
6 Type=simple
|
||
7 User=nobody
|
||
8 Restart=on-failure
|
||
9 RestartSec=5s
|
||
10 ExecStart=/opt/frp/frps -c /opt/frp/frps.ini
|
||
11
|
||
12 [Install]
|
||
13 WantedBy=multi-user.target
|
||
```
|
||
|
||
命名为frps.service,保存到/etc/systemd/system/ 下,然后设置开机就行:
|
||
|
||
```bash
|
||
systemctl daemon-reload
|
||
systemctl enable frps.service
|
||
```
|
||
|
||
4. 上述所涉及到的端口,如开了防火墙,则需设置打开端口
|
||
5. 如要有安全性方面的考虑,则考虑tls证书加密传输。请参考frps配置文件。
|
||
|
||
## 客户端(内网设备)安装
|
||
|
||
1. 参考服务器端,把frps 改为frpc,其余操作都一样;
|
||
2. 主要讲讲配置文件:
|
||
|
||
```bash
|
||
[common]
|
||
server_addr = 1.2.3.4 #服务器端的地址或域名,都可以
|
||
server_port = 7890 #服务器端口,务必与服务器配置一致
|
||
admin_addr = 192.168.1.3 #这个本机的,写127.0.0.1也一样
|
||
admin_port = 7891 #以下三行,不看也罢
|
||
admin_user = maxwell
|
||
admin_pwd = 123456
|
||
log_file = /var/log/frpc.log #日志
|
||
tls_enable = true #这个开了以后,似乎服务器上也要开
|
||
token = dongshan #这个token要跟服务器的一致
|
||
[ssh]
|
||
type = tcp
|
||
local_ip = 192.168.1.3
|
||
local_port = 22
|
||
remote_port = 6000 #ssh访问的端口。以后可以用ssh root@1.2.3.4 -p 6000 访问内网了
|
||
|
||
[web]
|
||
type = http
|
||
local_ip = 192.168.1.3
|
||
local_port = 8081
|
||
custom_domains = web.yourdomain.com #以后要用这个域名 + 服务器开放的端口(我的上面的配置是18083)访问这个应用
|
||
# 比如,访问这个应用,就输入:web.yourdomain.com:18083
|
||
use_encryption = true
|
||
```
|
||
|
||
*( End of article )*
|
||
|
||
* * *
|
||
|
||
|
||
|
||
|
||
|
||
|
||
## 我自己的配置情况
|
||
|
||
|
||
| 序号| 服务器 | 服务器供应商 | 端口|应用文件位置 | 备注 |
|
||
| --- | --- | --- | --- |--- |--- |
|
||
| 1 | i.disbaidu.com | 搬瓦工cn2 | 7890/7891/18083 |/opt/frp | 服务器端,有效 |
|
||
| 2 | g.disbaidu.com | 南非cn2 | 7890/7891/18083 |/opt/frp | 服务器端,有效 |
|
||
| 3 | passwd.top |腾讯云 | 7890/7891/18083 |/opt/frp | 服务器端,有效 |
|
||
| 4 | home.disbaidu.com | 家庭宽带 |- |- | 未配置 |
|
||
| 5 | 192.168.168.74 | 公司内网服务器 | 7891 | /opt/frp | 服务器端,有效 |
|
||
|
||
### 服务端配置
|
||
frps.ini (/opt/frp/)
|
||
``` ini
|
||
[common]
|
||
bind_port = 7890
|
||
dashboard_port = 7891
|
||
dashboard_user = maxwell
|
||
dashboard_pwd = 6281613
|
||
enable_prometheus = true
|
||
vhost_http_port = 18083
|
||
token = dongshan
|
||
```
|
||
|
||
### 客户端配置
|
||
|
||
frpc.ini (/opt/frp/)
|
||
``` ini
|
||
[common]
|
||
server_addr = 1.117.242.66
|
||
server_port = 7890
|
||
admin_addr = 192.168.168.74
|
||
admin_port = 7891
|
||
admin_user = maxwell
|
||
admin_pwd = 6281613
|
||
log_file = /var/log/frpc.log
|
||
tls_enable = true
|
||
token = dongshan
|
||
[ssh] #ssh默认6000端口
|
||
type = tcp
|
||
local_ip = 192.168.168.74
|
||
local_port = 22
|
||
remote_port = 6000
|
||
use_encryption = true
|
||
use_compression = true
|
||
[web] #calibre-web的应用
|
||
type = http
|
||
local_ip = 192.168.168.74
|
||
local_port = 8083
|
||
custom_domains = books.regolith-linux.org.cn
|
||
use_encryption = true
|
||
use_compression = true
|
||
[web2] #calibre的应用
|
||
type = http
|
||
local_ip = 192.168.168.74
|
||
local_port = 8080
|
||
custom_domains = book.regolith-linux.org.cn
|
||
use_encryption = true
|
||
use_compression = true
|
||
[web3] #默认端口
|
||
type = http
|
||
local_ip = 192.168.168.74
|
||
local_port = 80
|
||
custom_domains = www.regolith-linux.org.cn
|
||
use_encryption = true
|
||
use_compression = true
|
||
[web4] #远程访问linux桌面的端口
|
||
type = tcp
|
||
local_ip = 192.168.168.74
|
||
local_port = 3389
|
||
remote_port = 7001
|
||
[resilio] #resilio服务
|
||
type = http
|
||
local_ip = 192.168.168.74
|
||
local_port = 8888
|
||
custom_domains = sync.regolith-linux.org.cn
|
||
use_encryption = true
|
||
|
||
```
|
||
|
||
|
||
|