2

I'm having trouble connecting to a remote database server.

I'm on a Windows 10 client and can successfully connect to my Ubuntu 16 server with putty over SSH, including with a keypair.

Now I have a MongoDB server running on the server which I want to connect to from my client but I don't want the server exposed more than necessary (it's not meant for public use anyway) to the internet so I want to use my SSH tunnel and forward the correct port trough it. I have no problem using the database server locally from within the server.

When trying to connect to the database from the client however I get connection refused.

On the server, in /var/log/auth.log I get when trying to connect:

sshd[30753]: error: connect_to < public-ip-redacted> port 27017: failed.

The sshd_config on the server has

GatewayPorts yes

AllowAgentForwarding yes

AllowTcpForwarding yes

So I've set up putty on the client with a local port forwarding to the remote.

On the server with tcptrack I can see some connections popping up but resetting on the loopback interface when trying to connect from the client and they disappear after 1-2 seconds.

What am I missing?

4
  • Is the database server listening on the loopback interface? or another? Is address covered in red 127.0.0.1? or other? What if you use 127.0.0.1? Sep 26, 2019 at 17:29
  • The red is the server public ip on all images. I try to connect to 127.0.0.1 on the client and putty forwards it to the server correctly (it shows up on the server) but then the server refuses the connection? Nothing logged in /var/log/mongodb/mongod.log. I can successfully use the database server locally on the server by connecting to localhost on the same port.
    – r .r
    Sep 26, 2019 at 17:45
  • 1
    Well, I think if you managed to connect to 127.0.0.1:27017 on the remote side, then you should have told PuTTY to forward the local port to 127.0.0.1:27017 on the remote side. This way there is no need for MongoDB to listen on other interfaces. Anyway please do not post answers in the question body. You can publish your solution as an answer. Sep 26, 2019 at 18:06
  • @KamilMaciorowski Thanks, this definitely worked even better.
    – r .r
    Sep 27, 2019 at 11:28

1 Answer 1

2

The mongoDB server was only listening to its 127.0.0.1 per its /etc/mongod.conf-file. The putty tunnel port forwarding should have been set to 127.0.0.1:< port> instead of < public-remote-ip>:< port> as, I guess, putty implicitly translates this to the remote's localhost.

2
  • The remote end of the port forwarding is used by the SSH server. It has nothing to do with PuTTY. So you need to set the remote end to an IP address to which the server can connect to. When you are logged in on the server, you probably have to use the 127.0.0.1:<port> as well, right? Sep 27, 2019 at 11:45
  • @MartinPrikryl Good to know! All the tutorials I found seems to have missed that point too. They all showed the remote public ip which I guess works if your server software listens to eth0 but an external firewall blocks incoming requests but except SSH.
    – r .r
    Sep 28, 2019 at 12:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .