美文网首页
Linux_SSH服务器只允许Key建立连接

Linux_SSH服务器只允许Key建立连接

作者: 很少更新了 | 来源:发表于2017-06-02 17:23 被阅读168次

    Linux_SSH服务器只允许Key建立连接

    1. 创建 SSH KEY

    使用ssh-keygen生成一个密钥对,并且将公钥注册到服务器的 $HOME/.ssh/authorized_keys 文件。

    2. 确保启用 SSH 公钥认证功能

    查看 /etc/ssh/sshd_config 文件,确保以下两条为 yes:
    
    RSAAuthentication yes
    PubkeyAuthentication yes
    一般它们默认都是 yes,如果不是,请修改为 yes,保存并且重启 SSH 服务:
    
    $ sudo service ssh reload
    

    3. 禁止密码安全验证

    编辑 /etc/ssh/sshd_config 文件,确保以下内容出现在文件中:
    
    ChallengeResponseAuthentication no
    PasswordAuthentication no
    UsePAM no
    保存并重启 SSH 服务:
    
    $ sudo service ssh restart
    如果你当前处于 SSH 连接登录状态,可能重启服务会失败,可以尝试重启系统。
    

    4. 禁止特定条件使用密码登录

    有时我们并不想禁止所有用户的口令登录,可以通过配置 sshd_config 文件来实现对特定对象的登录设置。

    使用 $ man sshd_config 查看帮助信息。sshd_config 支持在文件中增加 Match 区块,如果 Match 关键字所在行的条件匹配成功,则 Match 后所有的关键字将被逐个加载,直到遇见另一个 Match 关键字或者文件结尾。所以一般 Match 区块添加在 sshd_config 文件末尾。

    Match 关键字支持的条件包括 User, Group, Host 和 Address,条件样式是单个字符串,多个样式使用逗号分隔,也可以使用通配符(*)和求反符号(!)。
    
    Address 条件样式可以是 CIDR(地址/掩码)格式,例如:192.0.2.0/24 或 3ffe:ffff::/32。
    

    例如

    禁止用户 foo,用户组 bar 使用口令登录,在 /etc/ssh/sshd_config 文件末尾添加以下内容:
    Match User foo, Group bar
        PasswordAuthentication no
    
    禁止除用户 foo 以外其他用户使用口令登录:
    Match User *, !foo
        PasswordAuthentication no
    

    Match 区块支持的关键字包括:

    AllowAgentForwarding, AllowTcpForwarding, AuthorizedKeysFile, 
    AuthorizedPrincipalsFile, Banner, ChrootDirectory, ForceCommand, 
    GatewayPorts, GSSAPIAuthentication, HostbasedAuthentication, 
    HostbasedUsesNameFromPacketOnly, KbdInteractiveAuthentication, 
    KerberosAuthentication, MaxAuthTries, MaxSessions, PasswordAuthentication,
     PermitEmptyPasswords, PermitOpen,  PermitRootLogin, PermitTunnel, 
     PubkeyAuthentication, RhostsRSAAuthentication, RSAAuthentication, 
     X11DisplayOffset, X11Forwarding, X11UseLocalHost.

    相关文章

      网友评论

          本文标题:Linux_SSH服务器只允许Key建立连接

          本文链接:https://www.haomeiwen.com/subject/dmmufxtx.html