专栏/frp内网穿透实现 SSH 安全访问

frp内网穿透实现 SSH 安全访问

2023年02月12日 04:58--浏览 · --点赞 · --评论
粉丝:284文章:89

这个示例将会创建一个只有自己能访问到的 SSH 服务代理。

对于某些服务来说如果直接暴露于公网上将会存在安全隐患。

使用 stcp(secret tcp) 类型的代理可以避免让任何人都能访问到要穿透的服务,但是访问者也需要运行另外一个 frpc 客户端。

配置公网IP服务器

修改配置文件 frps.ini

[root@RockyLinux ~]# vim /usr/local/frp/frps.ini

[common]
bind_port = 7000

使用 systemd 启动 frps

[root@RockyLinux ~]# systemctl start frps.service

配置内网需要暴露的机器

修改配置 frpc.ini

[root@Rocky-Server ~]# vim /usr/local/frp/frpc.ini

[common]
server_addr = x.x.x.x    #这里修改为公网服务器的IP地址
server_port = 7000

[secret_ssh]
type = stcp
# 只有 sk 一致的用户才能访问到此服务
sk = abcdefg
local_ip = 127.0.0.1
local_port = 22

后台启动 frpc

[root@Rocky-Server frp]# cd /usr/local/frp/
[root@Rocky-Server frp]# ./frpc -c ./frpc.ini &>/dev/null &

配置要访问内网服务的机器

修改配置 frpc.ini

[root@Rocky-Client ~]# vim /usr/local/frp/frpc.ini

[common]
server_addr = x.x.x.x    这里修改为公网服务器的IP地址
server_port = 7000

[secret_ssh_visitor]
type = stcp
# stcp 的访问者
role = visitor
# 要访问的 stcp 代理的名字
server_name = secret_ssh
sk = abcdefg
# 绑定本地端口用于访问 SSH 服务
bind_addr = 127.0.0.1
bind_port = 6000

后台启动 frpc

[root@Rocky-Client ~]# cd /usr/local/frp/
[root@Rocky-Client frp]# ./frpc -c ./frpc.ini &>/dev/null &

查看端口监听情况

[root@Rocky-Client frp]# netstat -nltp | grep frp
tcp        0      0 127.0.0.1:6000          0.0.0.0:*               LISTEN      187638/./frpc

测试

在要访问内网服务的机器上通过 SSH 访问内网机器

[root@Rocky-Client frp]# ssh -p 6000 root@127.0.0.1
The authenticity of host '[127.0.0.1]:6000 ([127.0.0.1]:6000)' can't be established.
ECDSA key fingerprint is SHA256:qza7Pkio11QBgHETtSCpoREjwDRjYy/xe7Pd/OJLlRI.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[127.0.0.1]:6000' (ECDSA) to the list of known hosts.
root@127.0.0.1's password:
Activate the web console with: systemctl enable --now cockpit.socket

This system is not registered to Red Hat Insights. See https://cloud.redhat.com/
To register this system, run: insights-client --register

Last login: Wed Nov  2 03:02:27 2022 from 192.168.26.1
[root@Rocky-Server ~]#

看到内网机器的主机名就说明成功登录了。

投诉或建议