如何限制特定IP地址(私有地址)的用户登录?

如何限制特定IP地址(私有地址)的用户登录?

我的 ssh 服务器计算机上有两个用户,user_A 和 user_B。 user_B 被允许使用私钥登录只是出于安全原因,因为他需要从远程登录。这一切都有效。我的问题:如何防止 user_A 使用用户名/密码从远程登录,因为他只需要从本地网络登录?根据 sshd 的手册页,允许使用 CIDR 表示法。

我所做的:
#605433 建议 ,所以我适应了AllowUsers [email protected]AllowUsers [email protected]/24

#740700 建议:

Match 192.168.0.10/24
   AllowGroups PrivateSubnetSshUsers

我的版本看起来像

Match 192.168.10.0/24
   AllowUsers user_A

出乎我的意料,在这两种情况下,user_A 仍然可以从 192.168.1.220 登录。在重试之前我已经做了一些systemctl restart sshd

我在这里忽略了什么?

答案1

首先,你的陈述中存在错误,Match这是应该的Match address 192.168.10.0/24,而不仅仅是错误Match 192.168.10.0/24。您的版本在尝试重新启动 sshd 时给出错误消息。你没有收到吗?

其次,您应该Match使用用户名,而不是地址。以下应该有效:

Match user user_A
   AllowUsers [email protected]/24

顺便提一句。从我的测试来看,单独(即不在块中)也可以工作(所以你在测试它时一定做错了什么),但它限制了AllowUsers [email protected]/24Match所有其他用户除了 user_A 之外的登录根本不(并且user_A只能从指定的网络登录)。如果在Match上述块内,则user_A只能从指定网络登录,所有其他用户可以从任何IP地址登录。

答案2

我感到非常尴尬。当然,案件已经解决,就像拉吉所正确指出的那样。我的错误,也就是尴尬,就是总是编辑 ssh_config,而不是 sshd_config。非常抱歉,并再次感谢您的帮助!

答案3

根据手册(https://www.man7.org/linux/man-pages/man5/sshd_config.5.html),它应该像这样工作:

# Only user_B is allowed to login
AllowUsers user_B

# Only the group PrivateSubnetSshUsers is allowed to login from the network 192.168.10.0/24
Match Address 192.168.10.0/24
        AllowGroups PrivateSubnetSshUsers 

# user_B is not allowed to login with password
Match User user_B
        PasswordAuthentication no

使用第一个选项,您可以锁定除 user_B 之外的所有用户。如果您从给定网络连接并且是 PrivateSubnetSshUsers 组的成员,则第二个选项允许登录。第三个选项强制 user_B 使用密钥。

它不是超级简单,但它应该可以完成工作。

相关内容