![SSH 公钥不会发送到服务器](https://linux22.com/image/209636/SSH%20%E5%85%AC%E9%92%A5%E4%B8%8D%E4%BC%9A%E5%8F%91%E9%80%81%E5%88%B0%E6%9C%8D%E5%8A%A1%E5%99%A8.png)
我浏览了很多网站都没有效果。所以我需要看看其他人是否能看到我做错了什么。
我使用的是 ubuntu 18。这是 ssh -v 输出:
debug1: Found key in /home/leithner/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:5nOs3DjENMsvwbU2jHNE2GOv6u/1L5TyFCgc3zPYNDM /home/leithner/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/leithner/.ssh/id_dsa
debug1: Trying private key: /home/leithner/.ssh/id_ecdsa
debug1: Trying private key: /home/leithner/.ssh/id_ed25519
debug1: Next authentication method: password
ssh with port output:
Host '[elm.pa...]:53999' is known and matches the ECDSA host key.
debug1: Found key in /home/leithner/.ssh/known_hosts:1
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:5nOs3DjENMsvwbU2jHNE2GOv6u/1L5TyFCgc3zPYNDM /home/leithner/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/leithner/.ssh/id_dsa
debug1: Trying private key: /home/leithner/.ssh/id_ecdsa
debug1: Trying private key: /home/leithner/.ssh/id_ed25519
debug1: Next authentication method: password
From /var/log/auth.log file:
Aug 19 14:44:55 birch sshd[22485]: Accepted publickey for leithner from 129.244.22.126 port 57912 ssh2: RSA SHA256:qLTekbB8a22YWHqRHPZjVSJ0dNEHTSoI3nFN97PEyuc
Aug 19 14:44:55 birch sshd[22485]: pam_unix(sshd:session): session opened for user leithner by (uid=0)
Aug 19 14:44:55 birch systemd-logind[1059]: New session 38875 of user leithner.
Aug 19 14:45:01 birch CRON[22582]: pam_unix(cron:session): session opened for user root by (uid=0)
Aug 19 14:43:44 birch sshd[22367]: pam_unix(sshd:session): session closed for user leithner
Aug 19 14:44:55 birch sshd[22485]: Accepted publickey for leithner from 129.244.22.126 port 57912 ssh2: RSA SHA256:qLTekbB8a22YWHqRHPZjVSJ0dNEHTSoI3nFN97PEyuc
Aug 19 14:44:55 birch sshd[22485]: pam_unix(sshd:session): session opened for user leithner by (uid=0)
Aug 19 14:44:55 birch systemd-logind[1059]: New session 38875 of user leithner.
Aug 19 14:45:01 birch CRON[22582]: pam_unix(cron:session): session opened for user root by (uid=0)
Aug 19 14:45:01 birch CRON[22582]: pam_unix(cron:session): session closed for user root
Aug 19 14:47:25 birch sshd[22567]: Received disconnect from 129.244.22.126 port 57912:11: disconnected by user
.ssh directory
directory permissions are drwx------ 2 leithner users 4096 Aug 19 15:27 .ssh
leithner@birch:~/.ssh$ ls -l
total 20
-rw------- 1 leithner users 394 Aug 19 14:43 authorized_keys
-rw------- 1 leithner users 26 Aug 19 15:23 config
-rw------- 1 leithner users 1675 Aug 19 15:26 id_rsa
-rw------- 1 leithner users 396 Aug 19 15:26 id_rsa.pub
-rw------- 1 leithner users 888 Aug 19 15:28 known_hosts
.ssh directory on server
directory permissions on server are: drwx------ 2 leithner ppa 4096 Aug 19 14:27 .ssh
leithner@elm:~/.ssh$ ls -l
total 20
-rw------- 1 leithner ppa 396 Aug 19 14:27 authorized_keys
-rw------- 1 leithner ppa 85 Aug 19 14:23 config
-rw------- 1 leithner ppa 1823 Aug 19 13:36 id_rsa
-rw------- 1 leithner ppa 394 Aug 19 13:36 id_rsa.pub
-rw------- 1 leithner ppa 1992 Aug 19 14:20 known_hosts
我使用 ssh-keygen 创建密钥,并使用 ssh-copy-id leithner@elm 将它们复制到服务器
ssh 请求密码而不是使用密钥。
ssh -p 53997 elm
leithner@elm's password:
这是服务器上的 auth.log:
8月22日11:59:03 elm sshd[2081934]:身份验证被拒绝:目录/home/leithner的所有权或模式错误8月22日11:59:12 elm sshd[2081934]:接受来自129.244.22.120端口43340 ssh2的leithner密码
我的目录应该有什么权限:
已解决:问题是我的主目录是 775,它需要是 755。 drwxr-xr-x 24 leithner ppa 4096 Aug 22 12:55 leithner
任何想法将不胜感激,我已经多次重做钥匙。
答案1
我唯一能想到的是
权限(其中大多数似乎是正确的)。
唯一看起来有问题的是
*.pub
钥匙。可以肯定的是,您可以chmod
再次打开文件夹和密钥。另外,stat --format '%a' <dir or file>
还会为您提供 的数值chmod
。这样您就可以查看文件夹和文件的权限是否正确。请注意,
chmod
以下权限(正确的)在主机和来宾上必须相同。
chmod 700 $HOME/.ssh
chmod 600 $HOME/.ssh/id_rsa
chmod 644 $HOME/.ssh/id_rsa.pub
chmod 600 $HOME/.ssh/authorized_keys # yes, this one needs specific permissions, too
复制方法错误
按照您的方式应该已经足够好了,但最好使用 . 将公钥复制到主机。请注意,要使其正常工作,权限必须正确,否则每次都会失败/提示输入密码。
ssh-copy-id -i .ssh/id_rsa.pub [email protected]
请发布不带该标志的 SSH 输出-v
。这可能会让我们更好地了解正在发生的事情。