3

I'm trying to run a command that might take 1+ hours and I've tried adding

while sleep 300; do echo "1"; done

so that it doesn't disconnect me for being idle and it worked.

But it still disconnects after sometimes for another reason it says Connection to *.*.*.* closed by remote host.

I guess its just a default time limit or something I don't really know?

Is there a way to increase that time or just make it never disconnects?

If not is there a way to keep that command running even after closing the ssh connection ?

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Khalood Kj
  • 75
  • 1
  • 1
  • 7

3 Answers3

10

If you can't change the server setting, you should start your ssh by specifying frequent keep-alive signals to allow your command to complete. E.g.

ssh -o ServerAliveInterval=20 myname@myhost.com

should issue an "alive" signal every 20 seconds towards the server, so that it keep the connection alive

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
2

For me, the SSH connection used to hang up.

The below solution worked for me. Now the SSH connection always stays live.

From the server:


  1. Edit the file at /etc/ssh/sshd_config
$ sudo vi /etc/ssh/sshd_config
  1. Add or enable this line in the file.
ClientAliveInterval 60
  1. Save the file and restart the sshd service
$ sudo service sshd restart

From the local machine/client:


  1. Edit the file at /etc/ssh/ssh_config
$ sudo vi /etc/ssh/ssh_config
  1. Add this line to the file.
ServerAliveInterval 60
  1. Save the file

About these Parameters :

ServerAliveInterval: number of seconds that the client will wait before sending a null packet to the server (to keep the connection alive).

ClientAliveInterval: number of seconds that the server will wait before sending a null packet to the client (to keep the connection alive).

Important: Whenever any modification is performed on a Production instance, please ensure to create a backup.

1

code below is for ServerAliveCountMax, please remember not to keep it as 0

https://github.com/openssh/openssh-portable/blob/c8eb3941758615c8284a48fff47872db926da63c/packet.c#L330

static void
server_alive_check(struct ssh *ssh)
{
    int r;

    if (ssh_packet_inc_alive_timeouts(ssh) > options.server_alive_count_max) {
        logit("Timeout, server %s not responding.", host);
        cleanup_exit(255);
    }
    if ((r = sshpkt_start(ssh, SSH2_MSG_GLOBAL_REQUEST)) != 0 ||
        (r = sshpkt_put_cstring(ssh, "keepalive@openssh.com")) != 0 ||
        (r = sshpkt_put_u8(ssh, 1)) != 0 ||     /* boolean: want reply */
        (r = sshpkt_send(ssh)) != 0)
        fatal_fr(r, "send packet");
    /* Insert an empty placeholder to maintain ordering */
    client_register_global_confirm(NULL, NULL);
    schedule_server_alive_check();
}
郭晓峰
  • 76
  • 2