使用ssh连接GitHub上的git服务器

2020/02/03 git 共 1485 字,约 5 分钟

之前在linux系统上登录GitHub都是用用户和密码登录的,现在打算用ssh登录GitHub,这样更安全,也不用输密码。

先配置git的用户名和邮箱:

git config --global user.name "your_name"
git config --global user.email "your_email@xxx.com"

先查看本地是否已经配置了公钥和私钥,如果已经存在私钥,无需再次生成。

cd ~/.ssh

生成密钥:

-> % ssh-keygen -t rsa -b 4096 -C your_email@xxx.com
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/ubuntu/.ssh/id_rsa.
Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:14HZgHvg4z5qnFwlcrUeZ7AzxOpp+cka9oEMPrr04YQ xxx@gmail.com
The key's randomart image is:
+---[RSA 4096]----+
|         o.      |
|        o ==     |
|       . *o+o    |
|      . B O.o.   |
|      .=SO.*.    |
|     o oBo.      |
|    Eo===o..     |
|   . ==+oo+.     |
|    oo+..o.      |
+----[SHA256]-----+

生成密钥后拷贝公钥id_rsa.pub的内容,到GitHub上新建ssh的key:GitHub SSH and GPG keys

新建完成后就可以登录GitHub了:

-> % ssh -T git@github.com
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

现在可以用ssh进行clone仓库的操作了:

-> % git clone git@github.com:xxx/xxx.github.io.git
Cloning into 'xxx.github.io'...
remote: Enumerating objects: 35, done.
remote: Counting objects: 100% (35/35), done.
remote: Compressing objects: 100% (28/28), done.
Receiving objects: 100% (6964/6964), 10.49 MiB | 20.00 KiB/s, done.
Resolving deltas: 100% (2129/2129), done.

文档信息

Table of Contents