Ubuntu – How to get terminal back after I suspend a computer over SSH

command linessh

I very often SSH into a computer just to send it to sleep. The problem is when it sleeps, it holds the SSH connection open (seemingly indefinitely). Here's what happens:

oli@bert:~$ ssh tank
oli@tank:~$ sudo pm-suspend
[sudo] password for oli:

I put my password in, hit Return and then it hangs… Until I wake the remote machine.

Is there any way to force a disconnect locally so I can get my terminal back without having to close it and start another?

Best Answer

Enter SSH escape codes! From man ssh:

ESCAPE CHARACTERS
 When a pseudo-terminal has been requested, ssh supports a number of
 functions through the use of an escape character.

 A single tilde character can be sent as ~~ or by following the tilde by a
 character other than those described below.  The escape character must
 always follow a newline to be interpreted as special.  The escape
 character can be changed in configuration files using the EscapeChar
 configuration directive or on the command line by the -e option.

 The supported escapes (assuming the default ‘~’) are:

 ~.      Disconnect.

 ~^Z     Background ssh.

 ~&      Background ssh at logout when waiting for forwarded connection /
         X11 sessions to terminate.

So: press Enter~..

Related Question