Problem Formulation
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 600
using
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 /etc/ssh/ssh_config
or ~/.ssh/config
:
ServerAliveInterval 60
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
or :wq!
to write to the file, save, and quit Vim.
π‘ Resource: A detailed explanation of all these configurations is available at the ssh_config
manpage.
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: