window下配置ssh
2024年7月2日大约 2 分钟
背景
在window下使用git,我习惯使用ssh方式进行拉取推送代码,其他方式流程就没有这么繁琐了。为什么使用SSH方式呢,我不想每次切换不同账号的时候重复输入密码,像github不支持直接输入密码,建议使用ssh方式。gitlab、云效等支持输入访问令牌方式连接远程仓库。
ssh配置
配置ssh cofnig文件
在C:\Users\catcat\.ssh目录下创建 config 文件,并自由添加需要的配置
这里的catcat表示window电脑对应的用户名,每个电脑的用户名都是自己设置的,自行修改即可。
# GitHub
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_github_mytest
    User mytest
    
# Github2
Host imtest.github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_github_imtest
    User imtest    
# gitee
Host gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_gitee_mytest
    User mytest    	
# gitee
Host imtest.gitee.com
    HostName gitee.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_gitee_imtest
    User imtest 	
	
# gitee
Host gitcode.net
    HostName gitcode.net
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_gitcode_nxgtest
    User test为什么配置两个github呢,我是想分开两个账号的公钥,虽然可以共用一个公钥,但是我还是习惯于分开。
生成秘钥
在目录下执行:
位置:C:\Users\catcat\.ssh
ssh-keygen -t ed25519 -C "<注释内容>"推荐使用ed25519算法方式生成秘钥
生成效果:
 catcat    .ssh     0ms⠀   ssh-keygen -t ed25519 -C "[email protected]"             pwsh   96  15:48:07 
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\catcat/.ssh/id_ed25519):输入自定义的名称,并回车
 catcat    .ssh     0ms⠀   ssh-keygen -t ed25519 -C "[email protected]"             pwsh   96  15:48:07 
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\catcat/.ssh/id_ed25519): id_ed25519_github_mytest
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_ed25519_github_mytest
Your public key has been saved in id_ed25519_github_mytest.pub
The key fingerprint is:
SHA256:FREWfhgLoEWqn21ewQWE65s+MGFoamjj0CWQvHG124 [email protected]
The key's randomart image is:
+--[ED25519 256]--+
|+.    .. .+      |
|o. . .  ..o+     |
| .. .    oo+     |
|.  . .   .+ .    |
|.o+ o o S  .     |
|+o *o*           |
|+ =**..          |
|=o+++o.          |
|B*o.+E.          |
+----[SHA256]-----+配置远程仓库SSH秘钥
将id_ed25519_github_mytest.pub文件里里的秘钥复制粘贴到 SSH秘钥位置,具体细节就不展开了。
配置全局git提交邮箱和用户信息
在C:\Users\catcat目录下增加 .gitconfig 文件,并添加以下配置:
[user]
	email = [email protected]
	name = mytest附件
详细操作看这里配置 SSH 密钥