Score:1

Reducing latency before retrieving a local terminal when an SSH session has expired

cn flag
s.k

When working on a remote server, I open an SSH connection in a terminal ssh remote_username@remote_host.

For some reason, if I left this connection idle for too long, the terminal becomes unresponsive, e.g. whatever key I hit, absolutely nothing happens.

But after a certain amount of time, I'm given back the hand on the underlying local terminal and I can work again:

remote_user@remote_server:~$ # <--- whatever I type, nothing happens
# many minutes later, this is printed:
remote_user@remote_server:~$ client_loop: send disconnect: Broken pipe
local_user@local_machine:~ # and I'm given back the hand on my local terminal

This amount of time is quite long (many minutes).

Hence my question: is there a way to parametrize this delay on Ubuntu 22.04.1 LTS?

Versioning

  • ssh: OpenSSH_8.9p1 Ubuntu-3ubuntu0.1, OpenSSL 3.0.2 15 Mar 2022
  • bash: GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Raffa avatar
jp flag
Try setting the `ServerAliveInterval` environment variable to try and keep the session active ... e.g. `ssh -o ServerAliveInterval=120 user@localhost`
Score:1
jp flag

You might need to set the option ServerAliveInterval ... From man ssh_config:

ServerAliveInterval

Sets a timeout interval in seconds after which if no data has been received from the server, ssh(1) will send a message through the encrypted channel to request a response from the server. The default is 0, indicating that these messages will not be sent to the server.

It takes a value as a number of seconds ... e.g. like so:

ServerAliveInterval 120

You can add that in a new line to ssh config either for your user in the file ~/.ssh/config(It's not there by default so you might need to create this file) or globally in the file /etc/ssh/ssh_config ... Notice that you should add it under a Host keyword ... e.g. in the /etc/ssh/ssh_config there is a keyword Host * which means apply the below arguments to all remote hosts ... So you would add it to the existing arguments e.g. like so:

Host *
    SendEnv LANG LC_*
    HashKnownHosts yes
    GSSAPIAuthentication yes
    ServerAliveInterval 120

Alternatively, as stated in man ssh:

-o option

Can be used to give options in the format used in the configuration file.

You can set that option directly in the command itself ... e.g. like so:

ssh -o ServerAliveInterval=120 user@host
Score:0
pl flag

I see this sometimes with my home connection to a remote server. One workaround is to leave something running on the remote end which produces output, so traffic is constantly flowing, but not to much that it's a bandwidth hog. What I use is Byobu

enter image description here

While running, Byobu has a clock on screen (along with other things like disk space, and other useful data) in the status bar. Having that on screen typically stops the connection timing out.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.