Ssh – How to setup autossh tunnel for MongoDB

mongodbsshssh-tunnel

Here's my setup –

I have two VPS ( linodes ). Linode1 has MongoDB on it. I need to access this MongoDB from Linode2. But, I don't want other users on the private network sneak my data, so I need to use autossh.

I ran autossh -M 5122 -N -R 27017:linode1ip:27017 linode1ip on Linode2 but then I am
prompted to enter password for root@linode1ip.

What I expect is, when I successfully execute (after modification) the above command, I can connect to mongo from Linode2 simply using mongo 127.0.0.1:27017. Am I wrong about this assumption?

Thank you,all.

Best Answer

The second -M in your command isn't required and will (if the command line you have posted is correct) cause you to get a Bad remote forwarding specification 'linode1ip'

The command that you want to setup the tunnel is

autossh -M 20000 -N -p 22 USER@LINODE1IP -L 27017/localhost/27017 (edited)

This will setup an ssh tunnel between localhost:27017 on linode2 and port 27017 on linode1.

If you want to get this setup without having to provide a password you will have to setup public key authentication and ssh-agent.