• [Linux] 如何禁止使用口令只允许使用密钥建立 SSH 连接


    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.

      

      sshd_config 文件内容大小写敏感,编辑该文件一定要小心仔细,如有不慎可能导致 SSH 服务器无法启动。如果你的主机就在你身边,你也许还可以从欢迎界面登录来修复错误,如果你只能通过 SSH 远程登录主机,Wooo~ 那就只能祝你好运了。

  • 相关阅读:
    《Ubuntu标准教程》学习总结
    Ubuntu下安装VirtualBox并为其添加USB支持
    Eclipse下配置TinyOS开发环境
    Ubuntu下的网络服务
    Ubuntu12.04添加环境变量
    Ubuntu12.04下搭建Java环境
    poj 1066 线段相交
    poj 2653 (线段相交判断)
    poj 2398 (叉积+二分)
    hdu 4883 思维题
  • 原文地址:https://www.cnblogs.com/ifantastic/p/3984810.html
Copyright © 2020-2023  润新知