Linux connection keep alive

How to keep SSH connection alive?

How can I keep an SSH connection alive without actively using it. When I use it, it works fine, but when I don’t use it for a few minutes, it disconnects. As I get an email each time I connect, how can I keep the SSH session alive? Note: In my .ssh/config file, at the top I have

Host * ServerAliveInterval 3 

What ssh client ? 3 is too short, use an interval of 60. You can also put that in /etc/ssh/ssh_config if you have root access.

3 Answers 3

You need to specify amount of the tries and interval of sending a packet to the server. You can put the following lines in your .ssh/config file:

Host examplehost Hostname examplehost.com ServerAliveInterval 180 ServerAliveCountMax 2 

This will send a packet to the server every 180 seconds (3 minutes) and it will try two times before it closes the connection after getting no response from the client.

Are there any specific config on sshd side that woud disable this keep alive attempt from client? I applied the similar setting but just not working

You can also add these arguments in the ssh command, like so.

ssh -o ServerAliveInterval=180 -o ServerAliveCountMax=2 $HOST 

On Linux and Apple Mac OS X operating systems, the ~/.ssh/config file enables you to specify many SSH settings, including those that keep alive an SSH connection. To do this, follow these steps:

  1. Use your preferred text editor to open the ~/.ssh/config file on your local computer. Note: If the .ssh directory or the config file do not exist, create them.
  2. Add the following lines to the config file. The Host value can be any name you want; it is simply a label for the other settings. The Hostname value is the remote host you want to access; replace example.com with your domain name. Replace username with your own Hosting account username:
 Host example Hostname example.com Port 7822 User username ServerAliveInterval 240 ServerAliveCountMax 2 

With this configuration, the SSH client sends a packet to the server every 240 seconds (4 minutes) to keep the connection alive. If the client does not receive a response after two tries (as specified by the ServerAliveCountMax setting), it closes the connection.

Читайте также:  Prime h510m k linux

For detailed information about all of the SSH configuration settings available, type man ssh_config at the command line.

  1. Save the changes to the config file.
  2. Connect to your account using SSH. To do this, simply type ssh example where example represents the Host value you specified in step 2.

Monitor the connection. If it still drops, gradually decrease the ServerAliveInterval setting in the config file until the connection is stable.

Источник

Default TCP KeepAlive settings

Sending probes after 1¼ minutes sound reasonable, and declaring failure after 9 failed probes does as well, but what is the idea behind the initial time being 2 hours?

Note that underlying connection tracking mechanisms and application timeouts may be much shorter.

The main point of enabling keepalive is to prevent any stateful network elements from dropping the state information, but such elements tend to drop the connections in a couple of minutes. With some rate-limited servers, curl with short —keepalive-time seems to significantly improve reliability of downloads.

So why is the default so long?

1 Answer 1

TCP Keep-alive was defined at a time when even the concept of firewall, let alone stateful firewall or NAT, was probably not widespread. From RFC 1122 (October 1989):

4.2.3.6 TCP Keep-Alives

Implementors MAY include «keep-alives» in their TCP
implementations, although this practice is not universally
accepted. If keep-alives are included, the application MUST
be able to turn them on or off for each TCP connection, and
they MUST default to off.

Keep-alive packets MUST only be sent when no data or
acknowledgement packets have been received for the
connection within an interval. This interval MUST be
configurable and MUST default to no less than two hours.

The main idea at the time wasn’t about stateful information lost:

DISCUSSION:
A «keep-alive» mechanism periodically probes the other
end of a connection when the connection is otherwise
idle, even when there is no data to be sent. The TCP
specification does not include a keep-alive mechanism
because it could: (1) cause perfectly good connections
to break during transient Internet failures; (2)
consume unnecessary bandwidth («if no one is using the
connection, who cares if it is still good?»); and (3)
cost money for an Internet path that charges for
packets
.

A TCP keep-alive mechanism should only be invoked in
server applications that might otherwise hang
indefinitely and consume resources unnecessarily if a
client crashes or aborts a connection during a network
failure.

I skimmed the updating RFCs, but couldn’t fine mention of keep alives.

Читайте также:  Linux mint настройка дисплея

Источник

Keep SSH session alive [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I use ssh -p8520 username@remote_host to login remote server. Issue: It is always connected and works properly when I am in the work place. Unfortunately, terminal freezes in 10 — 15 minutes after I connected with the remote server from home. There’s no error/timeout report on the console but the cursor cannot move any more. When enter w to check the login users, some zombies login users are there, and I have to kill them manually. This is quite annoying. Can anyone help me?

@martin screen helps to keep the program running. Unfortunately sometimes i have to work on the remote server:(

Are your saying screen only? It can solve the lost connection issue? No screen -S screenName ? I always use screen with -S and -r

No, this does not solve the issue, you only avoid loosing your work in the terminal. If you don’t do anything, it will still freeze, you just can pick up where you started. I meant that you can create a terminal which you can always resume by using screen . You seem to already know that 😉

5 Answers 5

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.

Читайте также:  Linux копирование одного файла

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 

Источник

Оцените статью
Adblock
detector