How to Properly Close PuTTY Sessions
Learn safe methods for terminating SSH sessions and preventing data loss
Why Proper Session Termination Matters
Properly closing PuTTY sessions is more important than you might think. Improper termination can lead to:
- Data loss: Unsaved work or interrupted file transfers
- Orphaned processes: Background processes continuing to run unnecessarily
- Resource locks: Files or databases remaining locked
- Security risks: Sessions left open on shared computers
- Connection limits: Server reaching maximum connection limits
Best Practice: Always use the proper exit command to gracefully close your SSH session before closing the PuTTY window.
Methods to Close PuTTY Sessions
Method 1: Using the exit Command (Recommended)
The exit
command is the cleanest and most reliable way to close a PuTTY session:
Steps:
- Type
exit
in the terminal and press Enter - The SSH connection will close gracefully
- The PuTTY window will automatically close
$ exit
Alternative commands that work similarly:
logout
- Works on most Unix/Linux systemsCtrl+D
- Sends an end-of-file signal (same as exit)
Method 2: Manual Window Close
You can close PuTTY by clicking the X button or pressing Alt+F4, but this method:
- Terminates the connection immediately without cleanup
- May leave processes running on the server
- Can cause data loss if operations are in progress
Warning: Only use this method if you're unable to access the terminal (frozen session) or in emergency situations.
Method 3: SSH Escape Sequence
For unresponsive sessions, use the SSH escape sequence to forcefully disconnect:
Steps:
- Press
Enter
to ensure you're on a new line - Type
~.
(tilde followed by period) - The connection will terminate immediately
Note: This only works with OpenSSH. PuTTY uses a different mechanism for special commands.
Method 4: Timeout or Idle Disconnects
SSH sessions can automatically disconnect due to:
Server-Side Timeouts:
- ClientAliveInterval: Server sends keepalive messages
- ClientAliveCountMax: Maximum failed keepalive attempts
- Idle timeout: Session terminates after inactivity period
Client-Side Settings (PuTTY):
- Connection → Seconds between keepalives: Set to 60 or 300
- Enable TCP keepalives: Prevents idle disconnections
Method 5: Using screen or tmux (Best for Long Sessions)
For long-running tasks, use terminal multiplexers that preserve sessions even after disconnection:
Using screen:
# Start a new screen session $ screen # Run your commands... # Detach from screen (keeps running) # Press: Ctrl+A, then D # Later, reattach to the session $ screen -r
Using tmux:
# Start a new tmux session $ tmux new -s mysession # Run your commands... # Detach from tmux (keeps running) # Press: Ctrl+B, then D # Later, reattach to the session $ tmux attach -t mysession
Preventing Data Loss
Before Closing Your Session
- Save all files: Check that editors like vim, nano are closed
- Check running processes: Use
jobs
orps
to view active processes - Complete file transfers: Ensure scp, rsync, or ftp operations are finished
- Commit database transactions: Close any database connections properly
- Stop services gracefully: Use proper shutdown commands for services
Checking for Background Jobs
# Check for background jobs $ jobs # If jobs are running, you'll see output like: [1]+ Running ./script.sh & # Bring job to foreground $ fg %1 # Or kill the job $ kill %1
Handling Unexpected Disconnections
If you lose connection unexpectedly:
- Reconnect quickly: Background processes may still be running
- Check process status: Use
ps aux | grep username
- Resume screen/tmux sessions: Your work will be preserved
- Check for lock files: Remove stale locks if necessary
Auto-Reconnect Settings in PuTTY
Keepalive Configuration
Configure PuTTY to maintain connections and prevent timeouts:
- Open PuTTY Configuration
- Navigate to Connection
- Set "Seconds between keepalives" to 60 (sends keepalive every 60 seconds)
- Enable "Enable TCP keepalives"
- Save your session
Using PuTTY Connection Manager (SuperPuTTY)
Third-party tools like SuperPuTTY offer auto-reconnect features:
- Automatic reconnection on disconnect
- Session monitoring and alerts
- Tabbed interface for multiple sessions
- Connection history and logging
Closing Multiple Sessions
When Using Multiple PuTTY Windows
- Close each session individually using
exit
- Or use a script to close all sessions at once (Windows):
taskkill /IM putty.exe /F
Warning: This force-kills all PuTTY processes without cleanup.
Using mTPuTTY (Multi-Tab PuTTY)
mTPuTTY allows you to manage multiple sessions in one window:
- Close individual tabs with Ctrl+W
- Close all tabs: File → Close All
- Each tab maintains its own SSH connection
Server-Side Session Management
Viewing Active SSH Sessions
# View all logged-in users $ who # View more detailed information $ w # View all SSH connections $ netstat -tnpa | grep 'ESTABLISHED.*sshd'
Terminating Stuck Sessions (Server-Side)
# Find the PID of the stuck session $ ps aux | grep sshd # Kill the specific session (replace PID) $ sudo kill -9 PID # Or disconnect a specific user $ sudo pkill -KILL -u username
Troubleshooting Common Issues
Session Won't Close with exit
Possible causes:
- Background processes preventing logout
- Screen or tmux session attached
- Shell configuration issues
Solutions:
- Check for background jobs:
jobs
- Force logout: Press
Ctrl+D
twice - Detach from screen/tmux:
Ctrl+A D
orCtrl+B D
PuTTY Window Freezes
Try these steps:
- Press
Ctrl+C
to interrupt the current operation - Press
Ctrl+Z
to suspend the process - Try
Ctrl+Q
if output is paused (XON/XOFF flow control) - Right-click and select "Restart Session"
- If all else fails, close the window and reconnect
"Connection Reset by Peer" Error
This error occurs when the server forcefully closes the connection:
- Server reached maximum connection limit
- Network issue or firewall interference
- Server crashed or rebooted
- Session timeout configured on server
Best Practices Summary
- ✅ Always use
exit
orlogout
to close sessions gracefully - ✅ Use screen or tmux for long-running tasks
- ✅ Enable keepalives to prevent idle disconnections
- ✅ Save your work before closing sessions
- ✅ Check for background jobs before logging out
- ✅ Configure proper timeout values on both client and server
- ❌ Avoid force-closing windows unless absolutely necessary
- ❌ Don't leave sessions idle for extended periods on shared systems