如何仅允许来自某些 IP 地址的 SSH 密码身份验证?

ænd*_*rük 121 ssh configuration security authentication password

我想只允许来自某个子网的 SSH 密码身份验证。我看到了全局禁止它的选项/etc/ssh/sshd_config

# Change to no to disable tunnelled clear text passwords
#PasswordAuthentication yes
Run Code Online (Sandbox Code Playgroud)

有没有办法将此配置应用于选定的 IP 地址范围?

Gil*_*il' 182

使用Match在最后/etc/ssh/sshd_config

# Global settings
…
PasswordAuthentication no
…

# Settings that override the global settings for matching IP addresses only
Match address 192.0.2.0/24
    PasswordAuthentication yes
Run Code Online (Sandbox Code Playgroud)

然后告诉 sshd 服务重新加载其配置:

service ssh reload
Run Code Online (Sandbox Code Playgroud)

  • 可能的问题是您将 Match 块放在 sshd_config 中间的某个位置。匹配行会影响接下来的每一行,直到下一个匹配行,所以它们应该在文件的末尾。 (31认同)
  • 尽管答案中有缩进,但 `sshd_config` 不是 Python `;)` (6认同)
  • @MichaelWaterfall 用这么少的信息来判断是不可能的。确保在验证新配置之前保持 shell 运行。重新启动 ssh 服务不会影响活动连接。 (3认同)

小智 9

你可以加:

AllowUsers user1@192.168.*.*, user2@192.168.*.*
Run Code Online (Sandbox Code Playgroud)

这会改变默认行为,真正拒绝来自所有主机的所有其他用户。匹配块在OpenSsh 5.1及更高版本上可用。