宝子们,若文章内容可以为你提供有用的价值,记得关注我哦!我会持续更新行业经验呢!
1、背景
为了满足远程或者公司内网运维服务器,通过ssh反向代理配置实现互联网机器ssh连接内网服务器,为实现方便、高效的运维提供了基础条件。
2、ssh反向代理实践过程
2.1 准备机器
192.168.59.102 内网服务器ip(A 互联网)
114.215.168.43 外网服务器ip (B 内网IP)
通过在A服务器上设置ssh反向代理到B服务器,将A服务器的22端口绑定到B服务器中的20010端口上,实现通过B服务器中的20010端口访问A服务器22端
2.2 设置服务器(B)ssh
vim /etc/ssh/sshd_config
# 修改 GatewayPort参数值为yes
systemctl restart sshd
2.3 设置服务器(A)ssh
设置A服务器至B服务器ssh连接时的免登录
ssh-keygen
cd ~
cd .ssh/
ssh-copy-id -i id_rsa.pub root@114.215.168.43
在A服务器上设置SSH反向代理
autossh -M 45678 -fCNR :20010:localhost:22 root@114.215.168.43
2.4 测试
在A服务器上查看代理状态
[root@node1 .ssh]# ps -ef | grep ssh
liyang 2640 2507 0 6月19 ? 00:00:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
root 9705 1 0 6月19 ? 00:00:00 /usr/sbin/sshd -D
root 49767 9705 0 15:40 ? 00:00:00 sshd: root@pts/1
root 51183 1 0 16:55 ? 00:00:00 autossh -M 45678 -CNR :20010:localhost:22 root@114.215.168.43
root 51195 51183 0 16:56 ? 00:00:00 /usr/bin/ssh -L 45678:127.0.0.1:45678 -R 45678:127.0.0.1:45679 -CNR :20010:localhost:22 root@114.215.168.43
root 51760 49773 0 17:27 pts/1 00:00:00 grep --color=auto ssh
在B服务器上查看端口信息
[root@AY140330180959236303Z ~]# netstat -tlnp | grep 20010
tcp 0 0 0.0.0.0:20010 0.0.0.0:* LISTEN 1420/sshd
tcp 0 0 :::20010 :::* LISTEN 1420/sshd