ssh 使用 pubkey 免密登录仍要求输入密码的解决方法

使用 sshd 的免密登录,仍然要求用户输入密码,查看日志发现

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
journalctl --unit=sshd

Sep 09 21:39:35 nfqinxiansystem-1 sshd[32051]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=localhost  user=ops
Sep 09 21:39:38 nfqinxiansystem-1 sshd[32051]: Failed password for ops from 127.0.0.1 port 37386 ssh2
Sep 09 21:39:42 nfqinxiansystem-1 sshd[32051]: Accepted password for ops from 127.0.0.1 port 37386 ssh2
Sep 09 21:46:31 nfqinxiansystem-1 sshd[1328]: Connection closed by 127.0.0.1 port 37392 [preauth]
Sep 09 21:47:25 nfqinxiansystem-1 sshd[2084]: Authentication refused: bad ownership or modes for file /home/ops/.ssh/authorized_keys
Sep 09 21:47:26 nfqinxiansystem-1 sshd[2084]: Connection closed by 127.0.0.1 port 37394 [preauth]
Sep 09 21:47:40 nfqinxiansystem-1 sshd[2213]: Authentication refused: bad ownership or modes for file /home/ops/.ssh/authorized_keys
Sep 09 21:47:53 nfqinxiansystem-1 sshd[2213]: Connection closed by 192.168.1.99 port 58096 [preauth]
Sep 09 21:47:54 nfqinxiansystem-1 sshd[2408]: Accepted publickey for ops from 192.168.1.99 port 58098 ssh2: RSA SHA256:z7QGrcrMvuKMqjbq/qKQk6PGcb5PLEiOp81W6kq3Mpc
Sep 09 21:49:42 nfqinxiansystem-1 sshd[1580]: Received signal 15; terminating.
Sep 09 21:49:42 nfqinxiansystem-1 systemd[1]: Stopping OpenSSH server daemon...
Sep 09 21:49:42 nfqinxiansystem-1 systemd[1]: Stopped OpenSSH server daemon.
Sep 09 21:49:42 nfqinxiansystem-1 systemd[1]: Starting OpenSSH server daemon...
Sep 09 21:49:42 nfqinxiansystem-1 sshd[2986]: Server listening on 0.0.0.0 port 22.
Sep 09 21:49:42 nfqinxiansystem-1 sshd[2986]: Server listening on :: port 22.
Sep 09 21:49:42 nfqinxiansystem-1 systemd[1]: Started OpenSSH server daemon.
Sep 09 21:49:48 nfqinxiansystem-1 sshd[2791]: Connection closed by 127.0.0.1 port 37396 [preauth]
Sep 09 21:49:50 nfqinxiansystem-1 sshd[3015]: Connection closed by 127.0.0.1 port 37398 [preauth]
Sep 09 21:51:02 nfqinxiansystem-1 sshd[3269]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=localhost  user=ops

需要做以下几件事:

  • 开启 sshd 允许密钥登录模式

    1
    2
    3
    4
    
    vim /etc/ssh/sshd_config
    
    #PubkeyAuthentication yes
    PubkeyAuthentication yes
  • 修改 ~/.ssh 文件权限

    1
    2
    3
    
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/config
william 支付宝支付宝
william 微信微信
0%