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

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


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

创建 SSH key

使用 ssh-keygen 命令

  • 打开终端输入命令 ssh-keygen 创建 key
ssh-keygen -t ed25519 -C "ichochy@outlook.com" 

注意:将ichochy@outlook.com换成自己的邮箱。

  • 命令构建ed25519 key,并自动保存到.ssh目录
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/mleo/.ssh/id_ed25519): 
Created directory '/Users/mleo/.ssh'.
  • 提示输入key的密码
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

注意:可以输入空和密码

  • 提示key创建成功,并保存在.ssh目录
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
  • 配置文件写入GitHub主机信息
Host github.com                           # 别名
	Hostname 	github.com                # GitHub 主机地址
	Port 		22                        # GitHub 主机端口
	User 		git                       # GitHub 主机用户
	Identityfile ~/.ssh/id_ed25519        # ssh-key
  • GitHub帐号中添加公钥key
iChochy(用户) => Settings => SSH and GPG keys => new SSH key

key

测试 SSH key

  • 测试 GitHub SSH 连接
ssh -T git@github.com
  • 确认是否连接
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])? 
  • 输入 yes确认连接
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

  • 通过 SSH 地址克隆远程仓库
git clone git@github.com:iChochy/iChochy.git 	# 克隆远程仓库 
  • 通过 SSH 免密提交代码
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
posted @ 2024-01-27 20:47  iChochy  阅读(380)  评论(0编辑  收藏  举报