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

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

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

fail2ban 限制猜密码

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

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

  • 禁止 root 用户通过密码登录,仅能通过密钥登录。
    /etc/ssh/sshd_config 中,修改/加入一行
    PermitRootLogin without-password或者PermitRootLogin prohibit-password,
    重启sshd。
  • 允许特定 IP 可以用密码登录root。(即,密码登陆IP白名单)
    sshd_config文件的最后面,加入以下几行,重启sshd。
# 允许从localhost,使用密码登录root
Match LocalAddress 127.0.0.1/32,::1/128
   PermitRootLogin yes
# 允许从本地地址,本地链路地址,ULA地址,使用密码登录root
# 本地地址,ULA 也可以写严格点,比如 192.168.1.0/24,fd55:1234:abcd::/48
Match Address 192.168.0.0/16,fe80::/64,fd00::/8
   PermitRootLogin yes
  • 登录服务器 的途径/方法
    • 用 ssh 的 key 登录 root。
      • linux中用ssh-keygen -t ed25519 -C 'myserverKey' -f mykeyfile 生成密钥对。
        mykeyfile.pub 文件中的内容,加入到服务器 /root/.ssh/authorized_keys 文件中即可。
        记得要 chmod 600 /root/.ssh/authorized_keys, 否则 sshd 不认。
        参考【SSH配置—Linux下实现免密码登录】(ed25519比rsa更快,更小,且不失安全性)
      • windows中,用 PUTTYGEN.exe 去生成密钥对。用 putty.exe 登录。
        参考【Putty使用密钥登陆SSH
    • 用密码登录一个普通用户,再 su 到 root,这个时候可以使用 root密码。
    • 从特定的 IP 登录 root,可以使用密码。

为了更安全

  • 全局设置:
    设置 PasswordAuthentication no 禁止所有用户使用密码登录。
    设置 PermitRootLogin no 禁止root用户登录。
  • 开放特定 IP 白名单,允许密码登录。使用 Match Address 192.168.2.0/24,fe80::/64,fd99:1234::/32 匹配,
    设置 PasswordAuthentication yes 允许所有用户使用密码登录。
    设置 PermitRootLogin yes 允许root用户登录。

关于AuthenticationMethods配置项

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


参考:
How to allow root login from one IP address with ssh public keys only
SSH密码登陆IP白名单
Best way to restrict some SSH users to publickey authentication only (disable password authentication)
限制某些SSH用户仅使用publickey身份验证的最佳方法(禁用密码身份验证)


转载注明来源: 本文链接 来自osnosn的博客.

原文地址:https://www.cnblogs.com/osnosn/p/14288787.html