Background
- Three subnets exist in an AZ in AWS. Two of them are private and one is public.
The Public Subnet has a
Jumpboxwhich can be connected to from my local machine viasshusing apemfile (Sample -ssh -i my-key-file.pem ec2-user@host1The First private subnet has an EC2 Instance that acts as a Application Server. It can only be reached from the Jumbox via
ssh. The samepemfile is used here. (Sample -ssh -i my-key-file.pem ec2-user@host2). This command is executed onhost1.- The second private subnet hosts an Oracle Instance using AWS RDS Service. It is running on port
1521. The DB Can only be accessed from the App Server/host2.
How I am working currently
host2hassqlplusclient installed.First,I connect to
host1, then tohost2, and then executesqlplusto execute Queries at the Command line (No GUI).
I am planning to use GUI tools like SQL Developer to connect right from my local machine. I thought using Port forwarding/SSH Tunneling It can be achieved.
I tried using different options, but with no success. The following links are useful:
- https://superuser.com/questions/96489/an-ssh-tunnel-via-multiple-hops
- https://rufflewind.com/2014-03-02/ssh-port-forwarding
My Approach to SSH Tunneling
ssh -N -L 9999:127.0.0.1:1234 ec2-user@host1 -i my-key-file.pem -v -v -v- This is executed on my local machine.
- It does not do much as I can already connect to
host1usingssh. I did not know how to forward many levels. Using this host as my first hop. After this,sshlistens on port9999which isLocalto my machine. It forwards any traffic tohost1to Port1234. My assumption is that, If I use sqlplus on my local machine connecting tolocalhost:9999, the traffic will arrive athost1:1234 - I used
127.0.0.1because the target of SSH tunneling is with respect to the SSH Server, which ishost1. Basically, Both Target, SSH Server are on the same host.
ssh -N -L 1234:db-host:1521 ec2-user@host2 -i my-key-file.pem -v -v- v- This is executed on
host1 After this, ssh forwards any incoming traffic on port
1234to target host (DB Host)/1521 usinghost2as the Tunnel.Again, my assumption is that,
sshis listening on port1234onhost1. Any traffic arriving from anywhere will be delivered toDB Hostusinghost2as the tunnel.
- This is executed on
I executed both commands and did not see any error. I verified which ports are listening using netstat -tulpn | grep LISTEN.
After these two, My plan was to connect to the Database using Hostname as localhost and Port number as 9999.
What's going wrong !
- But when I try to connect to the DB from my local machine, getting an error from my SQL Client Got minus one from a read call. I could not understand the Debug messages from ssh logs.
I believe my understanding of how port forwarding works might not be right. Any inputs would be helpful. Thanks for your time !
