GitHub - fatedier/frp: A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet.
服务端需要部署在一台能在公网访问的机器上
服务端
以服务端IP地址47.10.13.3为例
配置文件
[root@xx bin]# pwd
/usr/bin
[root@xx bin]# cat frps.ini
[common]
# frp监听的端口,默认是7000,可以改成其他的
bind_port=5001
# 授权码,请改成更复杂的
token=2022
# frp管理后台端口,请按自己需求更改
dashboard_port = 5000
# frp管理后台用户名和密码,请改成自己的
dashboard_user = admin
dashboard_pwd = admin
enable_prometheus = true
# frp日志配置
log_file = /root/frps.log
log_level = debug
log_max_days = 5
将服务端程序及配置文件放到/usr/bin目录下
[root@xx bin]# pwd
/usr/bin
[root@xx bin]# ll frps*
-rwxr-xr-x 1 root root 14036992 Sep 23 21:37 frps
-rw-r--r-- 1 root root 425 Sep 23 23:53 frps.ini
配置服务端frps服务及开机自动启动服务
[root@xx system]# pwd
/lib/systemd/system
[root@xx system]# cat frps.service
[Unit]
Description=Frps Server Service
After=network.target
[Service]
TimeoutStartSec=30
Restart=on-failure
RestartSec=5s
ExecStart=/usr/bin/frps -c /usr/bin/frps.ini
ExecStop=/bin/kill $MAINPID
ExecReload=/usr/bin/frps reload -c /usr/bin/frps.ini
[Install]
WantedBy=multi-user.target
# 重新加载配置文件
systemctl daemon-reload
# 启动frpc服务
systemctl start frps
# 开机自启动frpc服务
systemctl enable frps
# 重启服务
systemctl restart frps
服务启动后,可以通过?http://47.10.13.3:5000 访问frp的管理页面,默认用户名密码都是admin
查看服务状态
[root@xx system]# service frps.service status
Redirecting to /bin/systemctl status frps.service
● frps.service - Frps Server Service
Loaded: loaded (/usr/lib/systemd/system/frps.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2022-09-24 09:04:48 CST; 4min 39s ago
Main PID: 16047 (frps)
CGroup: /system.slice/frps.service
└─16047 /usr/bin/frps -c /usr/bin/frps.ini
Sep 24 09:04:48 lm systemd[1]: Started Frps Server Service.
服务端OS为CentOS,查看服务启动日志
[root@xx log]# pwd
/var/log
[root@xx log]# tail -20 messages
Sep 24 09:04:40 lm systemd: Reloading.
Sep 24 09:04:48 lm systemd: Started Frps Server Service.
Sep 24 09:10:01 lm systemd: Started Session 199071 of user root.
查看frps日志
[root@xx ~]# pwd
/root
[root@xx ~]# tail -200 frps.log
2022/09/23 23:54:35 [I] [root.go:209] frps uses config file: ./frps.ini
2022/09/23 23:54:35 [I] [service.go:194] frps tcp listen on 0.0.0.0:65001
2022/09/23 23:54:35 [I] [service.go:293] Dashboard listen on 0.0.0.0:65000
2022/09/23 23:54:35 [I] [root.go:218] frps started successfully
2022/09/23 23:54:53 [I] [service.go:450] [acad8bcb8f05e9e4] client login info: ip [117.143.51.99:9017] version [0.44.0] hostname [] os [linux] arch [amd64]
2022/09/23 23:54:53 [D] [control.go:219] [acad8bcb8f05e9e4] new work connection registered
2022/09/23 23:54:53 [I] [tcp.go:64] [acad8bcb8f05e9e4] [ssh] tcp proxy listen port [65002]
2022/09/23 23:54:53 [I] [control.go:465] [acad8bcb8f05e9e4] new proxy [ssh] type [tcp] success
2022/09/23 23:55:23 [D] [control.go:494] [acad8bcb8f05e9e4] receive heartbeat
2022/09/24 09:04:48 [I] [root.go:209] frps uses config file: /usr/bin/frps.ini
2022/09/24 09:04:48 [I] [service.go:194] frps tcp listen on 0.0.0.0:65001
2022/09/24 09:04:48 [I] [service.go:293] Dashboard listen on 0.0.0.0:65000
2022/09/24 09:04:48 [I] [root.go:218] frps started successfully
2022/09/24 09:04:53 [I] [service.go:450] [420388e842065db5] client login info: ip [117.143.51.99:8840] version [0.44.0] hostname [] os [linux] arch [amd64]
2022/09/24 09:04:53 [D] [control.go:219] [420388e842065db5] new work connection registered
2022/09/24 09:04:53 [I] [tcp.go:64] [420388e842065db5] [ssh] tcp proxy listen port [65002]
2022/09/24 09:04:53 [I] [control.go:465] [420388e842065db5] new proxy [ssh] type [tcp] success
2022/09/24 09:05:23 [D] [control.go:494] [420388e842065db5] receive heartbeat
2022/09/24 09:05:53 [D] [control.go:494] [420388e842065db5] receive heartbeat
2022/09/24 09:06:23 [D] [control.go:494] [420388e842065db5] receive heartbeat
2022/09/24 09:06:53 [D] [control.go:494] [420388e842065db5] receive heartbeat
客户端
客户端就是连接到任意内网的机器
将客户端程序及配置文件放到/usr/local/bin下
shang@shang-pc:/usr/local/bin$ pwd
/usr/local/bin
shang@shang-pc:/usr/local/bin$ ll frpc*
-rwxr-xr-x 1 root root 10997760 Sep 24 00:50 frpc*
-rw-r--r-- 1 root root 153 Sep 24 00:50 frpc.ini
配置文件
shang@shang-pc:/usr/local/bin$ pwd
/usr/local/bin
shang@shang-pc:/usr/local/bin$ cat frpc.ini
[common]
### 服务端IP地址,一般为公网IP
server_addr = 47.10.13.3
### 服务端端口,接收客户端TCP连接
server_port = 5001
token = 2022
[ssh]
type = tcp
### 客户端内网IP
local_ip = 192.168.0.200
### 客户端端口
local_port = 22
### 服务端会将 5002 端口接收到的TCP数据包转发到客户端的22端口
remote_port = 5002
配置客户端机器的frpc服务及开机自动启动
shang@shang-pc:/lib/systemd/system$ pwd
/lib/systemd/system
shang@shang-pc:/lib/systemd/system$ cat frpc.service
[Unit]
Description=Frpc Client Service
After=network.target
[Service]
TimeoutStartSec=30
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/frpc -c /usr/local//bin/frpc.ini
ExecStop=/bin/kill $MAINPID
ExecReload=/usr/local/bin/frpc reload -c /usr/local/bin/frpc.ini
[Install]
WantedBy=multi-user.target
# 重新加载配置文件
systemctl daemon-reload
# 启动frpc服务
systemctl start frpc
# 开机自启动frpc服务
systemctl enable frpc
# 重启服务
systemctl restart frpc
查看服务状态
shang@shang-pc:/lib/systemd/system$ service frpc status
● frpc.service - Frpc Client Service
Loaded: loaded (/lib/systemd/system/frpc.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2022-09-24 01:04:53 UTC; 17min ago
Main PID: 1966 (frpc)
Tasks: 6 (limit: 4536)
Memory: 4.0M
CPU: 814ms
CGroup: /system.slice/frpc.service
└─1966 /usr/local/bin/frpc -c /usr/local//bin/frpc.ini
Sep 24 01:04:53 shang-pc systemd[1]: Started Frpc Client Service.
Sep 24 01:04:53 shang-pc frpc[1966]: 2022/09/24 01:04:53 [I] [service.go:349] [420388e842065db5] login to server success, get run id [4>
Sep 24 01:04:53 shang-pc frpc[1966]: 2022/09/24 01:04:53 [I] [proxy_manager.go:144] [420388e842065db5] proxy added: [ssh]
Sep 24 01:04:53 shang-pc frpc[1966]: 2022/09/24 01:04:53 [I] [control.go:181] [420388e842065db5] [ssh] start proxy success
?系统日志
shang@shang-pc:/var/log$ pwd
/var/log
shang@shang-pc:/var/log$ tail -200 syslog
Sep 24 01:02:26 shang-pc systemd[1]: Started Frpc Client Service.
Sep 24 01:02:26 shang-pc frpc[1820]: 2022/09/24 01:02:26 #033[1;33m[W] [service.go:128] login to server failed: dial tcp 47.103.103.39:65001: connect: connection refused#033[0m
Sep 24 01:02:26 shang-pc frpc[1820]: dial tcp 47.103.103.39:65001: connect: connection refused
Sep 24 01:02:26 shang-pc systemd[1]: frpc.service: Main process exited, code=exited, status=1/FAILURE
Sep 24 01:02:26 shang-pc systemd[1]: frpc.service: Failed with result 'exit-code'.
Sep 24 01:02:31 shang-pc systemd[1]: frpc.service: Scheduled restart job, restart counter is at 108.
Sep 24 01:02:31 shang-pc systemd[1]: Stopped Frpc Client Service.
Sep 24 01:04:53 shang-pc systemd[1]: Started Frpc Client Service.
Sep 24 01:04:53 shang-pc frpc[1966]: 2022/09/24 01:04:53 #033[1;34m[I] [service.go:349] [420388e842065db5] login to server success, get run id [420388e842065db5], server udp port [0]#033[0m
Sep 24 01:04:53 shang-pc frpc[1966]: 2022/09/24 01:04:53 #033[1;34m[I] [proxy_manager.go:144] [420388e842065db5] proxy added: [ssh]#033[0m
Sep 24 01:04:53 shang-pc frpc[1966]: 2022/09/24 01:04:53 #033[1;34m[I] [control.go:181] [420388e842065db5] [ssh] start proxy success#033[0m
通过公网IP登录内网机器
(base) [ 9:23上午 ] [ shang@iMac:~ ]
$ ssh -p 5002 shang@47.10.13.3
shang@47.10.13.3's password:
Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-43-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Sat Sep 24 01:23:33 AM UTC 2022
System load: 0.0 Processes: 120
Usage of /: 6.6% of 97.87GB Users logged in: 1
Memory usage: 5% IPv4 address for enp2s0: 192.168.0.200
Swap usage: 0%
65 updates can be applied immediately.
30 of these updates are standard security updates.
To see these additional updates run: apt list --upgradable
Last login: Sat Sep 24 01:23:34 2022 from 192.168.0.200
shang@shang-pc:~$ pwd
/home/shang
shang@shang-pc:~$ uname -a
Linux shang-pc 5.15.0-43-generic #46-Ubuntu SMP Tue Jul 12 10:30:17 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
搭建好内网穿透服务后,就可以通过公网IP访问到内网机器。
在内网机器的主板bios中设置通电自启,再配合一个智能插座,就可以实现远程开机。
内网机器开机后,会启动frpc服务,建立内网穿透连接
|