暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Linux Mac ssh 客户端长连接防断连 - tcp心跳 TCPKeepAlive,ServerAliveInterval,ServerAliveCountMax

digoal 2021-01-01
1611

作者

digoal

日期

2021-01-30

标签

PostgreSQL , ssh


背景

securecrt 可以设置会话option: send protocol no-op 来防止ssh连接长时间没有数据包时被断开.

linux macos自带的ssh客户端也有同样的参数:

ServerAliveInterval
ServerAliveCountMax
TCPKeepAlive

实际上PostgreSQL数据库也有类似设置参数

```

- TCP settings -

see "man tcp" for details

tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds;

                                    # 0 selects the system default

tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;

                                    # 0 selects the system default

tcp_keepalives_count = 0 # TCP_KEEPCNT;

                                    # 0 selects the system default

tcp_user_timeout = 0 # TCP_USER_TIMEOUT, in milliseconds;

                                    # 0 selects the system default

```

例子:

ssh -o TCPKeepAlive=yes -o ServerAliveInterval=15 -o ServerAliveCountMax=3 root@192.168.56.101

man ssh_config

TCPKeepAlive
Specifies whether the system should send TCP keepalive messages to the other side. If they are sent, death of the connection or crash of one of the machines will be properly noticed. However, this means that connections will die if the route is down temporarily, and some people find it annoying.

The default is yes (to send TCP keepalive messages), and the client will notice if the network goes down or the remote host dies. This is important in scripts, and many users want it too.

To disable TCP keepalive messages, the value should be set to no. See also ServerAliveInterval for protocol-level keepalives.

ServerAliveCountMax
Sets the number of server alive messages (see below) which may be sent without ssh(1) receiving any messages back from the server. If this threshold is reached while server alive messages are being sent, ssh will disconnect from the server, terminating the session. It is important to note that the use of server alive messages is very different from TCPKeepAlive (below). The server alive messages are sent through the encrypted channel and therefore will not be spoofable. The TCP keepalive option enabled by TCPKeepAlive is spoofable. The server alive mechanism is valuable when the client or server depend on knowing when a connection has become unresponsive.

The default value is 3. If, for example, ServerAliveInterval (see below) is set to 15 and ServerAliveCountMax is left at the default, if the server becomes unresponsive, ssh will disconnect after approximately 45 seconds.

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.

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论