Cancel ssh port forward

8,267

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 [email protected] && 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).

Share:
8,267

Related videos on Youtube

Paul
Author by

Paul

Updated on September 18, 2022

Comments

  • Paul
    Paul over 1 year

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

    ssh -fND port [email protected]

    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
    Paul about 13 years
    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
  • Robin Green
    Robin Green about 13 years
    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?
  • Paul
    Paul about 13 years
    Just the standard gnome terminal - freshly installed today.
  • Robin Green
    Robin Green about 13 years
    Looks like this bug was fixed back in Jaunty Jackalope. Which version of Ubuntu are you using?
  • Paul
    Paul about 13 years
    Downloaded it just the other day. It's lucid lynx : 10.04 LTS
  • Robin Green
    Robin Green about 13 years
    Is this page of any use? 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")
  • Paul
    Paul about 13 years
    Unfortunately it doesn't help. I didn't use & and I didn't exit, I closed the window.
  • Paul
    Paul about 13 years
    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.