html tool

显示标签为“sshd”的博文。显示所有博文
显示标签为“sshd”的博文。显示所有博文

2023年7月10日星期一

转:ssh并发连接控制

 参考:

https://blog.tankywoo.com/2020/09/02/ssh-maxstartups.html

https://juejin.cn/s/sshd_config%20maxstartups%20maxsessions


单个客户端ssh数量

maxsessions用于限制每个客户端可以同时打开的SSH会话数量。这个参数可以防止单个客户端占用过多的服务器资源。maxsessions的语法如下:

typescript
复制代码
MaxSessions number



MaxStartups

past MaxStartups,查阅 man 手册:

MaxStartups
    Specifies the maximum number of concurrent unauthenticated connections to the SSH daemon.
    Additional connections will be dropped until authentication succeeds or the LoginGraceTime expires for a connection.
    The default is 10:30:100.

    Alternatively, random early drop can be enabled by specifying the three colon separated values start:rate:full (e.g. "10:30:60").
    sshd(8) will refuse connection attempts with a probability of rate/100 (30%) if there are currently start (10) unauthenticated connections.
    The probability increases linearly and all connection attempts are refused if the number of unauthenticated connections reaches full (60).

即控制并发初始化连接的个数,初始化连接即还未到认证层面的连接,如默认的 10:30:100,表示如果超过 10 个并发初始化连接,则后续的连接由 30% 的几率直接被关闭,如果超过 100 个连接,则所有新的连接 100% 直接关闭。

再比如我本地模拟测试,配置 MaxStartups 3:100:3,即表示并发初始化连接超过 3 个时,直接 100% 丢包,所以我开 tmux 四个窗口同时登陆服务端,总会有一个连接报错并关闭连接。

结合实际的场景为何会出现这个问题,我们的服务器都做了 ssh 白名单防火墙限制,只有白名单的网段可以登录,其余数据包在防火墙层面直接丢弃;但是个别服务器因为一些原因,未开启此配置,导致有大量的 ssh 端口扫描,自然就达到了 MaxStartups 的限制,也就出现了偶尔丢包的情况。


修改/etc/sshd_config 配置后 ,重启service sshd restart


2022年11月14日星期一

Connection closed by remote host

 问题:

jenkins 的log中提示"Connection closed by remote host"

解决:

参考:

【sshd配置修改】

https://blog.csdn.net/anyxie/article/details/50560553 【MaxSessions 

https://blog.csdn.net/u014686399/article/details/84778292 【maxstartup 

三元组】


【服务重启】https://blog.csdn.net/wo541075754/article/details/79092281


ssh提示 "Connection closed by remote host"的原因:

如果原来是可以用ssh连接的, 突然连接不上通常是连接数过多导致的. 

解决方法一. 把SSH连接数改大 

修改服务器上的这个文件:/etc/ssh/sshd_config 找到这行:

  1. # MaxSessions 10 

去掉前面的"#" 并把数字改大,最后重启sshd service sshd restart 然后重新连接即可. 

-------------------------------------------------------------------

问题:重启后还是在50以内的ssh看到 连接问题,继续:

maxstartup

这个是限制处于联机页面的连接数,默认值10。联机页面就是当你登录ssh时,还没输入密码的页面。


三元组形式

10:30:60

10:当连接数达到10时就开始拒绝连接,不过不是全部拒绝,我们继续往下看

30:当连接数到达10时,之后的连接有30的概率被拒绝掉

60:当连接数达到60时,之后的连接就全部拒绝了

一个数字的形式

我们可以直接 写个60,这样言简意赅,连接数达到60之前敞开玩,达到60后就不能玩了。


-------------------------------------------------------------------

问题:

重启sshd时,提示:

”Extra argument service.“


解决: systemctl restart sshd

在修改了sshd_config文件之后需要重启sshd,准备执行一下命令进行重启:


/etc/init.d/ssh restart

1

但是发现目录下根本没有ssh程序,于是通过命令which ssh查找到ssh命令路径在:


/usr/bin/ssh

1

然后尝试在后面添加restart参数进行重启发现,此命令也行不通。


# /usr/sbin/sshd restart

Extra argument restart.

再次尝试使用一下命令进行重启:


# sudo service ssh restart

Redirecting to /bin/systemctl restart ssh.service

Failed to restart ssh.service: Unit not found

发现也行不通。


最终终于发现原因,原来service ssh restart是centos 6的命令。centos 7的命令应该是:


systemctl restart sshd