内网穿透工具 frp 介绍

1.下载

去它的release页面下载对应系统的版本

2.配置与安装

服务端使用frps文件,客户端使用frpc文件,frps和frpc分别对应服务端和客户端的你主程序,.ini则分别是他们的对应配置文件,主要的配置见参考链接1,参考链接2,任选其1即可.

3.后台运行

3.1 nohup

使用nohup可以让你的程序在此次连接结束后依旧运行,远程服务器的时候可以使用
例如

1
nohup ./frpc -c ./frpc.ini

3.2 以服务的方式运行

3.2.1 支持systemctl的方式

新版本解压后可以看到有systemd这个文件夹,里面是已经写好的服务文件,这里以frpc.service为例配置为客户端开机重启的服务

  • 首先

你需要把frpc.service文件中的ExecStartExecReload替换你自己的程序路径

  • 其次

frpc.service文件放到系统服务对应的位置,ubuntu 16.04放在/lib/systemd/system/下,CentOS 7放在/usr/lib/systemd/system/

  • 最后
1
2
3
4
5
6
7
8
9
10
# 使用service或者systemctl启动服务
service frpc start|reload|stop|status
systemctl frpc start|reload|stop|status

# 开机时启动一个服务
systemctl enabl frpc
# 开机时禁用一个服务
systemctl disable frpc
#查看服务是否开机启动
systemctl is-enabled frpc

3.2.2 支持services方式

如果你的系统版本比较老,比如RedHat 6.8,这类系统不支持systemctl启动服务,那么你就需要手写一个脚本来启动服务,并把这个服务放在/etc/init.d目录下,
参考脚本,以frpc为例,这个脚本是我以sshd为基础改的:

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
#chkconfig 2345 99 01
# frpc
#
# description: frpc is a niwangchuantou tool
#
# processname: frpc
# config: /etc/frp/frpc.ini

### BEGIN INIT INFO
# Provides: frpc
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 6
# Short-Description: Start up the frpc server daemon
# Description: frpc is a neiwangchuantou tool
### END INIT INFO

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0
prog="frpc"

# Some functions to make the below more readable
FRPC=/usr/bin/frpc
INI_FILE=/etc/frp/frpc.ini

start()
{
$FRPC -c $INI_FILE
}

stop()
{
killall $prog 2>/dev/null
}

reload()
{
$FRPC reload -c $INI_FILE
}

restart() {
stop
start
}

case "$1" in
start)
start &
;;
stop)
stop &
;;
restart)
restart &
;;
reload)
reload &
;;
*)
echo $"Usage: $0 {start|stop|restart|reload}"
RETVAL=2
esac
exit $RETVAL

配置

1
2
3
4
赋予脚本执行权限
chmod +x frpc.service
使用前加入chkconfig管理列表
chkconfig --add frpc

使用:

1
2
#脚本中已经自动为开机启动
service frpc start|reload|restart|stop

参考链接

frp参考链接

参考1

frp内网穿透

参考2

使用frp实现内网穿透

参考3

Frp后台自动启动的几个方法

参考4

FRP局域网穿透+linux自动监控服务运行

linux 配置服务参考连接

centos6添加系统服务
CentOS6自定义服务控制脚本
Centos7 服务 service 设置命令 systemctl 用法

以下是frp的github ReadMe

英文版
中文版