osnosn

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

配置sshd_除了特定ip外_仅密钥登录_或证书登录

转载注明来源: 本文链接 来自osnosn的博客,写于 2021-01-17.

fail2ban 限制猜密码

  • 只要保留了,允许密码登录。就应该装上 fail2ban 来防止无限制(次数)猜密码。
    或者用防火墙规则,限制连接数。
    参考【Debian10_Centos8_fail2ban_sshd算法_限制连接数
  • 把 sshd 的端口从 22 改为一个不常用的端口。能大幅度减少被猜密码的频率。

更改 root 的登陆名

  • 比如,改 root 的登陆名为 admin 或 boss。
  • vipw -s把 root 的密码部分改为 *
  • 新建一个账号 admin,vipw 中修改 uid,gid 都为 0
    passwd admin 设置密码

为了安全,建议禁止root通过密码登录

  • 禁止 root 用户通过密码登录,仅能通过密钥登录。
    /etc/ssh/sshd_config 中,修改/加入一行
    PermitRootLogin prohibit-password或者PermitRootLogin without-password,
    重启sshd。
  • 允许特定 IP 可以用密码登录root。(即,密码登陆IP白名单)
    sshd_config文件的最后面,加入以下几行,重启sshd。
    # 在前面加上这一行。禁止 root 用户通过密码登录。
    PermitRootLogin prohibit-password
    # 禁止所有用户使用密码登录
    # PasswordAuthentication no
    
    # 允许从localhost,使用密码登录root
    # 允许从本地地址,本地链路地址,ULA地址,使用密码登录root
    # 本地地址,ULA 也可以写严格点,比如 192.168.1.0/24,fd55:1234:abcd::/48
    Match Address 127.0.0.1/32,::1/128,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,fe80::/16,fd00::/8
       PermitRootLogin yes
    #  PasswordAuthentication yes
    
  • 说明文档:
    Match 后面,
    • LocalAddress LocalPort – Specifies and match the the local (listen) address and port upon which the incoming connection was received via LocalAddress and LocalPort clauses. 匹配接受连接的,本地监听地址,监听端口。
    • Address - 匹配来源IP,客户端的IP。
  • 登录服务器 的途径/方法
    • 用 ssh 的 key 登录 root。
    • 用密码登录一个普通用户,再 su 到 root,这个时候可以使用 root密码。
    • 从特定的 IP 登录 root,可以使用密码。

为了更安全

  • 全局设置:
    设置 PasswordAuthentication no 禁止所有用户使用密码登录。
    设置 PermitRootLogin prohibit-password 禁止root用户, 密码登录。
  • 开放特定 IP 白名单,允许密码登录。使用 Match Address 192.168.2.0/24,fe80::/16,fd99:1234::/32 匹配,
    设置 PasswordAuthentication yes 允许所有用户使用密码登录。
    PermitRootLogin prohibit-password
    PasswordAuthentication no
    Match Address 127.0.0.1/32,::1/128,192.168.0.0/16,10.0.0.0/8,172.16.0.0/12,fe80::/16,fd00::/8
    #  PermitRootLogin yes
       PasswordAuthentication yes
    

关于AuthenticationMethods配置项

还有一种登录认证方法,用CA签名证书登录

参考

---end---


转载注明来源: 本文链接 https://www.cnblogs.com/osnosn/p/14288787.html 来自osnosn的博客.

posted on 2021-01-17 13:52  osnosn  阅读(1226)  评论(0编辑  收藏  举报