【问题标题】:AllowGroups and root@IPAllowGroups 和 root@IP
【发布时间】:2014-03-06 17:19:18
【问题描述】:

我有几台具有以下 sshd 配置的服务器。

# Authentication:
PermitRootLogin no
AllowGroups ssh
PubkeyAuthentication yes
PasswordAuthentication no

这意味着“ssh”组中的每个用户都可以登录,但只能使用 pubkey。不允许root登录。

但root一定有一个例外:我的$ip备份服务器必须以root身份登录。

我试过了:

AllowUsers root@$ip
AllowGroups ssh

但是 AllowUsers 会覆盖 AllowGroups 语句。所以只有 $ip 的 root 才能登录。

Match User root, Address $ip
    PermitRootLogin {yes|without-password}
    AllowUsers root

Match Address $ip
    PermitRootLogin {yes|without-password}
    AllowUsers *

两者都被完全忽略。 “ssh”组中的普通用户仍然只能登录。

这是一个简单的场景,用户登录限制为 pubkey,root 登录限制为 pubkey 和某些 ip。如何解决?

【问题讨论】:

    标签: ssh ssh-keys openssh sshd


    【解决方案1】:

    你还没有发布你的整个sshd_config,所以重现这种情况有点困难,但这似乎有效:

    # Main config prohibits all logins
    PermitRootLogin no
    AllowUsers root
    
    # Permit root logins from a specific address
    Match Address 192.168.1.20
      PermitRootLogin yes
    
    # Allow logins to anyone in "ssh" group.
    Match Group ssh
      AllowUsers *
    

    另一种解决方案是:

    • 在您的sshd_config 中有以下内容:

      AllowGroups ssh
      PermitRootLogin without-password
      
    • 使root 成为ssh 组的成员。

      usermod -a -G ssh root
      
    • 将公钥添加到/root/.ssh/authorized_keys 源地址,像这样:

      from=192.168.1.20 ssh-rsa ...
      

    这会让你得到你想要的:

    • 只有ssh 组的成员才能登录。
    • root只能从特定ip地址登录 authorized_keys 文件。

    【讨论】:

    • 在 authorized_keys 中,IP 地址必须用引号括起来: from="192.168.1.20" ssh-rsa ... 两种解决方案都可以正常工作。我采用授权密钥的第二种方式——也许是更“标准的方式”。非常感谢!
    • AllowUsers in a Match 似乎被禁止,并且可能在重新启动时使 ssh 服务崩溃。 (虽然不确定,说来话长……)
    猜你喜欢
    • 2015-12-28
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多