Keep SSH session alive

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Keep SSH session alive

Keeping Your SSH Session Alive: Say Goodbye to Annoying Disconnects! ๐Ÿ˜Š๐Ÿ’ป

Are you tired of your SSH session freezing or disconnecting after a short period of time? ๐Ÿ™„ Don't worry, you're not alone! Many people experience this frustrating issue, especially when accessing remote servers from different locations. But fear not! We've got some easy solutions for you to keep your SSH session alive and maintain uninterrupted work. Let's dive right in! ๐Ÿ’ช๐Ÿš€

The Issue: Terminal Freezes When Connecting from Home ๐Ÿ ๐Ÿ”’

Our friend here is using the command ssh -p8520 username@remote_host to connect to a remote server. While it works perfectly fine at the workplace, the terminal freezes 10-15 minutes after being connected from home. ๐Ÿ˜ž Sounds like a typical scenario, right?

There is no error or timeout report on the console, but the cursor simply refuses to budge. Our frustrated friend tries to check the login users with the command w, only to find some zombie login users lurking around, forcing them to be manually killed. ๐ŸงŸโ€โ™‚๏ธ๐Ÿ’”

Trust us, we understand the annoyance! But fret not, we've got your back. Here are a few simple solutions to keep your SSH session alive and kicking, no matter where you are! ๐Ÿ’กโœจ

Solution 1: Customize the SSH Server Configuration โš™๏ธ๐Ÿ”ง

One way to prevent your SSH session from disconnecting is to tweak the SSH server configuration on the remote machine. Follow these steps:

  1. SSH into the remote server using your usual command.

  2. Open the SSH server configuration file using a text editor. In most cases, it is located at /etc/ssh/sshd_config.

  3. Look for the line ClientAliveInterval and ClientAliveCountMax.

    • ClientAliveInterval controls the time (in seconds) that the server waits for activity from the client.

    • ClientAliveCountMax represents the number of client pings that can be missed before the server terminates the connection.

  4. Increase the values of both parameters to a higher number to allow more time between client pings and to tolerate temporary network issues.

    • For example, set ClientAliveInterval to 120 and ClientAliveCountMax to 3 for a more stable connection.

  5. Save the changes and exit the text editor.

  6. Restart the SSH server on the remote machine to apply the new settings. This can typically be done with the command sudo service ssh restart or sudo systemctl restart ssh.

With these tweaked settings, your SSH session should stay alive for a longer period, giving you uninterrupted productivity. ๐ŸŒŸ๐Ÿ™Œ

Solution 2: Adjust SSH Client Configuration ๐ŸŒโœ๏ธ

If Solution 1 didn't do the trick, worry not! We've got another neat solution for you.

  1. Open the SSH configuration file located at ~/.ssh/config. If the file doesn't exist, simply create it using your favorite text editor.

  2. Add the following lines to the file:

Host remote_host
    ServerAliveInterval 60
  • Replace remote_host with the actual hostname or IP address of your remote server.

  • ServerAliveInterval represents the time (in seconds) between server-initiated empty packets sent to the client. By default, it is set to 0, which means no empty packets will be sent.

  1. Save the file and exit the text editor.

With these configurations, your SSH client will send periodic empty packets to the server every 60 seconds. This will trick the server into believing that the connection is still active, giving you a more reliable and consistent SSH session. ๐Ÿค๐Ÿš€

Solution 3: Use SSH Keepalive โฑ๏ธ๐Ÿค–

If all else fails, we've got one more ace up our sleeve: SSH Keepalive!

  1. Open your SSH configuration file again (~/.ssh/config).

  2. Add the following lines:

Host *
    ServerAliveInterval 60
    TCPKeepAlive yes
    UseDNS no
  1. Save the file and exit the text editor.

These additional configurations (like in Solution 2) will not only enable empty packet keepalives but also instruct SSH to keep the connection alive by sending actual encrypted packets. This method is more effective in avoiding disconnections caused by firewalls or network devices that drop idle connections. ๐Ÿ›ก๏ธ๐Ÿ’ช

The Final Step: ๐Ÿ’ฌ Share Your Success Story and Engage! ๐Ÿ“ฃ๐Ÿคฉ

Now that you have your SSH session up and running smoothly, why not share your success story with us and our tech-savvy community? We'd love to hear from you! Share your experience in the comments section below and let us know which solution worked best for you. We're here to help and learn together! ๐Ÿ—ฃ๏ธ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Safe and uninterrupted SSH connections await you! Remember, a small tweak can make a big difference. Keep your sessions alive and supercharge your productivity! ๐Ÿ’ฏโœจ

Stay curious. Keep exploring. And may your SSH sessions never be disconnected! ๐ŸŒŸ๐Ÿ”’

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

๐Ÿ”ฅ ๐Ÿ’ป ๐Ÿ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! ๐Ÿš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings ๐Ÿ’ฅโœ‚๏ธ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide ๐Ÿš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? ๐Ÿค” Well, my