Ssh – unable to connect to forwarded port over ssh

forwardingpostgresqlssh

I have two servers. Server 1 has access to an Oracle instance on a server I have permission to hit but not login to via SSH. I would like to use SSH port forwarding to connect from server 2 to server 1 and from there to the Oracle instance. I have set GatewayPorts=yes and AllowTcpForwarding=yes but I still feel like something is wrong.

For the purposes of this question, I would like to focus simply on connecting to a postgres instance on server 1 (port 5432) from server 2 port forward from 16951.

After running:

ssh -L 16951:localhost:5432 server1 -nNT -f -g

from server 2, I can see "localhost.16951" after running netstat to check open ports which leads me to believe that the port is listening successfully.

When I run:

psql -U jbarton -p 16951

I get a message saying that there is no listening port localhost:16951

    psql: could not connect to server: No such file or directory
     Is the server running locally and accepting
     connections on Unix domain socket "/tmp/.s.PGSQL.16951"?

Any comments, answers or suggestions are greatly appreciated.

Update

When I now try this with oracle, connect using: ./sqlplus W_USER/password@//localhost:16951/dbname, I get this error: ERROR: ORA-12537: TNS:connection closed

My oracle ora files look like this:

`#tnsnames.ora
PROD2 =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 16951))
      (CONNECT_DATA =
         (SERVER = DEDICATED)
         (SERVICE_NAME = prod2)
      )
    )

And the sqlnet.ora

'# sqlnet.ora Network Configuration File Generated by Oracle configuration tools.

NAMES.DIRECTORY_PATH= (TNSNAMES, EZCONNECT)
NAMES.LOG_DIRECTORY = /var/oracle/network/log

Best Answer

If you do not specify the hostname psql assumes domain socket connections. As per the man page:

If you omit the host name, psql will connect via a Unix-domain socket to a server on the local host, or via TCP/IP to localhost on machines that don't have Unix-domain sockets.

Try adding -h localhost to the psql command line.

As to the Oracle error, I would guess because of the following (oracle is much harder to diagnose):

The ORA-12537 error sometimes relates configuration issues in the sqlnet.ora, protocol.ora and listener.ora files.  Verify that you service names match between your listener and remote client connect strings.

So your connect string including localhost is being rejected by oracle.

update

If that is a local tnsnames.ora, have you tried sqlplus W_USER@PROD2 instead? (it will prompt for a password if required). You may have to set the environment variable TNS_ADMIN to the location of where tnsnames.ora resides.

Related Question