SSH port forwarding without session
Solution 1:
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:
- More details on how to build tunnels with
-L
and-R
- Tips on using
ControlMaster
to maintain your tunnel
Solution 2:
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.
Solution 3:
-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.