我正在使用 WSL(适用于 Linux 的 Windows 子系统),并且在运行以下命令时无法识别 GitHub 中已为我的本地计算机配置的公钥:
git clone [email protected]:<my_username>/<repo_name>.git
我收到权限被拒绝的消息:
Cloning into 'CV'...
[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
存储库存在并且密钥的存储方式类似于
~/.ssh/id_<custom_name>.pub
。
我期望使用已经生成的 ssh 密钥和在 Github 中配置的 ssh 连接可以将我的存储库克隆到我的本地系统中。
我找到了一种一致且可维护的方法来处理存储在
~/.ssh/
目录下的多个自定义 ssh 密钥。
第 1 步 [必需]: 我假设您已经生成了 ssh 密钥(公共和私有):
~/.ssh/id_myKey_ed25519
~/.ssh/id_myKey_ed25519.pub
(如果没有密钥,请尝试向您的 GitHub 帐户添加新的 SSH 密钥)
Step2 [~/ssh/config]:我们需要确保
~/.ssh/config
已经存在。如果没有,请创建并编辑一个(假设您可以创建并编辑 ~/.ssh/config
文件。):
vim ~/.ssh/config
在
config
文件中,您可以在 identityFile
属性中使用所需的密钥显式定义 ssh 连接:
Host github_account
User git
HostName github.com
PreferredAuthentications publickey
identityFile ~/.ssh/id_myKey_ed25519
(注意: 请记住这里的用户不是您的 github 用户名。*)
第三步【验证】:验证配置,运行:
ssh github_account
您应该得到如下类似的输出(当询问时输入您
passphrase
):
> Enter passphrase for key '/home/<user>/.ssh/id_myKey_ed25519':
> Hi <github_username>! You've successfully authenticated, but GitHub does not provide shell access.
恭喜,您已成功管理您的 ssh 密钥并为每个主机配置了使用情况。