Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

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

share|improve this question
Not directly what you are asking but how about just pressing ctrl+d multiple times? – Ulrich Dangel Jun 26 '12 at 17:04
@UlrichDangel I have never used that before, but also a quick way to back out of connections quickly. – Banjer Jun 26 '12 at 17:42
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 '12 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 '12 at 20:05
up vote 17 down vote accepted

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

Just press CtrlD and it will log you out there for hold ctrl and press "d" till the window disappears.

share|improve this answer
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 '12 at 12:49
What does Ctrl + D does, i mean is it safely exists or just kills the process ? – AMB Aug 6 '15 at 2:23
It safely exits. It is a shell shortcut that is the same as typing 'exit' and pressing enter. – Joe Aug 7 '15 at 12:54

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

share|improve this answer

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.

share|improve this answer
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 '12 at 22:15

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

Script: myssh.sh

ssh $1
if [ $? -eq 5 ]; then
 exit 5

Call via the dot command:

$ . myssh user@server.com

If you want to exit one level:

$ exit

If you want to exit all:

$ exit 5
share|improve this answer
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 '14 at 19:08

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|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.