19

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 [email protected]

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 [email protected]

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?

1
  • 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, 2014 at 21:55

3 Answers 3

35

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 [email protected]

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:

9
  • 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. Oct 18, 2014 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" Oct 18, 2014 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, 2014 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, 2014 at 14:29
  • 3
    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, 2018 at 21:33
4

This works even on a mac

ssh -o ControlPersist=yes -fNT -L 3306:127.0.0.1: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.

1

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

ssh -R $rport:dc-bb7925e7:$camport -p 25 [email protected]&

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
  • 2
    I had already tried the &. It does not forward anything Oct 18, 2014 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, 2018 at 1:54

You must log in to answer this question.

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