自定义路径保存SSH密钥无法连接GitHub的问题求助
问题
当在非默认路径/名称生成SSH密钥时,无法通过SSH连接GitHub。多次尝试使用自定义名称的密钥均失败,但默认路径/名称生成的密钥可以正常工作,具体操作如下:
正常工作的操作
PS C:\Users\sjoao\.ssh> ssh-keygen -t ed25519 -C "sjoaovictor622@gmail.com" Generating public/private ed25519 key pair. Enter file in which to save the key (C:\Users\sjoao/.ssh/id_ed25519): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in C:\Users\sjoao/.ssh/id_ed25519 Your public key has been saved in C:\Users\sjoao/.ssh/id_ed25519.pub The key fingerprint is: SHA256:sBpycUHR17hJwxtmDZFJgZSiNP9XBdlBP41AKQIEc5w sjoaovictor622@gmail.com The key's randomart image is: +--[ED25519 256]--+ | o=B*.==@+*o. | | ooE.= & =.oo.| | ..+o. * B ...o| | .o.o + . .| | . o ..S . | | o o . . | | . . | | | | | +----[SHA256]-----+ PS C:\Users\sjoao\.ssh> Start-Service ssh-agent PS C:\Users\sjoao\.ssh> ssh-add .\id_ed25519 Identity added: .\id_ed25519 (sjoaovictor622@gmail.com) PS C:\Users\sjoao\.ssh> Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard PS C:\Users\sjoao\.ssh> git clone git@github.com:jottaVictor/tictactoegame.git Cloning into 'tictactoegame'... The authenticity of host 'github.com (20.201.28.151)' can't be established. ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github.com' (ED25519) to the list of known hosts. remote: Enumerating objects: 135, done. remote: Counting objects: 100% (135/135), done. remote: Compressing objects: 100% (93/93), done. remote: Total 135 (delta 47), reused 121 (delta 33), pack-reused 0 (from 0) Receiving objects: 100% (135/135), 45.16 KiB | 388.00 KiB/s, done. Resolving deltas: 100% (47/47), done.
无法工作的操作
PS C:\Users\sjoao\.ssh> ssh-keygen -t ed25519 -C "sjoaovictor622@gmail.com" Generating public/private ed25519 key pair. Enter file in which to save the key (C:\Users\sjoao/.ssh/id_ed25519): .\test Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in .\test Your public key has been saved in .\test.pub The key fingerprint is: SHA256:pOwOkkTVPPMaNn01y6VVmp1+B2P0MQgsfpDeq0KV7pM sjoaovictor622@gmail.com The key's randomart image is: +--[ED25519 256]--+ | .o o.. o+.| | . = + .ooo=+| | . =+ =o **.o| | . .+oo=.o+..o | | . .o+S.. . .o| | . . ... . . o| | o . o . o | | . o . E | | . . . | +----[SHA256]-----+ PS C:\Users\sjoao\.ssh> Start-Service ssh-agent PS C:\Users\sjoao\.ssh> ssh-add .\test Identity added: .\test (sjoaovictor622@gmail.com) PS C:\Users\sjoao\.ssh> Get-Content $env:USERPROFILE\.ssh\test.pub | Set-Clipboard PS C:\Users\sjoao\.ssh> git clone git@github.com:jottaVictor/tictactoegame.git Cloning into 'tictactoegame'... The authenticity of host 'github.com (20.201.28.151)' can't be established. ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. This key is not known by any other names. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'github.com' (ED25519) to the list of known hosts. git@github.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
操作的唯一区别是密钥保存名称,且两个公钥均已添加到GitHub的SSH密钥列表中,请问问题出在哪里?
问题原因与解决方法
核心原因:SSH客户端默认只会在
~/.ssh/目录下查找id_rsa、id_ed25519这类默认命名的密钥文件,自定义名称的密钥不会被自动识别——哪怕你用ssh-add把它加入了agent,SSH连接GitHub时也不会主动匹配这个密钥。解决方法:
- 在
~/.ssh/目录下创建一个无扩展名的config文件,添加以下内容:Host github.com IdentityFile ~/.ssh/test # 替换为你的自定义密钥路径 IdentitiesOnly yes - 保存文件后,重新执行
git clone操作即可正常连接。
- 在
补充说明:
IdentitiesOnly yes用于强制SSH仅使用指定的密钥,避免agent尝试其他无关密钥导致验证冲突。- 若后续需要用不同自定义密钥连接不同主机,可在
config文件中添加多个Host块分别配置。
内容的提问来源于stack exchange,提问作者Victor Santos
相关产品推荐
相关产品推荐

