Index / Blog

第一次通过 SSH key 免密连接 GitHub 的完整过程

原文:https://ichochy.com/posts/blog/20221107.html

通过密码连接GitHub总是要输入密码,麻烦,现在使用 SSH key 可以轻松实现免密验证.

创建 SSH key

使用 ssh-keygen 命令

ssh-keygen -t ed25519 -C "[email protected]" 

注意:将[email protected]换成自己的邮箱。

Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/mleo/.ssh/id_ed25519): 
Created directory '/Users/mleo/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

注意:可以输入空和密码

Your identification has been saved in /Users/mleo/.ssh/id_ed25519
Your public key has been saved in /Users/mleo/.ssh/id_ed25519.pub

配置 SSH key

touch ~/.ssh/config
Host github.com                           # 别名
	Hostname 	github.com                # GitHub 主机地址
	Port 		22                        # GitHub 主机端口
	User 		git                       # GitHub 主机用户
	Identityfile ~/.ssh/id_ed25519        # ssh-key
iChochy(用户) => Settings => SSH and GPG keys => new SSH key

key

测试 SSH key

ssh -T [email protected]
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? 
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi iChochy! You've successfully authenticated, but GitHub does not provide shell access.

使用 SSH key

git clone [email protected]:iChochy/iChochy.git 	# 克隆远程仓库 
Enumerating objects: 405, done.
Counting objects: 100% (405/405), done.
Delta compression using up to 8 threads
Compressing objects: 100% (205/205), done.
Writing objects: 100% (206/206), 27.81 KiB | 6.95 MiB/s, done.
Total 206 (delta 199), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (199/199), completed with 194 local objects.
To github.com:iChochy/iChochy.git
   1d44cdb..bde62c3  master -> master