I used ssh -L 10002:192.168.0.30:10002 192.168.1.135 to establish port forwarding but now I need to remove it.
How do I do this?
I used ssh -L 10002:192.168.0.30:10002 192.168.1.135 to establish port forwarding but now I need to remove it.
How do I do this?
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>
kill -9 until after you've tried just kill. Many processes will have signal handlers which will clean up their use of resources, cleanly close connections and other pre-shutdown tasks. If you kill with -9, the process dies immediately without doing the cleanup. Killing without -9 will work most of the time.
– Doug Harris
Dec 23 '09 at 18:44
kill -9 without reason is like using a shotgun to kill a mosquito. :)
– Darren Hall
Dec 23 '09 at 21:24
pgrep ssh | xargs kill. Don't use -9 for nothing indeed
– GabLeRoux
Jan 08 '16 at 22:35
ssh command, or that all the ssh commands you are running are fine to kill. This is hardly a good general assumption.
– tripleee
Apr 14 '16 at 09:11
netstat -peanut, last column will be PID/Program name, grep the port you are looking for and you'll be way closer to the solution
– GabLeRoux
Apr 14 '16 at 12:44
ssh instances on multiple remote servers all the time, some of them without my direct active involvement. For example, Emacs Tramp mode opens an ssh connection behind the scenes when I visit a remote buffer. Some people use userspace filesystems which do something similar. It's not at all uncommon. In fact, I would assume single user, single ssh instance to be a minority fringe use case. If it works for you, good for you, but it's not good general advice.
– tripleee
Oct 23 '16 at 09:16
pgrep -f 'the exact comand that you want to kill' | xargs kill
– onlycparra
Jun 14 '21 at 07:48
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.
ssh -O exit 192.168.1.135.
– Tomilov Anatoliy
Sep 28 '17 at 09:27
How to cancel a forwarded port in an already running SSH session:
-KL 10002 (or whatever port number)You should see this:
ssh> -KL 10002
Canceled forwarding.
~C does not work, try launching cat on the remote shell and, while it's running, type the shortcut. Related: https://superuser.com/a/1192862/570332 (SSH escape key ("~") only works when connection is stuck?...)
– Artfaith
Apr 21 '23 at 14:56
You can enter an interactive console by typing ~C (capital "C"). This lets you dynamically add and remove port forwardings (among a few other things).
This sequence has to come right after a carriage return/newline. So in doubt, just type Enter~C (in sequence).
If you don't see the characters appear on the console, you're doing it right :)
You should now see an ssh> prompt.
To remove the port, simply enter -KL 10002 followed by Enter (where 10002 is your forwarded port).
The inverse - adding a new forward - can be done like this (from start to finish):
Enter~C
ssh> -L 10002:192.168.0.30:10002
Enter
~C escape not available to multiplexed sessions. If that's the case, see a3nm's excellent answer.
– Alan De Smet
Jul 16 '18 at 21:20
You could use the "escape-key" (usually ~) followed by C to get a cli to your connection. You can from there remove tunnels without taking down your connection.
killall ssh is also a neat variant which is easy to remeber and convenient if nothing except SSH port forwarding is enabled. Note it will stop all opened SSH connections also.
Warning: remote port forwarding failed for listen portmessage. – GabLeRoux Jan 08 '16 at 22:38