The SSH escape sequence ~. closes a frozen session from the client side, without waiting for the server: press Enter, then ~, then a period. It works because OpenSSH handles the escape character locally at the start of a line, even when the network or the remote host has stopped responding.

How ~. works in SSH: the OpenSSH escape character

OpenSSH uses ~ as its default escape character, and the client only recognises it at the beginning of a line, right after you press Enter. Type ~. in the middle of a command and it goes straight to the server. Type it after a newline and your local ssh intercepts it, so it never touches the network.

That is what makes it useful. When the TCP connection is stuck, Ctrl-C and exit do nothing, because both need to reach the server. ~. doesn't depend on the server at all: it drops the connection from your end and gives you back your local prompt. Escapes are only available when a pseudo-terminal has been requested, which is the normal case for an interactive session. The full list lives in the ssh manual page.

The SSH escape sequences worth memorising: ~., ~?, ~C and ~~

OpenSSH documents these sequences, and every one of them must follow Enter:

  • ~. disconnects immediately.
  • ~? prints the escape sequences your version supports.
  • ~C opens the ssh command line to add or cancel port forwards (-L, -R, -D, -KL, -KR, -KD).
  • ~# lists active forwarded connections.
  • ~^Z backgrounds ssh (Ctrl-Z after the tilde).
  • ~B sends a BREAK to the remote system.
  • ~R requests a rekey of the connection.
  • ~v and ~V raise and lower the verbosity of diagnostic messages.
  • ~~ sends a literal tilde, which is how you reach nested sessions.
# Press Enter, then the sequence
~.                           # close this session
~~.                          # close the nested session (ssh inside ssh)
~?                           # list available sequences
~C                           # open the ssh command line
ssh> -L 8080:localhost:80    # add a local forward on the fly
ssh> -KL 8080                # cancel it

A common setup: you hop from your laptop to a bastion, then on to another server. ~. kills the first connection and everything running through it. ~~. makes the first ssh pass a tilde and a period to the second one, which does the disconnecting. Each extra tilde goes one level deeper.

Recent OpenSSH releases disable ~C by default through the EnableEscapeCommandline option. If ~C does nothing, add EnableEscapeCommandline yes to the relevant Host block. Running local commands with !command from that prompt also requires PermitLocalCommand yes.

When an SSH session is really dead and when it only looks dead

Press Enter and type ~?. If the help appears, your local ssh client is fine and the problem sits in the network or on the server. Bear in mind this is a local check: it says nothing about whether the server is alive, only that your end responds. Before you cut the connection, rule out three fake hangs:

  1. An accidental Ctrl-S. It pauses terminal output through XOFF flow control, and Ctrl-Q resumes it.
  2. A slow foreground command. Try Ctrl-C: if the server answers, the session is alive.
  3. A full-screen program waiting for input. An editor or a pager can look frozen, and Esc or q usually gets you out.

If none of that produces any echo, the session is dead for practical purposes. That is typical after switching from wifi to mobile data, suspending a laptop or rebooting the server. To avoid waiting blindly, ServerAliveInterval makes the client send control messages through the encrypted channel. Once ServerAliveCountMax messages in a row go unanswered (3 by default), ssh disconnects on its own. ServerAliveInterval defaults to 0, which means it is disabled.

Changing or disabling the SSH EscapeChar in ~/.ssh/config

The EscapeChar directive sets the escape character. It takes a single character, ^ followed by a letter for a Ctrl combination, or none. On the command line, the equivalent is -e.

Host *
    ServerAliveInterval 15
    ServerAliveCountMax 3

Host bastion
    HostName bastion.example.com
    EscapeChar ^]

# One connection with no escapes (fully transparent session)
ssh -e none user@server

With none, no escapes are available and the connection is fully transparent. That matters when binary data flows through the channel and might contain a tilde after a newline. A different character makes sense for nested sessions, so each hop gets its own, or when your keyboard layout turns ~ into a dead key. Every directive is described in the ssh_config manual page.

SSH escape sequences versus Mosh: comparison table

Mosh uses a different sequence: Ctrl-^ followed by a period ends the session. These are the differences that matter when a connection hangs:

What is comparedSSH (OpenSSH)Mosh
Sequence to quitEnter, then ~ and a periodCtrl-^ followed by a period
TransportTCP, port 22 by defaultUDP, ports 60000-61000 by default
Switching from wifi to mobile dataThe TCP connection usually hangsThe session stays up
Port forwarding-L, -R and -D, also via ~CNo port forwarding or X11 forwarding
StartupDirect connection to the SSH serverStarts mosh-server over an initial SSH connection

Mosh has no counterpart to ~C because it doesn't forward ports, so if you need a tunnel, open it with a separate ssh connection. The details are in the official Mosh documentation.

Closing frozen SSH sessions from your phone with termi1

On iOS and Android touch keyboards, the tilde usually sits on a secondary symbols page, and the sequence also needs an Enter first. termi1 adds its own keyboard row with Esc, Ctrl, Tab and arrow keys, which makes Ctrl-C, Ctrl-Q and combinations like Mosh's Ctrl-^ easier to type.

On a phone, the most common cause of a frozen session is a network change. termi1 keeps the session alive with Mosh when you move from wifi to mobile data, and uses tmux so whatever you left running is still there when you come back. If a plain SSH connection does hang, close it, reconnect and pick up your work with tmux attach. The termi1 terminal for iOS and Android is free and works without an account.

Frequently asked questions

Why doesn't ~. close my SSH session?

The ~. sequence won't close an SSH session unless it starts a new line, so press Enter before the tilde. It also fails when EscapeChar is set to none, when the escape character has been changed, or when your keyboard treats ~ as a dead key and never sends it.

What is the difference between ~. and exit in SSH?

The difference between ~. and exit in SSH is where each one is processed. exit runs in the remote shell and needs a responsive server. ~. is caught by your local ssh client and drops the connection even when the network or server is gone. In a healthy session, exit is the clean way out.

How do I close a nested SSH session without closing the outer one?

A nested SSH session closes with ~~. after pressing Enter: the first client passes a literal tilde through, and the second client reads ~. as a disconnect. Each additional tilde reaches one level deeper. Alternatively, give each hop its own EscapeChar in ~/.ssh/config.

Do remote processes die when I close SSH with ~.?

Remote processes running in the foreground usually end when ~. cuts the SSH session, because the shell loses its terminal. To keep them running, start them inside tmux and reattach with tmux attach after reconnecting to the server.

What is the Mosh escape sequence?

The Mosh escape sequence is Ctrl-^ followed by a period, and it ends the session even when the server stops responding. Mosh has no ~C command line because it does no port forwarding or X11 forwarding. For tunnels, open a separate SSH connection with -L, -R or -D.