centos7 内网穿透教程

时间:2025-03-06 15:35:20

centos7 内网穿透教程

    • 场景介绍
    • 配置端口
    • 配置ssh服务

场景介绍

A:只有内网IP,没有公网IP;但是可以访问公网的服务器
B:有公网IP的服务器
C:可以连互联网的PC
需求,C需要访问A
正常情况下是无法访问到A服务器的,需要拨VPN或者借助工具(向日葵、teamviewer等)
下面我们通过,ssh的隧道,来实现内网穿透

配置端口

B服务器执行

#建立本地转发,将1688端口转发到1699端口上。
ssh -p 22 -fN -g -L 1689:localhost:1688 root@localhost

-f -N 后台运行参数
这两个参数配合,可以在输入登录密码之后使程序在后台运行.
本地端口转发时,默认转发端口只允许本地程序访问,使用-g参数才可以接受远程主机连接.这个参数和-L一起使用时才起作用,和-R一起使用时无用.

A服务器执行

#被远程服务器执行 将本地的22端口发送到远端的1688端口
ssh -p22 -fNR 1688:localhost:22 root@39.106.106.136

配置ssh服务

在SSH服务器的配置中启用GatewayPorts选项

sed -i s/"#GatewayPorts no"/“GatewayPorts yes”/g /etc/ssh/sshd_config

server每隔60秒给客户端发送一次保活信息包给客户端

sed -i s/"#ClientAliveInterval 0"/“ClientAliveInterval 60”/g /etc/ssh/sshd_config

server端发出的请求客户端没有回应的次数达到86400次的时候就断开连接

sed -i s/"#ClientAliveCountMax 3"/“ClientAliveCountMax 86400”/g /etc/ssh/sshd_config