Kill a frozen SSH session with Enter ~. (OpenSSH escape sequences)
Press Enter, then ~ then . to close a hung OpenSSH session at once. Use ~~. for the inner ssh in a nested login, ~? for the list, and EscapeChar to change ~.
When an SSH session stops responding, type these three keys in order:
Enter ~ .
The OpenSSH ssh client closes the connection right away and prints Connection to host.example.com closed. You get your own prompt back without waiting for a timeout. The ~. itself never reaches the remote host: the client reads it.
Why Enter comes first
ssh treats ~ as its escape character only at the start of a line. If you typed anything since the last Enter, the tilde is sent to the host as an ordinary character. Pressing Enter first makes sure the tilde starts a new line. On a frozen connection the Enter does nothing on the remote side, so it is safe.
The escapes work only in a session with a terminal (a pty). A session started with ssh -T, or with a command and no -t, has no escape character, so ~. is passed through like any other text. See SSH "PTY allocation request failed on channel 0" for when a session gets a terminal.
Nested SSH: ~~. closes the inner one
If you log in to one host and run ssh again from there, both clients watch for the tilde. The first ssh (the outer one, nearest your keyboard) catches ~. and drops the whole chain.
To close only the inner session, type the tilde twice: Enter ~ ~ .. The outer client turns ~~ into one literal ~ and sends it on, so the inner client sees ~. at the start of a line and closes. You land back at the shell on the first host. Each extra tilde reaches one hop further in.
A connection through a jump host with ssh -J is one client, not a nested one, so a single ~. closes it. See Jump hosts (ProxyJump).
Other escapes
Type Enter ~ ? for the list your client supports. The common ones:
~.disconnect.~^Z(tilde, then Ctrl+Z) suspendsshand return to the local shell;fgresumes it.~#list forwarded connections.~&putsshin the background at logout while forwarded connections or X11 sessions finish.~Bsend a BREAK to the remote system, if the server supports it.~Rask for a rekey.~Ishow information about the current connection.~vand~Vraise or lower the log level for messages written to the terminal.~~send one literal tilde.~Copen the ssh command line, where you can add or cancel port forwards (-L,-R,-D,-KLand so on).
Since OpenSSH 9.2, ~C prints commandline disabled unless you turn it on with EnableEscapeCommandline yes in ~/.ssh/config or with -o EnableEscapeCommandline=yes.
With connection sharing (ControlMaster), ~. in a shared session closes that session only; the master connection stays up. Some escapes, such as ~^Z, are not available in shared sessions. See SSH ControlMaster: reuse one connection with ControlPath and ControlPersist.
Change or turn off the escape character
Use a different character when ~ is awkward to type, or when an inner program needs to receive a tilde at the start of a line:
ssh -e '^]' [email protected]
Or set it per host in ~/.ssh/config:
Host host.example.com
EscapeChar ^]
The value is a single character, ^ and a second character for a control key (^] is Ctrl+]), or none. -e none turns the escapes off and makes the session transparent for binary data, so ~. no longer works for that connection.
Mosh uses Ctrl+^ instead
Mosh has its own escape. Its default escape character is Ctrl+^ (Ctrl+Shift+6 on a US keyboard), recognized at any time, not only after Enter. Ctrl+^ . closes the Mosh session and Ctrl+^ Ctrl+Z suspends the client. The MOSH_ESCAPE_KEY environment variable changes the character. See How Mosh works and How long a Mosh session lasts.
Stop sessions from hanging in the first place
A session freezes when the network path breaks and neither end notices. ServerAliveInterval in ~/.ssh/config makes ssh check the server and exit on its own when it stops answering. See SSH keep-alives and idle timeouts and SSH "client_loop: send disconnect: Broken pipe". To keep the work on the host when the link drops, run it inside tmux: Keep your work after a disconnect with tmux, and attach automatically.
Related
- SSH keep-alives and idle timeouts
- SSH "client_loop: send disconnect: Broken pipe"
- Why did my session disconnect?
- Jump hosts (ProxyJump)
- SSH ControlMaster: reuse one connection with ControlPath and ControlPersist
- SSH "PTY allocation request failed on channel 0"
- How Mosh works
- How long a Mosh session lasts
- Ctrl+Z and "Stopped": get the program back with fg, bg and jobs
Sources
- ssh(1): ESCAPE CHARACTERS and the -e option
- ssh_config(5): EscapeChar, EnableEscapeCommandline
- OpenSSH 9.2 release notes: ~C command line disabled by default
- OpenSSH source, clientloop.c: escape handling and the closed-connection message
- OpenSSH source, ssh.c: how -e values are read
- mosh(1): ESCAPE SEQUENCES and MOSH_ESCAPE_KEY