SSH Escape Characters on Ubuntu Linux Explained

This article explains the purpose and utility of SSH escape characters on an Ubuntu Linux client. Readers will learn how these special keystrokes allow users to interact directly with their local SSH client to manage frozen connections, configure port forwarding on the fly, and pause sessions without disconnecting.

What is the SSH Escape Character?

On an Ubuntu Linux client, the SSH escape character is a special keyboard trigger—by default, the tilde key (~)—that tells the local SSH client to intercept the subsequent keystroke instead of sending it to the remote server.

Normally, every key you type during an SSH session is transmitted directly to the remote machine. However, when the escape character is invoked properly, the local SSH client pauses transmission to the remote host and executes a local command instead.

How to Use the Escape Character

To trigger an escape command, the escape character must be typed immediately after a newline. The SSH client will not recognize the escape sequence if it is typed in the middle of a line of text.

The standard sequence to initiate an escape command is: 1. Press Enter (to start a new line). 2. Press the ~ (tilde) key. 3. Press the specific command key (e.g., ., ^Z, or C).

Essential SSH Escape Commands

Once the escape character sequence is initiated, several commands can be executed. Below are the most common and useful escape commands available on Ubuntu:

1. Terminate a Frozen Session (~.)

If a remote server crashes, drops its network connection, or runs a process that hangs the terminal, the session can become completely unresponsive. Pressing Enter, then ~, then . (period) will immediately terminate the connection and return you to your local Ubuntu command prompt.

2. Suspend the SSH Session (~^Z)

If you need to run a quick command on your local Ubuntu machine without closing your remote session, press Enter, then ~, followed by Ctrl + Z. This suspends the SSH client and puts it in the background. To resume the session, type fg in your local terminal and press Enter.

3. Open the SSH Command Line (~C)

Pressing Enter, then ~, then Shift + C opens an interactive SSH command line (ssh>). This command line allows you to modify the current connection dynamically. The most common use case is adding port forwards on the fly without restarting the connection, using syntax like:

ssh> -L 8080:localhost:80

4. List All Available Options (~?)

If you forget the available escape commands, pressing Enter, then ~, then ? will print a help menu directly in your terminal listing all supported escape sequences.

Customizing or Disabling the Escape Character

If you frequently type tildes at the start of lines or use applications that conflict with the default escape character, you can change or disable it.

To disable the escape character for a single session, use the -e flag with none:

ssh -e none user@remote_host

To permanently change the escape character (for example, to a Ctrl-character or a different symbol), add the EscapeChar directive to your local SSH configuration file (~/.ssh/config):

Host *
    EscapeChar ~

Replacing ~ with your preferred character or none will apply the configuration to all future SSH connections.