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:

  1. Type exit in the terminal and press Enter
  2. The SSH connection will close gracefully
  3. The PuTTY window will automatically close
$ exit

Alternative commands that work similarly:

  • logout - Works on most Unix/Linux systems
  • Ctrl+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:

  1. Press Enter to ensure you're on a new line
  2. Type ~. (tilde followed by period)
  3. 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 or ps 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:

  1. Open PuTTY Configuration
  2. Navigate to Connection
  3. Set "Seconds between keepalives" to 60 (sends keepalive every 60 seconds)
  4. Enable "Enable TCP keepalives"
  5. 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

  1. Close each session individually using exit
  2. 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:

  1. Check for background jobs: jobs
  2. Force logout: Press Ctrl+D twice
  3. Detach from screen/tmux: Ctrl+A D or Ctrl+B D

PuTTY Window Freezes

Try these steps:

  1. Press Ctrl+C to interrupt the current operation
  2. Press Ctrl+Z to suspend the process
  3. Try Ctrl+Q if output is paused (XON/XOFF flow control)
  4. Right-click and select "Restart Session"
  5. 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 or logout 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

Related Resources

;