[Tut] Keep SSH Session Alive - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Keep SSH Session Alive (/thread-100995.html) |
[Tut] Keep SSH Session Alive - xSicKxBot - 04-19-2023 Keep SSH Session Alive <div> <div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{"align":"left","id":"1302633","slug":"default","valign":"top","ignore":"","reference":"auto","class":"","count":"1","legendonly":"","readonly":"","score":"5","starsonly":"","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","title":"Keep SSH Session Alive","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})","font_factor":"1.25"}'> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div> </p></div> <h2 class="wp-block-heading">Problem Formulation</h2> <p>I just tried to run <a rel="noreferrer noopener" href="https://blog.finxter.com/what-is-autogpt-and-how-to-get-started-your-quick-guide-to-success/" data-type="post" data-id="1292053" target="_blank">AutoGPT</a> on an EC2 instance using SSH from my local Windows machine. But here’s the annoying part: <strong>the connection always closes and AutoGPT can only work in small ~10 minute increments</strong>. When I return to my machine, I need to SSH into my instance and restart the program.</p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2694.png" alt="⚔" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Problem Formulation</strong>: 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.</p> <h2 class="wp-block-heading">Quick and Easy Solution (Client-Side)</h2> <p>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. </p> <p>Create a configuration file in your home directory at <code>$HOME/.ssh/config</code>, and set its permissions to <code>600</code> using <code><code>chmod 600 ~/.ssh/config</code></code> after file creation. To send a keep-alive signal every 240 seconds, for example, add the following lines to the configuration file:</p> <pre class="wp-block-preformatted"><code>Host * ServerAliveInterval 240</code></pre> <p>You can get this done with the following two commands on Linux:</p> <pre class="wp-block-preformatted"><code>printf '%s\n' 'Host *' ' ServerAliveInterval 240' > ~/.ssh/config chmod 600 ~/.ssh/config</code></pre> <p>You can then check the file content using the command <code>cat ~/.ssh/config</code> like so:</p> <figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="986" height="168" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-186.png" alt="" class="wp-image-1302658" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-186.png 986w, https://blog.finxter.com/wp-content/uploads/2023/04/image-186-300x51.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-186-768x131.png 768w" sizes="(max-width: 986px) 100vw, 986px" /></figure> <h2 class="wp-block-heading">Alternative Solution: Server Side</h2> <p class="has-base-2-background-color has-background">In some cases, you have access to the server’s SSH settings. In that case, add an entry <code>ClientAliveInterval 60</code> to the file <code>/etc/ssh/sshd_config</code>. I used the Vim editor in the terminal to accomplish this.</p> <div class="wp-block-image"> <figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="776" height="517" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-187.png" alt="" class="wp-image-1302803" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-187.png 776w, https://blog.finxter.com/wp-content/uploads/2023/04/image-187-300x200.png 300w, https://blog.finxter.com/wp-content/uploads/2023/04/image-187-768x512.png 768w" sizes="(max-width: 776px) 100vw, 776px" /></figure> </div> <p>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:</p> </div> https://www.sickgaming.net/blog/2023/04/18/keep-ssh-session-alive/ |