exit out of all SSH connections in one command and close PuTTY

194,090

Solution 1

Try using the ssh connection termination escape sequence.

In the ssh session, enter ~. (tilde dot). You won't see the characters when you type them, but the session will terminate immediately.

$ ~.
$ Connection to me.myhost.com closed.  

From man 1 ssh

The supported escapes (assuming the default ‘~’) are:
 ~.      Disconnect.
 ~^Z     Background ssh.
 ~#      List forwarded connections.
 ~&      Background ssh at logout when waiting for forwarded 
         connection / X11 sessions to terminate.
 ~?      Display a list of escape characters.
 ~B      Send a BREAK to the remote system (only useful for SSH protocol
         version 2 and if the peer supports it).
 ~C      Open command line.  Currently this allows the addition of port 
         forwardings using the -L, -R and -D options (see above). It also
         allows the cancellation of existing remote port-forwardings using 
         -KR[bind_address:]port.  !command allows the user to execute a 
         local command if the PermitLocalCommand option is enabled in
         ssh_config(5).  Basic help is available, using the -h option.
 ~R      Request rekeying of the connection (only useful for SSH protocol 
         version 2 and if the peer supports it).

Solution 2

Just press Ctrl + D to exit and it will log you out. Hold Ctrl and press D repeatedly to log you out of multiple windows, tabs, or levels until the window disappears.

Solution 3

Simply close PuTTY. (Alt+F4 by default IIRC.)

Solution 4

If you don't mind doing a little scripting you can do this.

Script: myssh.sh

#!/bin/bash
ssh $1
if [ $? -eq 5 ]; then
 exit 5
fi

Call via the dot command:

$ . myssh [email protected]

If you want to exit one level:

$ exit

If you want to exit all:

$ exit 5

Solution 5

Or you could use exec to replace your shell process with ssh when jumping to another host:

SSH to host1 with PuTTY...
banjer@host1:~> #...doin some work...ooh! need to go check something on host8...
banjer@host1:~> exec ssh host8
banjer@host8:~> #...doin some work...OK time for lunch. lets close putty...
banjer@host8:~> exit
Putty closes.

5 levels deep is not pretty, since the traffic will pass through all the other servers. Because of that I don't recommend just killing PuTTY or ssh (~.), since (depending on what you do) this could result in orphaned processes on the servers.

Better to try and be less "lazy". Right-click on puttys title bar makes opening a new session quick. If you have a "default" server and accept 1 jump from that, the "Duplicate Session" feature is very useful. Especially when using pubkey authentication.

Share:
194,090

Related videos on Youtube

devarni
Author by

devarni

Musician and coder.

Updated on September 18, 2022

Comments

  • devarni
    devarni over 1 year

    Is there a way to back out of all SSH connections and close PuTTY in "one shot"? I work in Windows 7 and use PuTTY to SSH to various Linux hosts.

    An example of the way I find myself working:

    SSH to host1 with PuTTY...
    banjer@host1:~> #...doin some work...ooh! need to go check something on host8...
    banjer@host1:~> ssh host8
    banjer@host8:~> #...doin some work...OK time for lunch. lets close putty...
    banjer@host8:~> exit
    banjer@host1:~> exit
    Putty closes.
    

    Per above, any way to get from host8 to closing PuTTY in one shot? Sometimes I find myself up to 5 or 10 hosts deep. I realize I can click the X to close the PuTTY window, but I like to make sure my SSH connections get closed properly by using the exit command. I also realize I'm asking for tips on how to increase laziness. I'll just write it off as "how can I be more efficient".

  • devarni
    devarni almost 12 years
    Pretty cool! Any way to put ~. into an alias or function? Just to give it an easy to remember name like exitall. Now I'm asking for way too much :). I tried a few things in ~/.bashrc but get -bash: ~.: command not found. I suppose bash sees it as a string and not an escape sequence coming directly from the keyboard.
  • devarni
    devarni almost 12 years
    I like the ctrl+d solution as well, but this answers my question more directly. Thanks all.
  • devarni
    devarni almost 12 years
    @Gilles gotcha, makes sense.
  • Simon Gates
    Simon Gates almost 12 years
    Sadly, this doesn't work universally. Any session running an editor or other full screen tool is likely to ignore EOT. And even bash will ignore it in the middle of a command line. Try it out yourself: type a single letter and try Ctrl-D.
  • Simon Gates
    Simon Gates almost 12 years
    And there's no guarantee that PuTTY also does this. The CLI ssh client needs this technique because it's CLI. PuTTY is a GUI application and there are far more user-friendly ways to do this stuff on a GUI.
  • clerksx
    clerksx over 11 years
    function is not POSIX -- just remove the keyword, and you need to quote $@ properly, or your arguments will be passed after word splitting (bad).
  • user3731606
    user3731606 almost 10 years
    Of course, you would have copy it to every computer that you may ever ssh from. Also, wouldn't it be easier to just define a shell function called ssh? That would avoid the preceeding period, etc.
  • Joe
    Joe almost 9 years
    It safely exits. It is a shell shortcut that is the same as typing 'exit' and pressing enter.
  • velis
    velis over 7 years
    Actually mine (windows git bash & ubuntu bash) issue a logout.
  • Joe
    Joe over 7 years
    yes, I believe exit also calls logout as ssh connections tend not to be login sessions.
  • G-Man Says 'Reinstate Monica'
    G-Man Says 'Reinstate Monica' over 6 years
    I believe that you didn't read the question correctly. OP does ssh host1, and then, from host1, does ssh host8. At that point a logout would do the same thing as the exit command they already know — exit out of host8 and put them back into host1.
  • Richard
    Richard over 6 years
    Ok, I see, my bad.
  • mwfearnley
    mwfearnley about 4 years
    Interesting. I've never heard of these ssh features until today. PuTTY doesn't seem to have them though.