我正在 Ubuntu 18.04.3 LTS 和 v21.5 上运行 ssh 命令mobaxterm
,并且尝试连接内核为 4.9 的 Linux 板。
我已经配置/etc/ssh/sshd_config
为
ClientAliveInterval 60
ClientAliveCountMax 0
在linux板上。
60秒后,仅在openssh 8.0版本中断开,但在8.2、8.5、8.7、8.9版本中不断开。是8.2版本的bug吗? 8.5、8.7。 8.9?
答案1
在 ClientAliveInterval /etc/ssh/sshd_config 中指定的时间之后
你配置了什么值?sshd 联机帮助页规定如下:
ClientAliveInterval
Sets a timeout interval in seconds after which if no data has been
received from the client, sshd(8) will send a message through the
encrypted channel to request a response from the client. The default is 0,
indicating that these messages will not be sent to the client.
This option applies to protocol version 2 only.
如果您希望连接超时,听起来默认值是可行的方法。
注意会话保持活动是还配置了在连接上客户一侧~/.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.
This option applies to protocol version 2 only.
比如我自己不想要ssh 会话超时,这就是我的以下内容的原因~/.ssh/config
:
Host *
ServerAliveInterval 15
ServerAliveCountMax 3
答案2
该文件的手册页sshd_config
(Openssh v8.6p1)在描述的末尾说道ClientAliveCountMax
:
Setting a zero ClientAliveCountMax disables connection termination.
您的ClientAliveCountMax 0
设置正在防止断开连接。尝试默认的3
(正如劳尔已经建议的那样),或者至少1
。
答案3
也许您可以使用 bash 脚本来终止空闲的 ssh 连接,并将此 bash 脚本设置为每分钟在 crontab 上运行一次。只需将下面的值更改为您首选的空闲时间即可终止 ssh 连接 1800 秒 = 30 分钟
#!/bin/bash
# Get the idle information from "w" command#
idle_info=$(w)
# Loop through each line of idle information#
while IFS= read -r line; do
# Extract the username, terminal, and idle time from the line
username=$(echo "$line" | awk '{print $1}')
terminal=$(echo "$line" | awk '{print $2}')
idle_time=$(echo "$line" | awk '{print $5}' | sed 's/[^0-9.]//g')
# Check if the terminal is an SSH session and the idle time exceeds 30 minutes (1800 seconds)#
if [[ "$terminal" =~ ^pts/ ]] && (( $(echo "$idle_time > 1800" | bc -l) )); then
# Terminate the SSH session#
sudo pkill -9 -t "$terminal"
echo "Killed SSH session for user $username on terminal $terminal (Idle for $idle_time seconds)"
fi
done <<< "$idle_info"