zoukankan      html  css  js  c++  java
  • [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~ 那就只能祝你好运了。

  • 相关阅读:
    【BZOJ】2453: 维护队列【BZOJ】2120: 数颜色 二分+分块(暴力能A)
    【转】使用json-lib进行Java和JSON之间的转换
    【转】MySQL索引和查询优化
    【转】SQL常用的语句和函数
    【转】MySQL日期时间函数大全
    VMare中安装“功能增强工具”,实现CentOS5.5与win7host共享文件夹的创建
    MyEclipse中消除frame引起的“the file XXX can not be found.Please check the location and try again.”的错误
    由于SSH配置文件的不匹配,导致的Permission denied (publickey)及其解决方法。
    复选框的多选获取值
    echart环形图制作及出现的一些问题总结
  • 原文地址:https://www.cnblogs.com/ifantastic/p/3984810.html
Copyright © 2011-2022 走看看