[20260724]putty配置tunnel远程连接.txt
--//以前测试过,参考链接:[20210929]putty配置tunnel远程连接.txt
--//理论讲配置ssh tunnel并且勾上 Local ports accept connections from other hosts,remote ports do the same(SSh-2 only).
--//配置好的端口无论是local ports还是remote ports,其他客户端都可以使用,我当时遇到的问题local ports,remote ports无法使
--//用,只能127.0.0.1主机可以使用。
--//今天正好业务需要这项功能,通过一台linux主机完成这些配置,最好配置相互认证不需要输入口令。
$ ssh -N -f -L 0.0.0.0:6666:remote_ip:1521 oracle@remote.example.com
--//这样这台机器的6666端口就可以提供给其他客户端,访问remote_ip:1521。
--//再次唤起探究当时为什么使用putty配置tunnel远程连接,remote ports不行。
--//我的测试环境:
192.168.56.1 客户端(os windows10)
192.168.56.103 数据库服务器(linux) sid=book pdb=book01p 用户口令:scott/book
--//192.168.56.1的 kitty(putty的变体)上配置如下:
--//通过kitty ,选择connection -> SSH -> tunnels,配置如下:
L5555=192.168.56.103:1521
R5556=192.168.56.103:1521
--//并且勾上 Local ports accept connections from other hosts,remote ports do the same(SSh-2 only).
--//这样登录192.168.56.103服务器器后,注意不要退出。
--//在192.168.56.1 的5555端口上建立监听 转发192.168.56.103:1521.
--//在192.168.56.103的5556端口上建立监听 转发192.168.56.103:1521.
--//当时的测试local ports可以,本地remote ports不行。
--//首先测试local ports,在192.168.56.103执行:
$ sqlplus -s -l scott/book@192.168.56.1:5555/book01p <<<@i
USERNAME INST_NAME HOST_NAME I# SID SERIAL# VERSION STARTED SPID OPID CPID SADDR PADDR
-------------------- -------------------- ------------------------- --- ----- -------- ---------- -------- ---------- ----- --------------- ---------------- ----------------
SCOTT BOOK01P-book centos9 1 31 45382 21.0.0.0.0 20260724 5293 28 5291 000000007E740350 000000007E57AFA8
--//说明现在192.168.56.1:5555可以提供给其他客户端访问,通过ssh tunnel实际连接的是192.168.56.103:1521。
--//接着测试local ports,在192.168.56.1执行:
D:\> sqlplus -s -l scott/book@192.168.56.103:5556/book01p @i
ERROR:
ORA-12541: TNS:no listener
SP2-0751: Unable to connect to Oracle. Exiting SQL*Plus
--//在192.168.56.103执行:
# netstat -tnlp| grep 555
tcp 0 0 127.0.0.1:5556 0.0.0.0:* LISTEN 5153/sshd-session:
tcp6 0 0 ::1:5556 :::* LISTEN 5153/sshd-session:
--//可以发现实际上仅仅在127.0.0.1:5556监听。
--//这回仔细看了文档,修改sshd配置GatewayPorts yes,就可以实现该功能,缺省no。
--//在192.168.56.103修改如下:
# grep -i gate /etc/ssh/sshd_config
GatewayPorts yes
# systemctl restart sshd
--//再次重新登录:
# netstat -tnlp| grep 555
tcp 0 0 0.0.0.0:5556 0.0.0.0:* LISTEN 5493/sshd-session:
tcp6 0 0 :::5556 :::* LISTEN 5493/sshd-session:
--//现在的192.168.56.103服务器上监听是0.0.0.0:5556,已经不再是127.0.0.1。
--//在192.168.56.1执行:
d:\>echo @spid | sqlplus -s -l scott/book@192.168.56.103:5556/book01p
==============================
SID : 31
SERIAL# : 64650
PROCESS : 6524:1576
SERVER : DEDICATED
SPID : 5554
PID : 28
P_SERIAL# : 29
KILL_COMMAND : alter system kill session '31,64650' immediate;
PL/SQL procedure successfully completed.
--//这样remote ports配置也可以实现。
--//补充很少使用windows机器作为ssh tunnel,实际上一般仅仅需要支持local port基本足够,再增加一台机器做一个linux下的测试。
--//测试环境:
192.168.56.1 客户端(os windows10)
192.168.56.102 数据库服务器(linux)
192.168.56.103 数据库服务器(linux) sid=book pdb=book01p 用户口令:scott/book
--//在192.168.56.103修改/etc/ssh/sshd_config,GatewayPorts yes,并且重启sshd服务。
--//192.168.56.102执行:
$ ssh -N -f -L 0.0.0.0:6666:192.168.56.103:1521 oracle@192.168.56.103
The authenticity of host '192.168.56.103 (192.168.56.103)' can't be established.
ECDSA key fingerprint is d8:39:7d:6e:28:cc:55:bd:ad:a4:04:d2:3f:cc:2a:ab.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.56.103' (ECDSA) to the list of known hosts.
oracle@192.168.56.103's password:
--//相当于在192.168.56.102打开6666端口。
--//简单说明:
-N Do not execute a remote command. This is useful for just forwarding ports (protocol version 2 only).
-f Requests ssh to go to background just before command execution. This is useful if ssh is going to ask for
passwords or passphrases, but the user wants it in the background. This implies -n. The recommended way to
start X11 programs at a remote site is with something like ssh -f host xterm.
# netstat -tnlp | egrep "666|Active|Proto"
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6666 0.0.0.0:* LISTEN 2876/ssh
--//192.168.56.1 客户端(os windows10)
D:\>echo @spid | sqlplus -s -l scott/book@192.168.56.102:6666/book01p
==============================
SID : 29
SERIAL# : 32499
PROCESS : 6240:5628
SERVER : DEDICATED
SPID : 3135
PID : 8
P_SERIAL# : 8
KILL_COMMAND : alter system kill session '29,32499' immediate;
D:\> echo SELECT SYS_CONTEXT ('userenv', 'ip_address') c20 FROM DUAL;| sqlplus -s -l scott/book@192.168.56.102:6666/book01p
C20
--------------------
192.168.56.103
--//注意这样客户端的IP地址与实际不同。
--//192.168.56.102执行:
$ ssh -N -f -R 6667:192.168.56.103:1521 oracle@192.168.56.103
oracle@192.168.56.103's password:
--//-R表示remote,相当于在远端服务器192.168.56.103打开6667端口。
--//测试环境限制,在实际环境你可以写成其他IP地址。例子:
ssh -N -f -R 6667:remote_ip:1521 oracle@192.168.56.103
--//只要192.168.56.103主机能访问remote_ip:1521,基本没有问题。
--//192.168.56.103执行:
# netstat -tnlp | egrep "666|Active|Proto"
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6667 0.0.0.0:* LISTEN 3149/sshd-session:
tcp6 0 0 :::6667 :::* LISTEN 3149/sshd-session:
--//192.168.56.1 客户端(os windows10)
D:\>echo @spid | sqlplus -s -l scott/book@192.168.56.103:6667/book01p
==============================
SID : 22
SERIAL# : 8827
PROCESS : 1396:5448
SERVER : DEDICATED
SPID : 3168
PID : 8
P_SERIAL# : 11
KILL_COMMAND : alter system kill session '22,8827' immediate;
PL/SQL procedure successfully completed.
D:\> echo SELECT SYS_CONTEXT ('userenv', 'ip_address') c20 FROM DUAL;| sqlplus -s -l scott/book@192.168.56.103:6667/book01p
C20
--------------------
192.168.56.102
--//注意这样客户端的IP地址与实际不同。
--//另外注意这样两个进程实际上在后台运行:
$ ps -ef | egrep 666[67]
oracle 2876 1 0 10:09 ? 00:00:00 ssh -N -f -L 0.0.0.0:6666:192.168.56.103:1521 oracle@192.168.56.103
oracle 3107 1 0 10:16 ? 00:00:00 ssh -N -f -R 6667:192.168.56.103:1521 oracle@192.168.56.103
--//最后收尾:
--//删除2个后台进程。
--//在192.168.56.103修改/etc/ssh/sshd_config,GatewayPorts no,注解它并且重启sshd服务。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-3177983/ ,如需转载,请注明出处,否则将追究法律责任。