SSH-Keygen出现“没有这个文件或目录”

67
尝试为我的git生成公钥。使用Powershell。
PS>ssh-keygen -t rsa -b 4096 -C "my@emailaddress.com"
Generating public/private rsa key pair.
Enter file in which to save the key (//.ssh/id_rsa):
Could not create directory '//.ssh': Read-only file system
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Saving key "//.ssh/id_rsa" failed: No such file or directory

如果我指定文件位置并运行

ssh -vT git@github.com

它不会检查自定义位置以使用公钥。
OpenSSH_7.1p1, OpenSSL 1.0.2d 9 Jul 2015
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [<<ANIPADDRESS>>] port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.1
debug1: Remote protocol version 2.0, remote software version libssh-0.7.0
debug1: no match: libssh-0.7.0
debug1: Authenticating to github.com:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client chacha20-poly1305@openssh.com <implicit> none
debug1: kex: client->server chacha20-poly1305@openssh.com <implicit> none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:<<SCAREDTOPUBLISH>>
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /.ssh/known_hosts:1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /.ssh/id_rsa
debug1: Trying private key: /.ssh/id_dsa
debug1: Trying private key: /.ssh/id_ecdsa
debug1: Trying private key: /.ssh/id_ed25519
debug1: No more authentication methods to try.
Permission denied (publickey).
7个回答

93
命令无法保存您的密钥,可能是因为它无法确定您的$HOME目录。请指定一个文件,在您拥有写访问权限的位置:
ssh-keygen -t rsa -b 4096 -C "my@emailaddress.com" -f /path/to/key

这将把你的私钥保存在/path/to/key,公钥保存在/path/to/key.pub。 成功时,你会看到如下信息,而不是错误提示:

Your identification has been saved in /path/to/key.
Your public key has been saved in /path/to/key.pub.
The key fingerprint is:
76:f7:82:04:1e:64:eb:9c:df:dc:0a:6b:26:73:1b:2c
The key's randomart image is:
+--[ RSA 2048]----+
|        o        |
|       o .       |
|        +        |
|       + +       |
|        S o .    |
|       . = = o   |
|        E * + o  |
|        o.++ o   |
|         *o..    |
+-----------------+

然后,为了让ssh在自定义位置查找文件,使用-i标志:

ssh -i /path/to/key -vT git@github.com

或者,如果您正在运行身份验证代理,则可以使用以下命令将密钥添加到代理中:

ssh-add /path/to/key

一旦您的密钥被代理存储,您只需要执行以下操作:

ssh -T git@github.com

响应应该长成这个样子:
Hi USER! You've successfully authenticated, but GitHub does not provide shell access.

你可以使用以下命令克隆您的代码库:

git clone git@github.com:USER/REPO

8
我遇到了一个错误:“权限拒绝公钥”。你漏掉了一步,需要登录到你的Github页面并添加id_rsa.pub密钥! - IgorGanapolsky
1
@IgorGanapolsky 我不明白这与本问题有何关联。我建议您提出一个新问题,并确保包括所有必要的细节,例如您运行的命令及其完整输出。祝好运。 - janos
4
这个问题特别涉及使用ssh-keygen命令时出现“无此文件或目录”的问题。它并不是关于使用GitHub进行身份验证的教程,针对这个问题还有许多其他答案,以及GitHub提供的优秀文档。 - janos
当它寻找identity file /home/yuri/.ssh/id_rsa-cert type -1时,我会收到一个key_load_public: No such file or directory错误。 - yuriploc
如果在Linux下工作:当您从文件浏览器(例如Nautilus)复制和粘贴路径时,“file://”会附加到路径的开头。这就是路径找不到的原因。 - TheTrowser
7
我曾尝试将相对于我的主目录的路径(例如~/.ssh/foo)传递给ssh-keygen,认为用户会被展开为当前用户,但似乎不支持这样做。将路径更改为绝对路径(如/Users/myuser/.ssh/foo)可以正常工作。 - Taylor D. Edmiston

10

在我的电脑上,使用cmd时,ssh-keygen命令似乎会失败:

您的身份标识已保存在[...]/.ssh/id_rsa中。 fdopen [...]/.ssh/id_rsa.pub 失败:没有那个文件或目录

私钥生成了,但公钥文件大小为0字节。

如果我在Windows的Git Bash中运行该命令,则可以正常工作。


9
这个对我有效:ssh-add '/home/user/.ssh/id_rsa'

请为我节省一天时间,非常感谢。 - meowww

3

我尝试了这个代替 ~/.ssh/whatever_file_name

这段代码对我有效

ssh-add /Users/<user>/.ssh/id_ed25519

<user>替换为您的用户文件夹名称...


1

您遇到了错误:Saving key "//.ssh/id_rsa" failed: No such file or directory

看起来您需要创建文件夹.ssh

请尝试运行mkdir .ssh,然后再次运行ssh-keygen -t rsa


-1

在我的 Mac 上适用: ssh-add /Users/your_user_name/.ssh/id_rsa


-1

对我来说,用户名是错误的 :) 真不敢相信这么简单 :(


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接