Git多个SSH KEYS解决方案(含windows自动化、TortoiseGit、SourceTree等)

简介: 工作过程中,经常会使用到多个git仓库,每个git仓库对应一个账号,可以理解为每个git仓库对应一个ssh key,因此我们需要管理多个ssh key。 一、快速创建ssh key 1. 创建SSH keys: mkdir -p ~/.ssh s...
工作过程中,经常会使用到多个git仓库,每个git仓库对应一个账号,可以理解为每个git仓库对应一个ssh key,因此我们需要管理多个ssh key。

一、快速创建ssh key

1. 创建SSH keys:

mkdir -p ~/.ssh
ssh-keygen -t rsa  -C "your_email@example.com" -f .ssh/id_rsa_xxx

2. 添加存在的SSH keys:
第一步,启动ssh-agent环境:
Git Bash:

 # start the ssh-agent in the background
eval "$(ssh-agent -s)"
Agent pid 59566

其它终端:

# start the ssh-agent in the background 
eval $(ssh-agent -s)
Agent pid 59566

第二步:

#id_rsa指你存在的private ssh key,如名为"id_rsa_git"

ssh-add ~/.ssh/id_rsa

3. 测试ssh配置
借助github测试,需先把ssh publish key添加到github账号:https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account/

#建议开启v,输出详细信息。i代表你的私钥路径。
ssh -i ~/.ssh/id_rsa_git -vT git@github.com
结果如下:

#省略debug输出
Hi your_email@example.com! You've successfully authenticated, but GitHub does not provide shel
l access.


注意 : ssh-add 这个命令不是用来永久性的记住你所使用的私钥的。实际上,它的作用只是把你指定的私钥添加到 ssh-agent 所管理的一个 session 当中。 而 ssh-agent 是一个用于存储私钥的临时性的 session 服务,也就是说当你重启之后,ssh-agent 服务也就重置了。


二、多个ssh keys解决方案
1. 在~/.ssh目录创建config,配置config管理多个ssh keys。

touch ~/.ssh/config

内容如下:

#github网站使用User=git
Host github
     HostName github.com
     User git
     IdentityFile ~/.ssh/id_rsa_git

#code.csdn网站
Host code.csdn.net
     HostName code.csdn.net
     User csdn
     IdentityFile ~/.ssh/id_rsa_csdn

2. windows配置ssh-agent随GIT BASH启动
由于priavte key不是默认名字id_rsa,因此每次打开git bash需要重复执行ssh-agent,ssh-add,然后再进行ssh操作,比较麻烦,因此我们通过脚本实现自动化。
参见:https://help.github.com/articles/working-with-ssh-key-passphrases/#auto-launching-ssh-agent-on-msysgit
在~目录创建.profile:

touch ~/.profile

# 复制下面脚本到~/.profile

# Note: ~/.ssh/environment should not be used, as it
#       already has a different purpose in SSH.

env=~/.ssh/agent.env

# Note: Don't bother checking SSH_AGENT_PID. It's not used
#       by SSH itself, and it might even be incorrect
#       (for example, when using agent-forwarding over SSH).

agent_is_running() {
    if [ "$SSH_AUTH_SOCK" ]; then
        # ssh-add returns:
        #   0 = agent running, has keys
        #   1 = agent running, no keys
        #   2 = agent not running
        ssh-add -l >/dev/null 2>&1 || [ $? -eq 1 ]
    else
        false
    fi
}

agent_has_keys() {
    ssh-add -l >/dev/null 2>&1
}

agent_load_env() {
    . "$env" >/dev/null
}

agent_start() {
    (umask 077; ssh-agent >"$env")
    . "$env" >/dev/null
}

if ! agent_is_running; then
    agent_load_env
fi

# if your keys are not stored in ~/.ssh/id_rsa or ~/.ssh/id_dsa, you'll need
# to paste the proper path after ssh-add
if ! agent_is_running; then
     ssh-add
    agent_start
    #指定你的多个keys,每个一行。如:ssh-add ~/.ssh/id_rsa_git
elif ! agent_has_keys; then
    ssh-add
    #指定你的多个keys,每个一行。如:ssh-add ~/.ssh/id_rsa_git
fi

unset env

重新打开GIT BASH,看到如下信息:

bash: /c/Users/xxx/.ssh/agent.env: No such file or directory
Identity added: /c/Users/xxx/.ssh/id_rsa_git (/c/Users/xxx/.ssh/id_rsa_git)
Identity added: /c/Users/xxx/.ssh/id_rsa_csdn (/c/Users/xxx/.ssh/id_rsa_git)

查看进程"ssh-agent"已运行后,打开GIT BASH:

ssh -vT git@gitlab.alibaba-inc.com

看到类似结果(有裁剪):

debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/xxx/.ssh/id_rsa_git
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access. 

至此,Windows环境Git管理多个SSH KEYS完成,Linux/MAC等*unix类似。

三、TortoiseGit/SourceTree配置
TortoiseGit是常见的GIT GUI工具,它使用ppk。

1. 打开 TortoiseGit/ PuTTYgen,并加载之前生成好的ssh private key(如~/.ssh/id_rsa_git)。

2. 点击"save private key"保存为ppk文件。
3. 打开 TortoiseGit/ Pageant,点击"Add key"添加上面生成的ppk文件。

可以使用TortoiseGit代替GIT BASH操作github、gitlab等东东了。

SourceTree与TortoiseGit操作方案类似,不同的是操作页面不一样。

四、其他ssh key注意事项
~/.ssh目录在*unix权限为700:chmod 700 ~/.ssh,在windows不需要。

对于指定私钥,设置密码:
ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]

目录
相关文章
|
SQL Apache Windows
Windows服务器80端口被占用的全面解决方案
在服务管理器中启动apache2服务,即可正常使用80端口。若系统中还安装了其他微软产品如sql等,也可尝试停止其服务进行测试,但请注意,SQL通常不会使用80端口,因此一般不会受到影响。以上就是关于80端口被system占用的详细解决方法,希望对你有所帮助。
|
安全 开发工具 git
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
958 5
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
|
Shell Linux 网络安全
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
1671 3
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
|
网络安全 开发工具 git
配置本地环境以管理Git多账户SSH连接的方法
通过以上步駟设置后, 您可以轻松管理多個 Git 账户并且根据不同项目需求切换 SSH 密匙进行版本控制操作。
1507 20
|
安全 Linux iOS开发
Tenable Nessus 10.10 (macOS, Linux, Windows) - 漏洞评估解决方案
Tenable Nessus 10.10 (macOS, Linux, Windows) - 漏洞评估解决方案
609 0
Tenable Nessus 10.10 (macOS, Linux, Windows) - 漏洞评估解决方案
|
安全 Linux iOS开发
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
892 0
Tenable Nessus 10.9.3 (macOS, Linux, Windows) - 漏洞评估解决方案
|
算法 安全 网络安全
git clone操作报错diffie-hellman-group1-sha1的解决方案
在处理这一问题时,需要确保了解相关操作的安全影响。`diffie-hellman-group1-sha1`算法被认为是不够安全的,这是因为随着计算能力的提高,`SHA-1`算法可以在合理的时间内被破解,而且其对应的 `1024位`Diffie-Hellman组也可能不够强大。因此,在确保Git操作的同时,也要考虑提升安全性的长期解决办法。强烈推荐与管理员或相关技术支持团队合作,升级和加强服务器端的安全配置。
401 12
|
API 开发工具 git
使用git pull遇到Automatic merge failed; fix conflicts and then commit the result.解决方案卓伊凡
使用git pull遇到Automatic merge failed; fix conflicts and then commit the result.解决方案卓伊凡
836 0
使用git pull遇到Automatic merge failed; fix conflicts and then commit the result.解决方案卓伊凡
|
监控 数据安全/隐私保护 Windows
Veeam Data Platform 12.3.2 (Windows) - 数据保护和管理解决方案
Veeam Data Platform 12.3.2 (Windows) - 数据保护和管理解决方案
299 1
|
安全 Linux 网络安全
Linux系统初步设置本地Git环境和生成SSH密钥的步骤。
现在您的Linux系统已经配置好了Git环境,并创建并添加了SSH密钥,可以安全地与远端仓库进行交互,无论是克隆、推送还是拉取操作。此过程确保了数据传输的安全并使版本控制流程更为顺畅。使用Git时应考虑定期更新并管理您的凭据,以确保安全性。
1713 0