这个问题很老了,可能更适合服务器故障,但除了引用手册页之外,它从来没有得到答案。我的回答是通过添加一些上下文来补充手册页的详细信息。
首先,应该注意的是,这两个设置是相互独立的,它们针对的是 SSH 连接的不同阶段。
最大会话数
SSH 允许会话多路复用,也就是只使用一个 TCP 连接同时打开多个会话(例如 shell、sftp 传输和原始命令)。这节省了多次 TCP 握手和多次 SSH 身份验证的开销。参数MaxSessions 允许将此多路复用限制为一定数量的会话。
如果您设置MaxSessions 1 并打开一个shell,您仍然可以运行SFTP 传输或打开第二个shell,但在后台SSH 将打开另一个TCP 连接并再次进行身份验证。 (使用密码验证使其可见)。
如果您设置MaxSessions 0,您可以确保没有人可以打开会话(shell、SFTP 或类似的),但您仍然可以连接以打开隧道或通过 ssh 连接到下一个主机。
查看ssh_config(5) 的 ControlMaster 部分。
MaxSessions
Specifies the maximum number of open shell, login or subsystem
(e.g. sftp) sessions permitted per network connection. Multiple
sessions may be established by clients that support connection
multiplexing. Setting MaxSessions to 1 will effectively disable
session multiplexing, whereas setting it to 0 will prevent all
shell, login and subsystem sessions while still permitting for-
warding. The default is 10.
MaxStartups
当您连接到远程 SSH 服务器时,在建立连接和成功验证之间有一个时间窗口。这个时间范围可能非常小,例如当您将 SSH 客户端配置为为此连接使用某个私钥时,或者它可能很长,当客户端首先尝试三个不同的 SSH 密钥时,要求您输入密码,然后等待您输入第二因素身份验证码你通过短信得到。同时在此时间范围内的连接总和是引用的手册页中提到的“并发未经身份验证的连接”。如果此状态下的连接太多,sshd 将停止接受新的连接。您可以调整 MaxStartups 以在发生这种情况时进行更改。
更改默认值的实际用例是例如由 ansible 等配置软件使用的跳转主机。当被要求在跳转主机后面配置大量主机时,Ansible 会同时打开许多连接,因此如果连接的打开速度快于 SSH 主机能够验证它们的速度,它可能会遇到此限制。
MaxStartups
Specifies the maximum number of **concurrent unauthenticated con-
nections 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).