Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

How to Prevent SSH Session Timeout on Raspberry Pi

How to Prevent SSH Session Timeout on Raspberry Pi

SSH session timeout on a Raspberry Pi can interrupt your remote work by disconnecting you unexpectedly after a period of inactivity. Preventing this timeout ensures your connection stays alive as long as you need, improving productivity and convenience when managing your Pi remotely. This guide walks you through the necessary configurations to maintain persistent SSH sessions.

What You’ll Need

  • A Raspberry Pi with SSH enabled and accessible over the network
  • Access to the Raspberry Pi’s terminal or SSH client
  • Basic knowledge of using command line and editing text files on Linux

Step-by-Step

  1. Connect to your Raspberry Pi via SSH. Use your preferred SSH client (like Terminal on macOS/Linux or PuTTY on Windows) to log in to your Raspberry Pi.

    Example: ssh pi@your_pi_ip_address
  2. Check current SSH server timeout settings. Run the following command to see if any timeout options are set:

    sudo grep -i clientalive /etc/ssh/sshd_config

    This checks if ClientAliveInterval and ClientAliveCountMax are configured.
  3. Edit the SSH server configuration file. Open the SSH daemon config file using a text editor like nano:

    sudo nano /etc/ssh/sshd_config
  4. Modify or add the following lines to keep the session alive:
    • ClientAliveInterval 60 – This sends a keepalive message every 60 seconds.
    • ClientAliveCountMax 3 – Allows the server to send 3 keepalive messages without response before disconnecting.

    If these lines exist, update their values; if not, add them at the end of the file.

  5. Save and close the file. In nano, press Ctrl+O to save and Ctrl+X to exit.
  6. Restart the SSH service to apply changes. Run:

    sudo systemctl restart ssh

    This reloads the SSH server with the new settings.
  7. Optionally, configure your SSH client to send keepalive packets. On your local machine, edit your SSH client config file (~/.ssh/config) or specify options when connecting:
    • Add to ~/.ssh/config:

      Host *

      ServerAliveInterval 60

      ServerAliveCountMax 3
    • Or connect using:

      ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 pi@your_pi_ip_address

    This helps keep the client side connection alive.

  8. Test your setup by remaining idle. Leave your SSH session open without activity for several minutes to confirm it no longer times out.
  9. For additional persistence, consider using screen or tmux. These terminal multiplexers keep your session running on the Raspberry Pi even if the SSH connection drops.

    Install with:

    sudo apt-get install screen or sudo apt-get install tmux

    Start a session with screen or tmux before running commands.

Common Mistakes

  • Editing the wrong SSH configuration file: Ensure you modify /etc/ssh/sshd_config on the Raspberry Pi, not the client’s config.
  • Not restarting the SSH service: Changes won’t take effect unless you restart the SSH daemon.
  • Setting intervals too high or too low: Too high intervals could still cause timeouts; too low may flood the network with keepalive packets.
  • Forgetting client-side keepalive settings: Sometimes the client disconnects first; configure both server and client for best results.
  • Ignoring firewall or network timeouts: Some routers or firewalls may terminate idle connections regardless of SSH settings.

Tips

  • Use tmux or screen for session persistence: Even if SSH disconnects, your processes continue running inside tmux or screen sessions.
  • Choose balanced keepalive intervals: 60 seconds is a common standard that balances network traffic and connection stability.
  • Check your router’s idle timeout settings: If your SSH still drops, your router or ISP might be closing idle sessions.
  • Use autossh for automatic reconnection: If your connection drops regularly, autossh can help maintain persistent tunnels.
  • Keep your Raspberry Pi’s software updated: Bugs in SSH or network stack can cause unexpected disconnects.

Troubleshooting

If your SSH session still times out after applying these settings, try the following:

  • Verify the SSH daemon configuration: Run sudo sshd -t to test for syntax errors in sshd_config.
  • Check SSH service status: Use sudo systemctl status ssh to confirm it’s running without errors.
  • Confirm your keepalive settings: Re-check /etc/ssh/sshd_config and your client config files for correct values.
  • Look for network interruptions: Use tools like ping or mtr to test network stability between your client and the Pi.
  • Check router/firewall logs: Some devices drop idle connections aggressively; adjust their settings if possible.
  • Try connecting from a different client or network: This helps identify if the issue is client-side or network-related.
  • Use verbose SSH output: Connect with ssh -vvv pi@your_pi_ip_address to see detailed connection logs.

Frequently Asked Questions

Why does my SSH session timeout even after setting ClientAliveInterval?
Some routers or firewalls may terminate idle connections regardless of SSH keepalive settings. Also, if the client doesn’t respond, the server will disconnect.
Can I disable SSH session timeout completely?
While you can set very high keepalive intervals and counts, it’s not recommended to disable timeouts entirely for security reasons.
What is the difference between ClientAliveInterval and ServerAliveInterval?
ClientAliveInterval is a server-side setting sending keepalive packets to the client, while ServerAliveInterval is client-side, sending packets to the server.
Is it safe to lower these intervals to prevent timeout?
Yes, but setting intervals too low can increase network traffic and may look suspicious on some networks. 60 seconds is a good balance.
How does using tmux or screen help with SSH timeouts?
tmux and screen keep your session running on the Raspberry Pi independently of your SSH connection, so you can reconnect without losing your work.
What if I can’t edit sshd_config due to permissions?
You need root or sudo privileges to edit /etc/ssh/sshd_config. Use sudo before your editor command to gain the necessary access.

By following these steps and tips, you can effectively prevent SSH session timeouts on your Raspberry Pi, ensuring a more reliable and productive remote management experience.

Related

How to Prevent SSH Session Timeout on Raspberry Pi
Scroll to top