自在工坊

走在代码边缘

使用FRP连接家中服务器

如何在公司或其它地方连接家中服务器?目前常用的稳定方案比较多,其中FRP方案是比较简单且稳定的一种方式,仅需要使用一台VPS做为中转服务器,在VPS与家中服务器分别安装FRP的服务端与客户端完成通信,记录操作方式如下;

普通安装方式配置文件如下:

[Unit]

Description=fraps service

After=network.target syslog.target

Wants=network.target
WantedBy=multi-user.target

服务器端配置如下:

# frps.ini

[Service]

Type=simple

[common]
bind_port = 7000 # 客户端与服务端通信端口号
vhost_http_port = 8081
token=XXXXXXXX # 客户端与服务端对接授权码可理解为密码

#启动服务的命令此处为frps的实际安装目录

$ ExecStart=/yourpath/frps -c /yourpath/frps.ini

Client端同理,按以上方式改写,客户端配置如下:

# frpc.ini

[common]
server_addr = 105.113.21.123 # 服务端VPS的IP地址请根据实际情况修改
server_port = 7000  客户端与服务端通信端口号与frps.ini中设置保持一致
token=XXXXXXXX # 客户端与服务端对接授权码与frps.ini中设置保持一致

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000 # 客户端接掌ssh请求的端口号确保开启防火墙允许使用此端口号

ExecStart=/yourpath/frp_0.37.0_linux_amd64/frps -c /yourpath/frp_0.37.0_linux_amd64/frps.ini

启动方式一:nohup

# server startup frps:

$ ./home/william/frp_0.37.0_linux_amd64/frps
或
$ nohup ./frps -c ./frps.ini &

# client startup frpc:
$ ./frpc -c ./frpc.ini
或
$ nohup ./frpc -c ./frpc.ini &

nohup /yourpath/frp_0.37.0_linux_amd64/frpc -c /yourpath/frp_0.37.0_linux_amd64/frpc.ini &

启动方式二:Debian系列中,可使用systemctl控制启动

debian9中:
$ sudo vim /lib/systemd/system/frps.service

Docker安装方式操作如下:

首先在服务器,客户端分别安装Docker,安装完成后参照以下方式部署Docker Image

服务器端Docker

# docker search frps
# docker pull snowdreamtech/frps
# docker run --restart=always --network host -d -v /etc/frp/frps.ini:/etc/frp/frps.ini --name frps snowdreamtech/frps

# /etc/frp/frps.ini # 为本机服务端配置文件
# :/etc/frp/frps.ini # 为Docker实体中文件位置

# docker ps # 查看运行状态,显示类似以下

CONTAINER ID   IMAGE                COMMAND                  CREATED        STATUS PORTS  NAMES
0ccff988564b   snowdreamtech/frps   "/bin/sh -c '/usr/bi…"   13 hours ago   Up 13 hours   frpc

客户端Docker

# docker pull snowdreamtech/frpc
# docker run --restart=always --network host -d -v /etc/frp/frpc.ini:/etc/frp/frpc.ini --name frpc snowdreamtech/frpc

# /etc/frp/frps.ini # 为本机客户端配置文件
# :/etc/frp/frps.ini # 为Docker实体中文件位置

正常启动后,可通过SSH方式登录验证,并根据反馈信息排查问题。 Docker方式可与普通方式共同使用,服务不受影响。

# ssh username@105.113.21.123 -p 6000
ip: # 服务端VPS的IP地址请根据实际情况修改
-p: # 端口号 客户端设置中进行设置注意在本例中为6000不是7001

Errors:

远程登录时反馈错误信息如下:

Bad ssh config on remote server. Cannot login in
/etc/ssh/ssh_config: line 55: Bad configuration option: permitrootlogin
/etc/ssh/ssh_config: terminating, 1 bad configuration options

To resolve this issue we have to commented out line 55 in /etc/ssh/ssh_config, which is # #PermitRootLogin yes 服务器端ssh设置错误,屏蔽PermitRootLogin yes行,root可登录设置应在/etc/ssh/sshd_config设置中进行。

PermitRootLogin is actually an option which is valid in the /etc/ssh/sshd_config file; not the ssh_config file. The difference is that the sshd_config file controls the SSH server and the ssh_config file controls the client. Therefore, it would indeed be a bad (invalid) config option in the client settings file.

Links: - https://hub.docker.com/r/snowdreamtech/frpc - https://hub.docker.com/r/fatedier/frp - https://www.jianshu.com/p/1272fbbecfeb - https://serverfault.com/questions/785290/bad-ssh-config-on-remote-server-cannot-login-in

Pelican Image

Comments