Rootop 服务器运维与web架构

ssh反向连接(ssh隧道端口转发)

当一台linux服务器处于内网状态,没有权限映射到外网时,就需要ssh反向连接功能实现控制内网机器。
反向连接就是在公网有一台机器,通过内网机器连接到外网服务器,在外网服务器上连接这台内网。

内网机器IP:192.168.0.193
外网机器IP:42.62.73.33


内网:
执行下面命令
[root@localhost ~]# ssh -f -N -R 10000:*:22 root@42.62.73.33
root@42.62.73.33’s password:
会提示接收密钥,及输入公网机器密码。

命令意思就是在公网服务器上开一台10000端口,监听所有网络接口。

外网:
[root@1 ~]# netstat -tnlp | grep 10000
tcp 0 0 127.0.0.1:10000 0.0.0.0:* LISTEN 3411/sshd
tcp 0 0 ::1:10000 :::* LISTEN 3411/sshd

可以看到sshd监听在10000端口上。内网机器连接公网成功。

连接内网:
[root@1 ~]# ssh root@127.0.0.1 -p 10000 #公网服务器执行
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
4e:75:a4:b4:15:14:52:a5:33:e3:be:cb:84:4a:34:e6.
Please contact your system administrator.
Add correct host key in /root/.ssh/known_hosts to get rid of this message.
Offending key in /root/.ssh/known_hosts:1
RSA host key for [127.0.0.1]:10000 has changed and you have requested strict checking.
Host key verification failed.
出现这个错误,表明之前已经连接过其它机器,需要删除保存在本地的密钥。

[root@1 ~]# rm -rf ~/.ssh/known_hosts

重试:
[root@1 ~]# ssh root@127.0.0.1 -p 10000
The authenticity of host ‘[127.0.0.1]:10000 ([127.0.0.1]:10000)’ can’t be established.
RSA key fingerprint is 4e:75:a4:b4:15:14:52:a5:33:e3:be:cb:84:4a:34:e6.
Are you sure you want to continue connecting (yes/no)? yes  #接受密钥
Warning: Permanently added ‘[127.0.0.1]:10000’ (RSA) to the list of known hosts.
root@127.0.0.1’s password: #输入内网服务器密码
Last login: Wed May 27 14:21:17 2015 from 192.168.0.16
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:2e:59:9b brd ff:ff:ff:ff:ff:ff
inet 192.168.0.193/24 brd 192.168.0.255 scope global eth0
inet6 fe80::20c:29ff:fe2e:599b/64 scope link
valid_lft forever preferred_lft forever
连接成功。

参数解释:
-f Fork into background after authentication.
后台认证用户/密码,通常和-N连用,不用登录到远程主机。
-L port:host:hostport
将本地机(客户机)的某个端口转发到远端指定机器的指定端口. 工作原理是这样的, 本地机器上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转发出去, 同时远程主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有 root 才能转发特权端口. IPv6 地址用另一种格式说明: port/host/hostport
-R port:host:hostport
将远程主机(服务器)的某个端口转发到本地端指定机器的指定端口. 工作原理是这样的, 远程主机上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转向出去, 同时本地主机和 host 的 hostport 端口建立连接. 可以在配置文件中指定端口的转发. 只有用 root 登录远程主机才能转发特权端口. IPv6 地址用另一种格式说明: port/host/hostport
-D port
指定一个本地机器 “动态的’’ 应用程序端口转发. 工作原理是这样的, 本地机器上分配了一个 socket 侦听 port 端口, 一旦这个端口上有了连接, 该连接就经过安全通道转发出去, 根据应用程序的协议可以判断出远程主机将和哪里连接. 目前支持 SOCKS4 协议, 将充当 SOCKS4 服务器. 只有 root 才能转发特权端口. 可以在配置文件中指定动态端口的转发.
-C Enable compression.
压缩数据传输。
-N Do not execute a shell or command.
不执行脚本或命令,通常与-f连用。
-g Allow remote hosts to connect to forwarded ports.
在-L/-R/-D参数中,允许远程主机连接到建立的转发的端口,如果不加这个参数,只允许本地主机建立连接。注:这个参数在实践中似乎不起作用。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/3423.html

作者:Venus

服务器运维与性能优化

评论已关闭。