Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
We often realize port is still being forwarded when we fire a new ssh session running same command again and getting Warning: remote port forwarding failed for listen port message. – GabLeRoux Jan 8 at 22:38
up vote 20 down vote accepted

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>
share|improve this answer
22  
No. No. No. Please, please, please do not use 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
9  
kill -9 without reason is like using a shotgun to kill a mosquito. :) – Darren Hall Dec 23 '09 at 21:24
    
I usually do a one liner pgrep ssh | xargs kill. Don't use -9 for nothing indeed – GabLeRoux Jan 8 at 22:35
    
@GabLeRoux That assumes you only have a single 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 at 9:11
    
I agree, knowing exactly what you're doing is way better :) When you use port forwarding at the same time, a good way to find out which pid it is is to run 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 at 12:44

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.

share|improve this answer
    
I'd like to know the specifics of this. I know you can add tunnels after you've SSH'd in, but have yet to find out how to take one down. – carestad Nov 24 '14 at 22:40
3  
When you are in CLI mode you could do help. -KL is the oppsite of -L, -KR is the oppsite of -R and -KD is the oppsite of -D. Doing "escape-key" (~) followed by # shows your tunnels. – Jimmy Hedman Nov 28 '14 at 11:02
    
@JimmyHedman you might want to edit your answer additionally to adding a comment. It makes everything more readable. And sometimes comments are hidden (in case there are too many). – exhuma Feb 19 at 15:51

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

share|improve this answer

You must log in to answer this question.

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