How do I remove an SSH forwarded port

If you are using Linux you can kill the process by:

ps aux | grep ssh

and then use

kill <id>

To kill the process.

If the kill command is not successfull you can try

kill -9 <id>

When using ssh multiplexing, killing the ssh process is often undesirable (it kills all open connections with that host), and you cannot easily access the escape because "escape not available to multiplexed sessions". The right way is then to run the analogue of the forwarding command that you want to cancel, but adding -O cancel. For instance:

ssh -O cancel -L 10002:192.168.0.30:10002 192.168.1.135

This will disable this port forwarding without terminating the session. Again, this will only work if ssh multiplexing is in use for the connection to 192.168.1.135.


How to cancel a forwarded port in an already running SSH session:

  1. Press ~+C (tilde + capital C)
  2. Type -KL port
  3. Press Enter

You should see this:

ssh> -KL 10002
Canceled forwarding.