January 19,2022
第一次使用git命令克隆代码的时候,会显示
wangsisideMacBook:~ wangsisi$ git clone git@github.com:hustcc/echarts-for-react.git
Cloning into 'echarts-for-react'...
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 keys
SSH是Secure Shell Protocol的缩写,该协议通过不安全的网络提供安全通道。
使用 SSH 协议可以连接远程服务器和服务并向它们验证。 利用 SSH 密钥可以连接 GitHub,而无需在每次访问时都提供用户名和个人访问令牌。
先查看是否有现有的 SSH 密钥。
ls -al ~/.ssh
wangsisideMacBook:~ wangsisi$ ls -al ~/.ssh
total 8
drwx------ 3 wangsisi staff 96 1 19 10:31 .
drwxr-xr-x+ 42 wangsisi staff 1344 1 19 10:31 ..
-rw-r--r-- 1 wangsisi staff 187 1 19 10:31 known_hosts
wangsisideMacBook:~ wangsisi$
id_rsa.pub
id_ecdsa.pub
id_ed25519.pub
显然我没有。
命令行输入ssh-keygen -t ed25519 -C "sh*.me@gmail.com"
sh*.me@gmail.com
替换成自己github的邮件地址
在Enter passphrase (empty for no passphrase):
这一步输入一个密码
wangsisideMacBook:~ wangsisi$ ssh-keygen -t ed25519 -C "sh***.me@gmail.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/wangsisi/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/wangsisi/.ssh/id_ed25519.
Your public key has been saved in /Users/wangsisi/.ssh/id_ed25519.pub.
再执行ls -al ~/.ssh
看到已经添加成功了
wangsisideMacBook:~ wangsisi$ ls -al ~/.ssh
total 40
drwx------ 6 wangsisi staff 192 1 19 11:08 .
drwxr-xr-x+ 42 wangsisi staff 1344 1 19 10:31 ..
-rw-r--r--@ 1 wangsisi staff 6148 1 19 11:08 .DS_Store
-rw------- 1 wangsisi staff 464 1 19 11:08 id_ed25519
-rw-r--r-- 1 wangsisi staff 100 1 19 11:08 id_ed25519.pub
-rw-r--r-- 1 wangsisi staff 187 1 19 10:31 known_hosts
如果生成ssh的时候忘记添加密码了,可以用以下的方式添加密码。修改密码也是这种方式。
$ ssh-keygen -p -f ~/.ssh/id_ed25519
> Enter old passphrase: [Type old passphrase]
> Key has comment 'your_email@example.com'
> Enter new passphrase (empty for no passphrase): [Type new passphrase]
> Enter same passphrase again: [Repeat the new passphrase]
> Your identification has been saved with the new passphrase.
eval "$(ssh-agent -s)"
wangsisideMacBook:~ wangsisi$ eval "$(ssh-agent -s)"
Agent pid 1843
先查看配置文件 open ~/.ssh/config
显示不存在,使用touch ~/.ssh/config
创建文件,再打开
wangsisideMacBook:~ wangsisi$ open ~/.ssh/config
The file /Users/wangsisi/.ssh/config does not exist.
wangsisideMacBook:~ wangsisi$ touch ~/.ssh/config
wangsisideMacBook:~ wangsisi$ open ~/.ssh/config
在文件中添加以下内容
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
UseKeychain yes
如果生成SSH的时候没有添加密码,这行不用写。
IdentityFile ~/.ssh/id_ed25519
是你自己要添加的私钥文件地址。
终端输入 ssh-add -K ~/.ssh/id_ed25519
再输入密码
添加成功
wangsisideMacBook:~ wangsisi$ ssh-add -K ~/.ssh/id_ed25519
Enter passphrase for /Users/wangsisi/.ssh/id_ed25519:
Identity added: /Users/wangsisi/.ssh/id_ed25519 (shisi.me@gmail.com)
登录github
将拷贝的公钥黏贴在此,title是为新密钥添加描述性标签。
点击Add SSH key 添加成功。
wangsisideMacBook:~ wangsisi$ git clone git@github.com:hustcc/echarts-for-react.git
Cloning into 'echarts-for-react'...
remote: Enumerating objects: 1414, done.
remote: Counting objects: 100% (70/70), done.
remote: Compressing objects: 100% (39/39), done.
remote: Total 1414 (delta 27), reused 44 (delta 19), pack-reused 1344
Receiving objects: 100% (1414/1414), 8.31 MiB | 3.36 MiB/s, done.
Resolving deltas: 100% (728/728), done.
成功!
——————————————
参考:
https://docs.github.com/cn/authentication/connecting-to-github-with-ssh/about-ssh