# SSH Escape Sequences: Reviving a Frozen Terminal

LLMS index: [llms.txt](/en/llms.txt)

---

SSH session froze, `Ctrl+C` does nothing, `Ctrl+D` spits out garbage — familiar situation. Before closing the terminal and losing the session, try built-in escape sequences. They operate at the SSH client level before data reaches the remote host.

## How to invoke escape sequences

The default escape character is tilde (`~`). The combination works only at the beginning of a line. Press Enter, then `~`, then the desired symbol. For example, `~.` terminates the connection.

> [!NOTE]
> If tilde does not work — make sure you pressed Enter before it. In the middle of terminal output the sequence is ignored.

## Escape sequence reference

```bash
ssh> ~?
```

This command prints the list of available escape sequences directly into the terminal.

```
Supported escape sequences:
 ~.  - terminate connection (and any multiplexed sessions)
 ~B  - send a BREAK to the remote system
 ~C  - open a command line
 ~R  - request rekey
 ~V/~v  - decrease/increase verbosity (LogLevel)
 ~^Z  - suspend ssh
 ~#  - list forwarded connections
 ~&  - background ssh (when waiting for connections to terminate)
 ~?  - this message
 ~  - send the escape character by typing ~~
```

You do not need to memorize everything. Keep three scenarios in mind — they cover 90% of problems.

## Emergency disconnect

```bash
# Example: frozen scp or sftp
# Press Enter, then:
~.
```

`~.` closes the SSH connection immediately, including all multiplexed sessions. Works even if the remote host is not responding. Do not confuse it with `~^Z` (suspend) — that leaves the session alive as a background process.

> [!WARNING]
> Forced disconnect does not send SIGHUP to the remote side. If an important process was running without nohup — it will die.

## Breaking the data flow

```bash
# Output of a command is stuck or SCP is slow
~V
```

`~V` (Shift+v) lowers the logging level. The reverse command — `~v` — increases verbosity. In practice useful when you see a stream of meaningless debug messages and want to hide them.

A more radical option — `~B`. Sends BREAK to the remote system. This can interrupt a stuck process that is listening on a serial port. Use deliberately: not all systems handle BREAK correctly.

## Built-in command mode

```bash
# Press Enter, then:
~C
```

You enter the SSH client command line. Here you can manage tunnels on the fly without reconnecting.

```bash
ssh> -L 8080:localhost:80
# Added local port 8080 -> remote:80

ssh> -R 2222:localhost:22
# Added reverse proxy on remote host

ssh> -D 1080
# SOCKS proxy on port 1080

ssh> -KL 8080
# Remove forwarded local port

ssh> help
```

> [!TIP]
> Command mode is convenient when you forgot to forward a port before connecting. No need to reconnect — add it directly from the session.

Format: `-L [bind_addr:]port:host:hostport` and `-R [bind_addr:]port:host:hostport`. Bind address `localhost` restricts forwarding to the local interface only.

## Forced rekey

```bash
~R
```

Forcibly initiates re-keying of the session. Sometimes useful when you notice strange delays or suspect encryption issues. In practice rare, but worth knowing.

## Escape sequence summary

| Sequence | Action |
|---|---|
| `~.` | Emergency disconnect |
| `~V` | Decrease verbosity |
| `~v` | Increase verbosity |
| `~C` | Command line (tunnels) |
| `~R` | Forced rekey |
| `~B` | Send BREAK |
| `~?` | Show help |
| `~^Z` | Suspend ssh |
| `~#` | List forwarded connections |
| `~&` | Background mode (on disconnect) |
| `~~` | Send literal tilde |

## Changing the escape character

If you need tilde in the remote session (for example, connecting to Cisco equipment), change the escape character:

```bash
ssh -e '^Z' user@host
```

Now escape sequences are invoked via `^Z` instead of `~`. Found in specific scenarios — do not touch without need.

> [!NOTE]
> You cannot change the character on the fly in an active session. Only at connection time via the `-e` flag.

## Common pitfalls

1. `~` does not work — forgot to press Enter before it.
2. Session is frozen but you are not sure SSH is still alive — try `~.` blind. It will not make things worse.
3. Multiplexing session (`ssh -M`) is bound to the control socket. `~.` closes all related sessions at once.
4. If you use PuTTY — escape sequences are different. There `~.` sends a literal tilde and dot. Use `Ctrl+]` and then `close`.

## When escape sequences will not help

- Network is physically unavailable — only disconnect.
- Remote process consumed stdin — ssh will receive nothing.
- Problem is at the terminal emulator level — killall ssh, then check tmux/screen.

SSH escape sequences are the first-line tool when a session freezes. Keep `~.` for disconnect and `~C` for tunnels in mind — that is enough for everyday use.
