1

给实验室服务器配置内网穿透,避免内网尴尬,在家也能用服务器~

 2 years ago
source link: https://blog.csdn.net/curledgoat/article/details/122673606
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

给实验室服务器配置内网穿透,避免内网尴尬,在家也能用服务器~

专栏收录该内容
6 篇文章 9 订阅

我这个环境呢,学校实验室的服务器是两块 3090 的 ubuntu,可以看我本专栏之前的文章,你能更细致的了解到我都干了些啥。realVNC 确实是实现了只要能联网就能用图形化的功能。但是这一放假不在同一个省份,250公里,图形界面反应太慢了。于是内网穿透咱就得用起来了呀~

内网穿透 (fast reverse proxy, frp),是一个高性能反向代理应用,可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转,暴露到公网中。通过在具有公网 IP 的节点上部署 frp 服务端,可以轻松地将内网服务穿透到公网,同时提供诸多专业的功能特性。

作者是中国人哦~

这是 Github 项目主页,这是官方文档。你要有一个公网 IP 节点,一般咱就是嫖个租个云服务器,这边需要读者自己了解一下阿里云、腾讯云、百度云、金山云、京东云等等云服务,咱就用华为云来举例子,因为阿里云免费到期了,华为云有免费活动
在这里插入图片描述
是租一个最便宜的计算型服务器就好,通常就叫弹性云服务器ECS,各种云服务的类型大概翻翻看看介绍,基本上就都理解的差不多了。有搭网站的、GPU的、做电商的、数据库的,大差不离,名字也是百花齐放啊。

现在你就有一台公网节点了~
在这里插入图片描述


接下来就要在公网 IP 节点服务器和内网服务器上安装 frp 服务的 service 端和 client 端了,把对应版本的服务下载到服务器上。一般咱就都是 linux_amd64 那个版本

root@ecs-curledgoat:~# ls -al
total 8680
drwx------  5 root root    4096 Jan 24 20:21 .
drwxr-xr-x 25 root root    4096 Jan 24 18:26 ..
-rw-r--r--  1 root root     158 Jan 24 18:32 .bash_history
-rw-r--r--  1 root root    3106 Apr  9  2018 .bashrc
drwx------  3 root root    4096 Feb 24  2021 .cache
-rw-r--r--  1 root root 8848900 Jan 24 20:21 frp_0.38.0_linux_amd64.tar.gz
drwx------  3 root root    4096 Feb 24  2021 .gnupg
-rw-r--r--  1 root root     148 Aug 17  2015 .profile
drwx------  2 root root    4096 Jan 24 16:01 .ssh
-rw-------  1 root root     120 Jan 24 20:14 .Xauthority

可以看到咱这边已经有了,然后解压出来,

root@ecs-curledgoat:~# tar -zxvf frp_0.38.0_linux_amd64.tar.gz
frp_0.38.0_linux_amd64/
frp_0.38.0_linux_amd64/frpc_full.ini
frp_0.38.0_linux_amd64/frps.ini
frp_0.38.0_linux_amd64/frps_full.ini
frp_0.38.0_linux_amd64/LICENSE
frp_0.38.0_linux_amd64/frpc
frp_0.38.0_linux_amd64/systemd/
frp_0.38.0_linux_amd64/systemd/[email protected]
frp_0.38.0_linux_amd64/systemd/frpc.service
frp_0.38.0_linux_amd64/systemd/frps.service
frp_0.38.0_linux_amd64/systemd/[email protected]
frp_0.38.0_linux_amd64/frpc.ini
frp_0.38.0_linux_amd64/frps

root@ecs-curledgoat:~# cd frp_0.38.0_linux_amd64/
root@ecs-curledgoat:~/frp_0.38.0_linux_amd64# ls -al
total 23188
drwxr-xr-x 3 1001  121     4096 Oct 25 21:01 .
drwx------ 6 root root     4096 Jan 24 20:24 ..
-rwxr-xr-x 1 1001  121 10219520 Oct 25 20:58 frpc
-rw-r--r-- 1 1001  121     9699 Oct 25 21:01 frpc_full.ini
-rw-r--r-- 1 1001  121      126 Oct 25 21:01 frpc.ini
-rwxr-xr-x 1 1001  121 13471744 Oct 25 20:58 frps
-rw-r--r-- 1 1001  121     5010 Oct 25 21:01 frps_full.ini
-rw-r--r-- 1 1001  121       26 Oct 25 21:01 frps.ini
-rw-r--r-- 1 1001  121    11358 Oct 25 21:01 LICENSE
drwxr-xr-x 2 1001  121     4096 Oct 25 21:01 systemd

frpc 是内网服务器用的,frps 是公网服务器用的。


接下来写配置文件。在公网机器上修改 frps.ini 文件,在内网机器上修改 frpc.ini 文件,假设 frps 所在服务器的公网 IP 为 x.x.x.x:

# frps
[common]
bind_port = 7000
# frpc
[common]
tls_enable = true #  frpc启动报错login to server failed: EOF时,添加这一行
server_addr = x.x.x.x
server_port = 7000

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

一定要注意,你云服务器的 7000 6000 端口一定要开放,在云服务器的安全策略中是可以配置的。你也可以根据自己的喜好设置不同的端口。


通过 systemd 启动服务并且配置开机自启,frp 的压缩包里有 systemd 目录,我们使用其中的单元文件 frpc.servicefrps.service 来配置,当然也可以使用单元模板文件

frps.service 的文件内容如下,注释是后期补上去的

[Unit]
Description=Frp Server Service # 服务描述
After=network.target # frps 将会在 network.service 启动完毕之后再启动

[Service]
Type=simple # 不论进程是否启动成功,systemctl start 都执行成功
User=nobody # 设置进程在执行时使用的用户
Restart=on-failure # on-failure 表示仅在服务进程异常退出时重启
RestartSec=5s # 设置在重启服务前暂停多长时间
ExecStart=/usr/bin/frps -c /etc/frp/frps.ini # 在启动该服务时需要执行的命令行

[Install]
WantedBy=multi-user.target # 用于 systemctl enable 时创建软连接

将这个文件拷贝到 /etc/systemd/system/ 目录下。并根据配置文件中的 ExecStart=/usr/bin/frps -c /etc/frp/frps.inifrpsfrps.ini 文件放置到对应的路径下。这个非常重要! 否则 systemd 无法通过 frps.service 配置文件所给出的路径去执行 frps 服务。

启动 frps 服务

> sudo systemctl start frps.service

查看 frps 服务的状态 (如果服务运行正常,小圆点会是绿色的,Markdown 的代码编辑显示不出来)

> sudo systemctl status frps.service
● frps.service - Frp Server Service
   Loaded: loaded (/etc/systemd/system/frps.service; enabled; vendor preset: enabled)
   Active: active (running) since Mon 2022-01-24 22:19:29 CST; 14s ago
 Main PID: 13201 (frps)
    Tasks: 6 (limit: 4658)
   CGroup: /system.slice/frps.service
           └─13201 /usr/bin/frps -c /etc/frp/frps.ini

Jan 24 22:19:29 ecs-curledgoat systemd[1]: Started Frp Server Service.
Jan 24 22:19:29 ecs-curledgoat frps[13201]: 2022/01/24 22:19:29 [I] [root.go:200] frps uses config file: /etc/frp/frps.ini
Jan 24 22:19:29 ecs-curledgoat frps[13201]: 2022/01/24 22:19:29 [I] [service.go:192] frps tcp listen on 0.0.0.0:7000
Jan 24 22:19:29 ecs-curledgoat frps[13201]: 2022/01/24 22:19:29 [I] [root.go:209] frps started successfully

停止 frps:

> sudo systemctl stop frps.service

开启 frps 服务开机自启动:

> systemctl enable frps.service
> Created symlink /etc/systemd/system/multi-user.target.wants/frps.service → /etc/systemd/system/frps.service.

禁用 frps 服务开机自启动:

> sudo systemctl disable frps.service
Removed /etc/systemd/system/multi-user.target.wants/frps.service.

客户端的 frpc 配置是完全一样的,就不再重复给出了。


SSH 连接内网服务器时,使用 内网服务器用户名@外网服务器地址:服务器端口(上例中的7000)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK