I am trying to forward my port 8085 which is a live camera stream port for a mini http server, to a remote server.

My command which I am using is as so:

ssh -R $rport:dc-bb7925e7:$camport -p 25 server@

As this command is right now, it does forward the port and I can see the live stream on the remote server by going to localhost:8085, BUT the problem is that on the client, there is a TTY session open which prevents all further scripts from running.

So I tried using ssh in the background with:

ssh -Nf -R $rport:dc-bb7925e7:$camport -p 25 server@

This does not work as it seems as if the connection is closed. The port forwarding is being used in a script where the script assesses an if/then/else condition and then executes to forward. Forwarding this port is not suposed to halt all other scripts. It should simply forward the port and then move on, while keeping it open.

What am I doing wrong or are there any other flags that can be used?

  • The flags you are using should work. The -N makes the ssh non-interactive by not running any command on the remote host and the -f causes the ssh command to run in the background which should allow your script to continue running. I have tested on my local machine and they work as expected. Are you sure there is no other error when running hte script? – Aner Oct 17 '14 at 21:55

Rather than backgrounding an active TTY, you can just let SSH take care of things by itself.

My suggestion is this:

ssh -fNT -R $rport:dc-bb7925e7:$camport -p 25 server@

The first three options are:

  -f    Requests ssh to go to background just before command execution.
  -N    Do not execute a remote command.
  -T    Disable pseudo-tty allocation.

The -f option is the one that actually backgrounds things, but -N and -T will save resources which you don't need to allocate for an SSH session whose sole purpose is to carry your tunnel.

Also note that some of these options can be added to a custom profile in your ~/.ssh/config file, in case you feel it would be preferable to put more of your static configuration into static configuration files rather than scripts. The RemoteForward config file option is equivalent to the -R command line option.

See also:

  • the -fNT does not work. Have no idea why, but what happens is that once executed, the camera no longer streams to its original port and the remote server can also not view at the forwarded port. – RootWannaBe Oct 18 '14 at 16:08
  • 1
    I just found out that for whatever reason, using the flags above is actually closing the connection. I used a killall ssh command which responded with "No Processes Found" – RootWannaBe Oct 18 '14 at 16:24
  • 2
    The -fNT options do work in general, I use them every day. I don't know enough about your particular setup to be able to explain why your tunnels might be getting closed after the ssh process backgrounds itself. The debug output you describe does not appear to be an error. If you want to update your question with the detailed results of your experiment, it would help us come up with better answers. – ghoti Oct 20 '14 at 16:42
  • 1
    You could perhaps add your debug output to your question (not in a comment). Maybe ssh isn't even establishing the session? Too many possible things that could be broken, not enough detail to diagnose. – ghoti Oct 22 '14 at 14:29
  • 2
    I had to add -o ControlPersist=yes to prevent the connection from being closed. This is due to my having ControlMaster=auto in my ~/.ssh/config – Jon Nalley Sep 10 '18 at 21:33

This works even on a mac

ssh -o ControlPersist=yes -fNT -L 3306: example.com

You can use & with that and it won't freeze in background, but it will background automatically after login anyways, so don't use &.

-f puts it in the background after login -N disables auto running the remote user shell -T disables the local psudo-tty

-o ControlPersist=yes prevents the connection from being closed when combines with ControlMaster=auto, which happens to be in some user's/install/distros ~/.ssh/config

This would be a way to chain multiple SSH connections and forwards with a one liner or a bash script.


-N isn't background, it's no remote command. You want something like:

ssh -R $rport:dc-bb7925e7:$camport -p 25 server@

Note the trailing ampersand, which will background the command.

You'll likely want to write some logic to go back and close the SSH session when you're done.

  • 2
    I had already tried the &. It does not forward anything – RootWannaBe Oct 18 '14 at 16:12
  • 1
    I think you'll find that the "logic to go back and close the SSH session" is described here. – Graham Oct 27 '18 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.