Keep Ssh Session Alive

Keep SSH session alive

The ssh daemon (sshd), which runs server-side, closes the connection from the server-side if the client goes silent (i.e., does not send information). To prevent connection loss, instruct the ssh client to send a sign-of-life signal to the server once in a while.

The configuration for this is in the file $HOME/.ssh/config, create the file if it does not exist (the config file must not be world-readable, so run chmod 600 ~/.ssh/config after creating the file). To send the signal every e.g. four minutes (240 seconds) to the remote host, put the following in that configuration file:

Host remotehost
HostName remotehost.com
ServerAliveInterval 240

To enable sending a keep-alive signal for all hosts, place the following contents in the configuration file:

Host *
ServerAliveInterval 240

How do I keep SSH connection alive on Windows 10?

You can solve this on the client side by setting the options ServerAliveInterval and ServerAliveCountMax (60 and 30 will send a keep-alive packet every minute and still keep the connection if your network falls for up to 30 minutes).

The Windows OpenSSH client has some issues finding the ssh_config file (it appears to have doubts about what the "home" directory is in a Windows System), so you may have to explicitly provide the config file path with -F:

ssh -F C:\wherever\ssh_config user@host

On the server side you can edit /etc/ssh/sshd_config, edit/add the similar ClientAliveInterval and ClientAliveMaxCount options and restart the sshd service.

Keep SSH session alive while computer sleep?

I found the answer it depends on tcp keepalive settings:

For the list of available TCP settings (FreeBSD 4.8 an up and 5.4):

sysctl -A | grep net.inet.tcp
  • net.inet.tcp.keepidle - Amount of time, in milliseconds, that the (TCP) connection must be idle before keepalive probes (if enabled) are sent.

  • net.inet.tcp.keepintvl - The interval, in milliseconds, between keepalive probes sent to remote machines. After TCPTV_KEEPCNT (default 8) probes are sent, with no response, the (TCP)connection is dropped.

  • net.inet.tcp.always_keepalive - Assume that SO_KEEPALIVE is set on all TCP connections, the kernel will periodically send a packet to the remote host to verify the connection is still up.

Therefore formula to calculate maximum TCP inactive connection time is following:

net.inet.tcp.keepidle + (net.inet.tcp.keepintvl x 8)

the result is in milliseconds. Therefore, by setting

net.inet.tcp.keepidle = 10000
net.inet.tcp.keepintvl = 5000
net.inet.tcp.always_keepalive = 1 (must be 1 always)

the system will disconnect a call when TCP connection is dead for: 10000 + (5000 x 8) = 50000 msec (50 sec). To make system remember these settings at startup, you should add them to /etc/sysctl.conf file

Keep SSH connection alive for subsequent ssh command using Python subprocess

There are a couple of ways to do that.

  • Keep the process open. Use Popen to open an ssh process and define its standard input and output as pipes. You can then write to its stdin and read from its stdout. Since you want to do it a number of times, you should not use communicate, but rather write directly to the process stdin using write and flush. (see a piece of same code below)
  • The better solution, in my mind, would be to use a Python ssh package such as https://github.com/paramiko/paramiko/ (or fabric - which is typically used for other purposes). Here, you can open a channel and just write to it and read from it with less hustle.

Sample code for reading / writing to a subprocess

import subprocess

p = subprocess.Popen("bash", stdin=subprocess.PIPE, stdout = subprocess.PIPE, stderr=subprocess.PIPE)

p.stdin.write(b"ls\n")
p.stdin.flush()

res = p.stdout.read(100)
print(res)

Mobaxterm: how to prevent ssh session from exiting?

If you are still looking for the answer like me, here you go!

Settings -> Configuration -> SSH -> SSH keepalive

Restart MobaXTerm after changing the setting for it to take effect.

https://superuser.com/a/1298536

Keep SSH Sessions running after disconnection - for overnight

Run the screen on the server (as opposed to on the client, which is what you seem to be doing right now). This way, MATLAB can write output even if you are not connected to the server via ssh. The order of commands for this is ssh, screen, matlab. If you want to resume your session, just connect to the server via ssh, then run something like screen -x

How do I keep a shell (ssh) connection alive?

Try SSH keepalive, for MobaXterm follow:

https://mobaxterm.mobatek.net/documentation.html#6_8_4



Related Topics



Leave a reply



Submit