I just tried to run AutoGPT on an EC2 instance using SSH from my local Windows machine. But here’s the annoying part: the connection always closes and AutoGPT can only work in small ~10 minute increments. When I return to my machine, I need to SSH into my instance and restart the program.
⚔️ Problem Formulation: Your SSH connection to a remote server works properly at your workplace, but it freezes after 10-15 minutes when connecting from home. You don’t receive any error messages, but you notice zombie login users that need to be killed manually.
Quick and Easy Solution (Client-Side)
To prevent an SSH connection from closing when the client goes silent, you can configure the client to send a keep-alive signal to the server periodically.
Create a configuration file in your home directory at
$HOME/.ssh/config, and set its permissions to
after file creation. To send a keep-alive signal every 240 seconds, for example, add the following lines to the configuration file:
chmod 600 ~/.ssh/config
Host * ServerAliveInterval 240
You can get this done with the following two commands on Linux:
printf '%s\n' 'Host *' ' ServerAliveInterval 240' > ~/.ssh/config chmod 600 ~/.ssh/config
You can then check the file content using the command
cat ~/.ssh/config like so:
Alternative Solution: Server Side
In some cases, you have access to the server’s SSH settings. In that case, add an entry
ClientAliveInterval 60 to the file
/etc/ssh/sshd_config. I used the Vim editor in the terminal to accomplish this.
Second Alternative Solution
You can also try this command to keep the connection alive, i.e., send a keep alive signal every 60 seconds:
ssh -o "ServerAliveInterval 60" <SERVER_ADDRESS> # Example with Key Pair on AWS EC2: ssh -o ServerAliveInterval=60 -i '.\AWS Key Pair.pem' ec2-user@XX.XX.XX.XX
To enable this on all your sessions, add the following line to your
You can use a similar command to write to the file as shown above or simply use
vim /etc/ssh/ssh_config or
sudo vim /etc/ssh/ssh_config, hit the
i key to insert the line and use the key combination (Hotkey)
:wq! to write to the file, save, and quit Vim.
💡 Resource: A detailed explanation of all these configurations is available at the
Boost Your Coding Skills
Do you want to keep improving your coding and tech skills? Feel free to check out our Python and tech academy by downloading your free cheat sheets for starters:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.