Cancel ssh port forward

4

4

To access restricted university web pages I often run the following command:

ssh -fND port username@uni.address.here

Then I open a firefox profile which I have set to go through port 5555 using a socks proxy. I have been doing this using cygwin ssh for a while and it works fine. Now I'm running it on ubuntu and it works too however it doesn't seem to end when I close the terminal. I currently have no terminals open but I can still browse the web using a socks proxy on port 5555 and access all the restricted pages that I shouldn't be able to. If I change the port number or firefox profile then I can't access those files so the only conclusion I can come to is that the connection is still open which leads to my question: How do I cancel this?

Paul

Posted 2011-04-24T15:17:30.923

Reputation: 145

Answers

4

The ssh process must still be running. If you know for sure that this is the only ssh process running as your user, you can do

killall ssh

as your user. However, it is much safer to find the right process PID and kill that specific process - this avoids killing any other ssh processes that happen to be running. You can either do that with a graphical tool, or:

ps -Afl|grep ssh

this will give you a list of processes - the first number after the username on each line is the PID. Then kill it:

kill PID

where PID is the PID you found above.

Edit by asker:

Instead of killing the process manually whenever I do this I made a shell file that someone else might find useful:

ssh -fND port user@uni.address.here && firefox -P SSH && kill `ps -ef|grep "ssh -fND"|grep -v "grep"|cut -c10-15`

The firefox command opens firefox with the SSH profile, which I had to create (it just has the necessary proxy settings). The second grep on the processes is just to avoid kill the grep process itself (though thinking about it now that doesn't seem necessary: grep will be finished by the time it will be killed).

Robin Green

Posted 2011-04-24T15:17:30.923

Reputation: 1 107

I tried something but just couldn't find the right process, thanks for the help it fixed it. But can you explain why it doesn't stop when I exit the terminal? Is there something I can do to stop this instead of looking up the PID every time? Thanks – Paul – 2011-04-24T15:29:18.043

The terminal must be sending a different kill signal to the ssh process - or even no signal at all! I know how to do the reverse (make processes ignore signals) but this is strange. Which terminal app are you using? Gnome terminal? xterm? – Robin Green – 2011-04-24T15:33:01.783

Just the standard gnome terminal - freshly installed today. – Paul – 2011-04-24T15:35:14.200

Looks like this bug was fixed back in Jaunty Jackalope. Which version of Ubuntu are you using? – Robin Green – 2011-04-24T15:38:23.963

Downloaded it just the other day. It's lucid lynx : 10.04 LTS – Paul – 2011-04-24T15:40:10.157

Is this page of any use? http://ubuntuforums.org/showthread.php?t=876292 (Basically, it seems to say "don't use &" and/or "don't exit your shell, just close the terminal window")

– Robin Green – 2011-04-24T15:46:40.367

Unfortunately it doesn't help. I didn't use & and I didn't exit, I closed the window. – Paul – 2011-04-24T16:20:25.237

Ok I have found a solution which isn't great but works, more or less. I'll add it to the bottom of this answer and accept that so people can see both parts. – Paul – 2011-04-24T17:16:19.807