{"id":309,"date":"2025-07-10T00:25:53","date_gmt":"2025-07-10T00:25:53","guid":{"rendered":"https:\/\/vitorprado.com\/?p=309"},"modified":"2025-07-10T00:26:00","modified_gmt":"2025-07-10T00:26:00","slug":"ssh-connection-alive-and-prevent-it-from-timing-out-heartbeat","status":"publish","type":"post","link":"https:\/\/vitorprado.com\/?p=309","title":{"rendered":"SSH connection alive and prevent it from timing out (heartbeat)"},"content":{"rendered":"\n<p>To keep an SSH connection alive and prevent it from timing out, you can use the&nbsp;<code>ServerAliveInterval<\/code>&nbsp;option in your SSH client configuration or command line.&nbsp;This option sets the interval at which the client sends keepalive messages to the server.&nbsp;Additionally, the&nbsp;<code>ServerAliveCountMax<\/code>&nbsp;option controls how many unanswered keepalive messages trigger a connection termination.&nbsp;<\/p>\n\n\n\n<p>To set the heartbeat interval in your SSH client configuration:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Locate the SSH configuration file:<\/strong>\u00a0The file is typically located at\u00a0<code>~\/.ssh\/config<\/code>\u00a0for user-specific settings, or\u00a0<code>\/etc\/ssh\/ssh_config<\/code>\u00a0for system-wide settings.<\/li>\n\n\n\n<li><strong>Add or modify the following lines:<\/strong>\u00a0<\/li>\n<\/ol>\n\n\n\n<p>Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Host *<br>  ServerAliveInterval 60<br>  ServerAliveCountMax 3<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Host *<\/code>\u00a0applies the settings to all hosts.\u00a0You can specify a particular host instead, like\u00a0<code>Host example.com<\/code>.\u00a0<\/li>\n\n\n\n<li><code>ServerAliveInterval 60<\/code>\u00a0sets the interval to 60 seconds.\u00a0<\/li>\n\n\n\n<li><code>ServerAliveCountMax 3<\/code>\u00a0closes the connection if 3 consecutive keepalive messages are not acknowledged.\u00a0<\/li>\n<\/ul>\n\n\n\n<p>To set the heartbeat interval directly in the SSH command:&nbsp;<\/p>\n\n\n\n<p>Code<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ssh -o ServerAliveInterval=60 -o ServerAliveCountMax=3 user@hostname<\/code><\/pre>\n\n\n\n<p>Explanation:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ServerAliveInterval<\/code>: Specifies the interval (in seconds) at which the client sends keepalive messages (also known as &#8220;hellos&#8221;) to the server.\u00a0If the server doesn&#8217;t respond within this interval, the client will send another keepalive.\u00a0If the server fails to respond after\u00a0<code>ServerAliveCountMax<\/code>\u00a0intervals, the connection is dropped.<\/li>\n\n\n\n<li><code>ServerAliveCountMax<\/code>: Defines the number of unanswered keepalive messages that will trigger a disconnection.\u00a0<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>To keep an SSH connection alive and prevent it from timing out, you can use the&nbsp;ServerAliveInterval&nbsp;option in your SSH client configuration or command line.&nbsp;This option sets the interval at which the client sends keepalive messages to the server.&nbsp;Additionally, the&nbsp;ServerAliveCountMax&nbsp;option controls how many unanswered keepalive messages trigger a connection termination.&nbsp; To set the heartbeat interval in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,11],"tags":[72,24,20,17,73,71,18],"class_list":["post-309","post","type-post","status-publish","format-standard","hentry","category-cloud","category-software","tag-bash","tag-cloud","tag-documentation","tag-linux","tag-shell","tag-ssh","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/vitorprado.com\/index.php?rest_route=\/wp\/v2\/posts\/309","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vitorprado.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vitorprado.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vitorprado.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vitorprado.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=309"}],"version-history":[{"count":1,"href":"https:\/\/vitorprado.com\/index.php?rest_route=\/wp\/v2\/posts\/309\/revisions"}],"predecessor-version":[{"id":310,"href":"https:\/\/vitorprado.com\/index.php?rest_route=\/wp\/v2\/posts\/309\/revisions\/310"}],"wp:attachment":[{"href":"https:\/\/vitorprado.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vitorprado.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vitorprado.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}