Quitting an SSH session is usually exit or Ctrl-D - the normal shell exit commands. When the connection itself is hung (not the shell), the SSH escape sequence ~. (tilde followed by period, on its own line) forces a disconnect. The team that knows the difference between a hung shell and a hung connection resolves the right problem with the right key.
Table of contents
- The normal quit: exit
- The escape sequence: ~.
- When the shell is hung vs the connection is hung
- Killing the SSH client process
- Server-side: killing the session
- Persistent sessions with tmux or screen
- Auto-reconnect with autossh
- FAQ
The normal quit: exit
The most common way to end an SSH session is the same way you end any shell session:
exit
Or press Ctrl-D (the EOF character). Both close the shell, which closes the SSH session, which terminates the connection.
The team that types logout (the csh/tcsh version) also works on bash/zsh. The team that types quit is wrong - that is not a shell command.
The escape sequence: ~.
When the SSH connection itself is hung (no response to exit, no Ctrl-D), the SSH client has an escape sequence that forces a disconnect:
<Enter> ~.
That is: press Enter (to start a fresh line), then tilde, then period. The client sees the ~. and terminates the connection without asking the server.
The team that does not know about ~. resorts to closing the terminal window, which works but is messier. The team that knows ~. disconnects cleanly even when the server is unresponsive.
Other useful escape sequences:
~.- disconnect.~^Z- background the SSH client (Ctrl-Z while in escape mode).~C- open command-line (for adding port forwards).~?- list all escape sequences.
When the shell is hung vs the connection is hung
The distinction matters:
- Shell hung: shell prompt visible but does not respond to input. Usually Ctrl-C cancels the running command. Or open a new shell session in a new terminal window.
- Connection hung: nothing visible at all, or a frozen prompt.
~.is the right answer.
The team that presses Ctrl-C on a hung connection (no shell prompt to interrupt) sees nothing happen. The team that presses ~. on a working shell exits the SSH session immediately. The right key depends on the failure mode.
Killing the SSH client process
If ~. does not work (rare - the client is in a really bad state), kill the process:
# Find the SSH process
ps aux | grep ssh
# Output: you 12345 0.0 0.1 ... ssh user@server
# Kill it
kill 12345
# or
kill -9 12345 # if the polite kill does not work
The team that uses Ctrl-Z to suspend the SSH client can kill %1 after. The team that closes the terminal window sends SIGHUP to the SSH client, which terminates it cleanly.
Server-side: killing the session
On the server, the team can kill an active SSH session:
# List active sessions
who
# or
w
# Find the SSH process
ps aux | grep sshd
# Output: sshd: deploy@pts/0
# Kill it
sudo kill <pid>
The team that has many active sessions sometimes needs to kill the right one. The who output shows which terminal (pts/0, pts/1) is which user.
Persistent sessions with tmux or screen
The team that runs long-running commands over SSH should use tmux or screen:
# Start a tmux session
tmux new -s mysession
# ... do work ...
# Detach: Ctrl-B then D
# Reconnect later
tmux attach -t mysession
That way, when the SSH session ends (intentionally or via network drop), the work continues. The team that does python server.py directly over SSH and gets disconnected loses the running process. The team that runs the process inside tmux survives disconnects.
~. is still the right answer for the immediate disconnect - the difference is the work continues on the server.
Auto-reconnect with autossh
The team that has flaky connections to a remote server uses autossh:
autossh -M 20000 -f -N user@server
autossh monitors the SSH connection and reconnects automatically when it drops. The team that uses this for tunnels keeps them up across network blips.
FAQ
What does Ctrl-D do in SSH?
Sends EOF to the shell, which exits. Same as typing exit. The team that uses Ctrl-D has one less keystroke per disconnect.
How do I quit an SSH session that is frozen?
Press Enter, then ~ (tilde), then . (period). The ~. escape sequence terminates the connection without asking the server. The team that does this knows about SSH escape sequences - most teams do not.
Why does my session stay open after I exit?
Either the SSH client has the connection in a weird state, or the server is keeping the process alive. ~. disconnects the client side. Killing the process on the server is the other half of a clean disconnect.
Can I have multiple SSH sessions to the same server?
Yes. Each terminal window is a separate SSH session. The team that uses tmux or screen multiplexes multiple sessions inside one SSH connection - useful when bandwidth is limited or the server caps concurrent connections.
Does the SSH connection drop when my laptop sleeps?
Usually yes. The TCP connection times out after a few minutes of no response. The team that uses ServerAliveInterval 60 and ServerAliveCountMax 3 in ~/.ssh/config keeps idle connections alive across short sleeps.
If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.
Useful related references: