准备
树莓派一枚
具有公网IP的服务器一枚
已备案的域名一枚(非必须)
教程
下载程序
下载
下载地址:https://github.com/fatedier/frp/releases
## 服务端
wget https://github.com/fatedier/frp/releases/download/v0.32.0/frp_0.32.0_linux_386.tar.gz
##客户端
wget https://github.com/fatedier/frp/releases/download/v0.32.0/frp_0.32.0_linux_arm64.tar.gz
linux上如果下载太慢,可以在先下载好在上传到linux中。
解压
进入文件所在目录 输入命令解压并进入文件夹
##服务端 linux云服务器
tar zxvf frp_0.32.0_linux_386.tar.gz
cd frp_0.32.0_linux_386/
##客户端 树莓派
tar zxvf frp_0.32.0_linux_arm64.tar.gz
cd frp_0.32.0_linux_arm64/
部分主要文件说明
| 文件 | 功能 |
|---|---|
| frps | 服务端运行文件 |
| frpc | 客户端运行文件 |
| frps.ini | 服务端配置文件 |
| frpc.ini | 客户端配置文件 |
| frps_full.ini | 服务端配置文件说明 |
| frpc_full.ini | 客户端配置文件说明 |
配置服务端
配置文件详解:详解
vim frps.ini
[common]
bind_port = 服务端与客户端通信的端口
allow_ports = 指定客户端可以使用的端口范围,如(2000-3000,4000,5000-6000)
vhost_http_port = 外网访问此端口,映射到内网的http服务
vhost_https_port = 外网访问此端口,映射到内网的https服务
subdomain_host = 允许客户端自定义子域名,如frps.com ,需要解析为泛域名
dashboard_port = 控制台端口
dashboard_user = 控制台用户名
dashboard_pwd = 控制台密码
privilege_token = 身份密钥,客户端连接时需要与此相同
注意,如果启动报错,请检查软件版本,软件版本一定要与实际操作系统相符合
开启的端口需要在云服务器的安全组打开
配置客户端
vim frpc.ini
[common]
server_addr = 服务器地址
server_port = 与服务器bind_port相同
privilege_token =身份密钥
[ssh]
type = tcp
local_ip = 树莓派所在局域网
local_port = 22服务端口
remote_port = 6000 外网访问的端口
[web]
type = http
local_port = 80#服务端口
subdomain = 自定义子域名如(web)
#custom_domains = 域名或公网IP,与subdomain二选一
[web2]
type = http
local_port = 8080#服务端口
custom_domains = 自定义子域名
SSH连接
通过 公网IP:端口 连接,端口为上面配置对应本地22端口映射的远程端口
访问Web服务
没有域名访问web时,使用 http://IP:服务器的http映射端口
有域名时,通过 http://subdomain.subdomain_host:http端口访问。
设置开机自启
写启动脚本
服务端脚本
vim frps.sh
#!/bin/bash
### BEGIN INIT INFO
# Provides: frps
# Required-Start: $network
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: run frps
# Description: run frps
### END INIT INFO
logfile=/root/log/frps.log
case "$1" in
start)
echo $(date) "start" >> $logfile
/etc/frp/frps -c /etc/frp/frps.ini >> $logfile &
;;
stop)
echo $(date) 'stop' >> $logfile
killall /etc/frp/frps -c /etc/frp/frps.ini
;;
restart)
echo $(date) 'restart' >> $logfile
killall /etc/frp/frps -c /etc/frp/frps.ini
/etc/frp/frps -c /etc/frp/frps.ini >> $logfile &
;;
reload)
echo$(date) 'reload' >> $logfile
;;
status)
ps aux | grep /etc/frp/frps.ini
;;
*)
echo "Usage: touchfile.sh <start|stop|restart|reload>"
;;
esac
exit 0
添加执行权限并创建软连接到init.d文件夹,加入开机启动项
chmod 755 frps.sh
ln -s /root/autorun/frps.sh /etc/init.d/frps #必须用绝对路径
update-rc.d frps defaults
手动启动方法
cd /etc/init.d
./frpc start
客户端脚本
vim frpc.sh
#!/bin/bash
### BEGIN INIT INFO
# Provides: frpc
# Required-Start: $network
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: run frpc
# Description: run frpc
### END INIT INFO
logfile=/root/log/frpc.log
case "$1" in
start)
echo $(date) "start" >> $logfile
/etc/frp/frpc -c /etc/frp/frpc.ini >> $logfile &
;;
stop)
echo $(date) 'stop' >> $logfile
killall /etc/frp/frpc -c /etc/frp/frpc.ini
;;
restart)
echo $(date) 'restart' >> $logfile
killall /etc/frp/frpc -c /etc/frp/frpc.ini
/etc/frp/frpc -c /etc/frp/frpc.ini >> $logfile &
;;
reload)
echo$(date) 'reload' >> $logfile
;;
status)
ps aux | grep /etc/frp/frpc.ini
;;
*)
echo "Usage: touchfile.sh <start|stop|restart|reload>"
;;
esac
exit 0
添加执行权限并创建软连接到init.d文件夹,并加入开机启动项
chmod 755 frpc.sh
ln -s /root/autorun/frpc.sh /etc/init.d/frpc #必须用绝对路径
update-rc.d frpc defaults
手动启动方法
cd /etc/init.d
./frpc start
本文详细介绍如何使用frp在树莓派与公网服务器间建立远程连接,包括程序下载、配置服务端与客户端、设置开机自启等步骤,实现通过公网IP访问树莓派上的SSH及Web服务。
3886

被折叠的 条评论
为什么被折叠?



