6

I have a command to forward a port from my computer to a server, as follows:

ssh -L 8000:localhost:8888 myserver.com

I would like to run this command in background. I don't need to enter user and password since I already setup a public key.

I tried adding & at the end, as follows: ssh -L 8000:localhost:8888 myserver.com& But, I got the following error:

[1]  + 30825 suspended (tty output)

I tried also nohup , as follows:

nohup ssh -L 8000:localhost:8888 myserver.com&
exit

but the port is not forwarding.

Finally, I tried ssh -f , as follows:

ssh -L 8000:localhost:8888 myserver.com

and, I got the following error:

Cannot fork into background without a command to execute.

My goal is to keep the ssh connection active in background without keeping the terminal open. Any help?

1 Answer 1

10

You mention ssh -f, which is correct, but you missed -N, which is the remaining piece of the puzzle:

-N Do not execute a remote command. This is useful for just forwarding ports. [...]

So close! Try this

ssh -fN -L 8000:localhost:8888 myserver.com
3
  • @roaima do i need to care about a timeout or is this only for sshfs? The process works infinite till i kill /suspend/reboot the background process?
    – Z0OM
    Jul 15, 2022 at 9:48
  • 3
    How can I stop this tunnel?
    – Tobia
    Oct 12, 2022 at 16:24
  • @Tobia kill the process (either explicitly with kill or implicitly by logging out) Oct 12, 2022 at 16:34

You must log in to answer this question.

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