我在M1 Macbook Pro 16上"如果这些信息很重要的话。
这是我做过的事情的清单!之后!删除配置文件的内容。
$ ssh -v localhost
OpenSSH_8.6p1, LibreSSL 3.3.6
我使用推荐的类型创建了一个简单的ssh文件:
$ ssh-keygen -t ed25519 -C "my.actual.email@whatever.com" -f github-personal
... password, whatever
$ ls -la
.
..
bkup
config
github-personal
github-personal.pub
known_hosts
然后我将公钥添加到我的账户
$ cat ~/.ssh/github-personal.pub | pbcopy
转到我的帐户,设置,SSH和GPG,添加密钥,给它一个相关的名称。
$ ssh-add -l
The agent has no identities.
很好,正如预期的
$ ssh-add ~/.ssh/github-personal
password:...
$ ssh-add -l
256 SHA256: ... my.email@whatever
将签名与github上的签名进行比较,是的,它是一样的,一切都正常。
$ ssh -T git@github.com
Hi [my-name]! You've successfully authenticated, but GitHub does not provide shell access.
$ git clone git@github.com:my-user/my-repo
cloning into ... whatever it works
不错!最低限度的工作!现在,让我们尝试拥有3个github帐户,每个帐户都有自己的SSH密钥。但是太可怕了。在添加其他帐户之前,让我们先用配置文件处理上面的内容?
contents of ~/.ssh/config:
AddKeysToAgent yes
IdentitiesOnly yes
Host personal-github
HostName github.com
User git # as instructed by git, only ever use the git user i.e. git@github.com
UseKeychain yes
IdentityFile ~/.ssh/github-personal
# PreferredAuthentications publickey,password
我不知道PreferredAuthentications publickey,password
是不是把我搞砸了,我试过有没有它。现在没有了。
$ ssh-add -l
yes, agent still has it, it's listed here
$ ssh -T git@github.com
git@github.com: Permission denied (publickey).
$ ssh-add -D
$ ssh-add -l
key not longer here
$ ssh personal-github
PTY allocation request failed on channel 0
Hi my-name! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
$ ssh-add -l
yep, key was added automatically
所以,也许我错过了什么。我理解以下内容:我在配置文件中定义了一个Host name-defined-by-me
。这会启动某种函数,或者其他什么函数,命名空间,不在乎。在那里我可以继续定义参数,直到下一个Host name-defined-by-me-2
出现。
然后我调用那个name-defined-by-me
来加载那个特定的配置。
在我的情况下,让我们再次查看文件:
AddKeysToAgent yes
IdentitiesOnly yes
Host personal-github
HostName github.com
User git # as instructed by git, only ever use the git user i.e. git@github.com
UseKeychain yes
IdentityFile ~/.ssh/github-personal
# PreferredAuthentications publickey,password
我已经在全球范围内定义了我想要的:
- 自动向代理添加密钥
- 但仅特定标识的密钥,而不是~/.ssh中的所有密钥/
- 而且——我想——只有当我打电话给
ssh name-defined-by-me
或这里的ssh personal-github
时
在personal-github
的特定主机部分中,我说我试图连接的主机名是github.com,用户git
构成git@github.com
。
我想使用我的mac密钥链,而不是每次更换主机时都提供密码。(在主机更改之间,我可能需要$ ssh-add -D
(。我正在指定要添加的单个文件。
当我调用$ssh personal-github
时,它确实添加了文件,它不会询问我的密码,github会用我的名字回应,所以一定有什么是正确的。
然而,我不能继续超过这一点。
$ ssh -T git@github.com
git@github.com: Permission denied (publickey).
$ rm -rf my-repo
$ git clone git@github.com:my-user-name/my-repo
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.
我不知所措。
ssh -T git@github.com
git@github.com: Permission denied (publickey).
这是意料之中的事
每当您在~/.ssh/config文件中的条目Host key1
下引用私钥时,都需要将URL更改为
ssh -Tv key1
git clone key1:me/myRepository
你可以对key2
,key3
。。。