When a network connection stalls, the remote shell may stop responding even though the SSH client is still running. In a Linux or macOS terminal, OpenSSH can disconnect the stuck SSH session and return you to your local prompt.

OpenSSH reads the escape keystrokes on your computer, so the remote shell can stay silent. The session needs a pseudo-terminal, which OpenSSH normally requests for an interactive login from a terminal.

Disconnecting can interrupt foreground programs and lose unsaved edits on the remote host. Pressing Enter can also submit a command already on the input line if the connection recovers.

Steps to disconnect a stuck SSH session:

  1. Press Enter in the stuck SSH terminal to start a fresh input line.

    OpenSSH looks for the escape character immediately after a newline, even when the remote shell shows no new prompt.

  2. Disconnect the SSH session by typing ~. at the start of that line.

    The tilde and period are two keystrokes. OpenSSH closes the session as soon as it reads the period, without another Enter. A custom EscapeChar replaces the tilde, and setting it to none disables the shortcut.
    Related: Show SSH client configuration

  3. Check for a remaining SSH process from the recovered local prompt if the stuck session was the only SSH client to that destination in this terminal.
    $ ps -ww -t "$(tty)" -o pid,args

    OpenSSH typically prints Connection to host.example.net closed. for your host before the local prompt returns. Quiet mode (ssh -q) omits the message. A list with no ssh row for your destination confirms the disconnect. With several identical SSH clients in the same terminal, the listing cannot tell them apart, so the recovered local prompt is the signal.

Share this guide