近期,由于在外面实习,但是毕业设计所使用的树莓派开发板还静静躺在我的宿舍之中,为了能够每天下班也能做我的毕业设计,特地去搜索了一些关于内网穿透的东西,发现了GitHub上一个开源的内网穿透工具—frp,经过短暂的学习之后,终于实现了能够远程ssh和vnc到我的树莓派。

内网穿透

内网穿透即NAT穿透,网络连接时术语,计算机是局域网内时,外网与内网的计算机节点连接通信

准备工作

  • 具有公网IP的vps
  • 树莓派可上外网

frp

  • 项目地址:https://github.com/fatedier/frp
  • 项目功能:实现内网穿透,原理为端口映射与流量转发
  • 具体功能实现:远程ssh远程vnc远程web等等

配置

1.服务器端配置

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
# 远程以root用户登陆你的vps

# 下载frp
wget https://github.com/fatedier/frp/releases/download/v0.25.3/frp_0.25.3_linux_amd64.tar.gz

# 解压
tar -zxvf frp_0.25.3_linux_amd64.tar.gz

# 编辑配置文件
vim frp_0.25.3_linux_amd64/frps.ini

########
[common]
bind_port = 8000
########

# 将以上内容添加至配置文件中,bind_port 是frp客户端与服务器连接的端口,需要在你的vps开放权限

# 编辑服务文件
vim /lib/systemd/system/frp.service

######################################
[Unit]
Description=frp service
After=network.target syslog.target
Wants=network.target

[Service]
Type=simple
ExecStart=/root/frp_0.25.3_linux_amd64/frps -c /root/frp_0.25.3_linux_amd64/frps.ini

[Install]
WantedBy=multi-user.target
######################################

# 将以上内容添加至配置文件中,保存退出

################################
systemctl start frp # 启动frp服务
systemctl restart frp # 重启frp服务
systemctl enable frp # 将frp加入开机启动项
systemctl status frp # 查看frp服务状态
################################

2.客户端配置

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
# 登陆你的树莓派

# 下载frp
wget https://github.com/fatedier/frp/releases/download/v0.25.3/frp_0.25.3_linux_arm.tar.gz

# 解压
tar -zxvf frp_0.25.3_linux_arm.tar.gz

# 编辑配置文件
vim frp_0.25.3_linux_amd64/frpc.ini

################################
[common]
server_addr = 104.243.24.56
server_port = 8000 #这个端口必须跟服务器的一样

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000 #这个端口自行设置,需要vps开启这个端口的权限

[vnc]
type = tcp
local_ip = 127.0.0.1
local_port = 5900
remote_port = 6001 #这个端口自行设置,需要vps开启这个端口的权限
################################

# 将以上内容添加至配置文件中

# 编辑服务文件
vim /lib/systemd/system/frp.service

######################################
[Unit]
Description=frp service
After=network.target syslog.target
Wants=network.target

[Service]
Type=simple
ExecStart=/home/pi/frp_0.25.3_linux_arm/frpc -c /home/pi/frp_0.25.3_linux_arm/frpc.ini

[Install]
WantedBy=multi-user.target
######################################

# 将以上内容添加至配置文件中,保存退出

################################
systemctl start frp # 启动frp服务
systemctl restart frp # 重启frp服务
systemctl enable frp # 将frp加入开机启动项
systemctl status frp # 查看frp服务状态
################################

享用

  • 连接ssh:ssh -oPort=6000 pi@vps的ip地址
  • 连接vnc:用vnc客户端连接vps的ip地址:6001