0%

ssh或scp失败的问题

项目中有这么一个需求,要把本地文件较大批量的拷贝到远程机器,因为拷贝的机器数比较多,所以使用了nohup scp xxx user@host:/path/to/file > /dev/null 2>& &这种异步操作,最近发现经常有一些文件拷贝失败,经排查最终定位到是ssh的连接数超出限制,导致被目标机器拒绝了。

官方文档地址:https://linux.die.net/man/5/sshd_config

其中需要修改的参数是MaxStartups,官方的文档描述如下:

1
2
3
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.

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).

简单来说就是这个参数用来指定ssh的最大并发未身份验证连接数,默认是10,在大批量文件拷贝的情况下显然是不够的。

参数可以设置一个整数,如30,也可以设置一个用冒号分割的值:start:rate:full,假如设置的是10:30:60,那么它的作用是:

  • 连接数在10(start)以下不会拒绝连接
  • 当连接数超过10(start)之后,新的连接有30%(rate)的几率被拒绝掉
  • 当连接数超过60(full)后,那么所有的连接均会被拒绝

查到了原因,根据自己实际的情况,将参数设置为合适值,执行service ssh restart重启ssh服务,问题修复。