31

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".

4
  • 7
    Not directly what you are asking but how about just pressing ctrl+d multiple times? Jun 26, 2012 at 17:04
  • 1
    @UlrichDangel I have never used that before, but also a quick way to back out of connections quickly.
    – Banjer
    Jun 26, 2012 at 17:42
  • 4
    Are you really nesting ssh sessions 5 to 10 levels deep? Do you realize that's creating a convoluted tunnel that bounces all packets in your ssh session through all of those hosts? Could you make the problem go away by simply not nesting ssh sessions?
    – jw013
    Nov 7, 2012 at 19:45
  • @jw013 No, I did not realize that, but makes sense. Honestly, 5-10 levels was an exaggeration, but I was trying to illustrate my question of "how to cleanly exit out of Putty in one fell swoop."
    – Banjer
    Nov 7, 2012 at 20:05

7 Answers 7

48

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).
6
  • 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.
    – Banjer
    Jun 26, 2012 at 17:36
  • I like the ctrl+d solution as well, but this answers my question more directly. Thanks all.
    – Banjer
    Jun 26, 2012 at 18:51
  • 4
    @Banjer bash never sees ~., it's handled by ssh itself. Jun 26, 2012 at 22:21
  • @Gilles gotcha, makes sense.
    – Banjer
    Jun 27, 2012 at 20:52
  • 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.
    – Alexios
    Jun 30, 2012 at 13:45
18

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.

5
  • 2
    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.
    – Alexios
    Jun 30, 2012 at 12:49
  • What does Ctrl + D does, i mean is it safely exists or just kills the process ?
    – user45645
    Aug 6, 2015 at 2:23
  • 1
    It safely exits. It is a shell shortcut that is the same as typing 'exit' and pressing enter.
    – Joe
    Aug 7, 2015 at 12:54
  • Actually mine (windows git bash & ubuntu bash) issue a logout.
    – velis
    Dec 21, 2016 at 12:19
  • yes, I believe exit also calls logout as ssh connections tend not to be login sessions.
    – Joe
    Jan 1, 2017 at 1:19
9

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

3

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
1
  • 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.
    – etherfish
    Aug 3, 2014 at 19:08
2

Another way is to make function for ssh:

   function ssh(){ /usr/bin/ssh $@ ; exit ; } 

Unfortunately it also will always close the connection and logout from console after you'll finish work on the remote system.

Note, that you need make such function at all your servers, otherwise this hack won't work. Btw you can always put function into ~/.bashrc or ~/.whatever_shell_you_use_rc .

It looks a little bit dirty hack comparing to uther's way.

1
  • function is not POSIX -- just remove the keyword, and you need to quote $@ properly, or your arguments will be passed after word splitting (bad).
    – Chris Down
    Nov 2, 2012 at 22:15
2

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.

-1

Typing logout in the putty terminal window does it for me:

richard@forevermore:~ > logout

Closes the ssh connection and quits the Putty Application.

2
  • 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. Nov 2, 2017 at 6:58
  • Ok, I see, my bad.
    – Richard
    Nov 3, 2017 at 5:34

You must log in to answer this question.

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