github配置SSH登录
全局基本配置
$ git config --global user.name "bytetoy"
$ git config --global user.email "12620716@qq.com"
$ git config -l
init.defaultbranch=master
user.email=12620716@qq.com
user.name=bytetoy
生成SSH密钥
我的机器上已经有其他SSH密钥了,因此需要指定密钥的名字 而且不建议对密钥设置密码。
$ ssh-keygen -t rsa -C "12620716@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/hotpod/.ssh/id_rsa): github_bytetoy
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in github_bytetoy
Your public key has been saved in github_bytetoy.pub
The key fingerprint is:
SHA256:lMTl+riE1X0uA+nfC8h/0Z1FDLPhMgUH7J89KaKQMdI 12620716@qq.com
The key's randomart image is:
+---[RSA 3072]----+
| .....oo*o |
| ..o .+ +o|
| . o ..o o. |
| . E o o.o .|
| . S + ...+=|
| = = + ++++|
| . + * = o..|
| . o o =. |
| . o.o. |
+----[SHA256]-----+
配置git密钥
由于我的此目录下有好几个SSH密钥对,需要指定使用刚才生成的密钥对。
- 在github上配置公钥:Settings --> SSH and GPG keys --> SSH Keys --> New SSH keys,将公钥
github_bytetoy.pub
内容填入key文本框。- 在本地配置私钥:在
~/.ssh
目录下,新建一个config
文件,指定ssh的私钥,如下:
# github_bytetoy 私钥
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_bytetoy
测试
默认情况下,只是连接测试成功,但是无法push和pull等动作
$ ssh -T git@github.com
Hi bytetoy! You've successfully authenticated, but GitHub does not provide shell access.
配置git的ssh登录
默认情况下,提交和拉取,仍然需要输入帐号和密码,非常不方便。通过以下配置,有密钥配置好了,可以直接push和pull。 将
bytetoy/bytetoy.git
修改自己的github帐号和仓库
$ git remote set-url origin git@github.com:bytetoy/bytetoy.git