I'm trying to tunnel my production mongo server to my test server, so, I can perform some tests.

I know I can simply establish a tunnel by ssh -L, but I need to run this as a background process as I cannot have this running in my terminal.

I tried

ssh -f user@myip -L 27017:localhost:27017 -N

I also tried simply running it as a daemon process but it didn't work either.

ssh -L 27017:localhost:27017 myuser@myip

I know pointing to the production database from a different server is bad. But the thing is, I've migrated my assets to S3, and need to test if everything works properly before moving this to live. This is just going to take me half a day to test, so I don't want to go through the trouble of setting up another instance and dump a copy of my DB here.

Please help me with this.

  • Did you try ssh -f user@myip -L 27017:localhost:27017 -N &?
    – MadHatter
    Jul 22, 2014 at 11:35
  • What is the problem with ssh -f user@myip -L 27017:localhost:27017 -N ?
    – piernov
    Jul 22, 2014 at 11:47
  • @MadHatter No. Ideally -f is supposed to keep the process in the background.
    – skmvasu
    Jul 23, 2014 at 5:28
  • @piernov The process keeps stopping abruptly, without notice. The log was empty as well.
    – skmvasu
    Jul 23, 2014 at 5:29
  • You can try things like nohup ssh -f user@myip -L 27017:localhost:27017 -N or nohup ssh user@myip -L 27017:localhost:27017 &.
    – piernov
    Jul 23, 2014 at 13:22

2 Answers 2


You can run your command inside screen and then detatch the screen from the terminal with:

Ctrl a + d

Once you have finished with your tests, just resume the session with screen -r and then exit


One option is to use AutoSSH utility. It is a daemon that keeps SSH sessions open for tunneling, and restarts sessions if they are closed for some reason.

  • AutoSSH works, but it's not running as a daemon. Unless I'm doing something specifically wrong, it keeps killing the process the moment I close my terminal. autossh -M 20000 -N -p 22 user@myip -L 27017/localhost/27017
    – skmvasu
    Jul 23, 2014 at 15:05
  • You need to make additional setup for AutoSSH so that it is started as a daemon via system init scripts. In Debian, there is a configuration system for this. Jul 24, 2014 at 2:39

You must log in to answer this question.

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