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]

目录
相关文章
|
9月前
|
安全 开发工具 git
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
855 5
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
|
9月前
|
Shell Linux 网络安全
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
1441 3
宝塔服务器面板部署安装git通过第三方应用安装收费怎么办—bash: git: command not found解决方案-优雅草卓伊凡
|
10月前
|
网络安全 开发工具 git
配置本地环境以管理Git多账户SSH连接的方法
通过以上步駟设置后, 您可以轻松管理多個 Git 账户并且根据不同项目需求切换 SSH 密匙进行版本控制操作。
1272 20
|
10月前
|
安全 BI 持续交付
金融保险行业 AD 域自动化管理解决方案
金融保险行业作为数据密集型领域,核心资产涵盖客户信贷信息、高净值客户数据、绿色金融项目资料等敏感内容,这些数据不仅是企业核心竞争力的体现,更是监管合规的重点关注对象。当前,行业正面临 “管理效率低下” 与 “数据安全风险” 的双重挑战。
290 0
|
12月前
|
算法 安全 网络安全
git clone操作报错diffie-hellman-group1-sha1的解决方案
在处理这一问题时,需要确保了解相关操作的安全影响。`diffie-hellman-group1-sha1`算法被认为是不够安全的,这是因为随着计算能力的提高,`SHA-1`算法可以在合理的时间内被破解,而且其对应的 `1024位`Diffie-Hellman组也可能不够强大。因此,在确保Git操作的同时,也要考虑提升安全性的长期解决办法。强烈推荐与管理员或相关技术支持团队合作,升级和加强服务器端的安全配置。
322 12
|
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.解决方案卓伊凡
707 0
使用git pull遇到Automatic merge failed; fix conflicts and then commit the result.解决方案卓伊凡
|
存储 Shell 开发工具
Git和TortoiseGit的安装与使用
Git和TortoiseGit的结合使用,可以大大提高版本控制的效率和便捷性。通过本文的步骤,您可以轻松安装和配置Git及TortoiseGit,并掌握基本的版本控制操作。
5538 82
|
11月前
|
安全 Linux 网络安全
Linux系统初步设置本地Git环境和生成SSH密钥的步骤。
现在您的Linux系统已经配置好了Git环境,并创建并添加了SSH密钥,可以安全地与远端仓库进行交互,无论是克隆、推送还是拉取操作。此过程确保了数据传输的安全并使版本控制流程更为顺畅。使用Git时应考虑定期更新并管理您的凭据,以确保安全性。
1551 0
|
网络协议 Linux 网络安全
微软工程师偷偷在用!这款SSH工具让Windows操控CentOS比Mac还优雅!
远程登录Linux服务器是管理和维护服务器的重要手段,尤其在远程办公、云服务管理等场景中不可或缺。通过工具如XShell,用户可以方便地进行远程管理。SSH协议确保了数据传输的安全性,命令行界面提高了操作效率。配置XShell连接CentOS时,需确保Linux系统开启sshd服务和22端口,并正确设置主机地址、用户名和密码。此外,调整字体和配色方案可优化使用体验,解决中文显示问题。
711 21
微软工程师偷偷在用!这款SSH工具让Windows操控CentOS比Mac还优雅!
|
Shell 网络安全 开发工具
Git常见问题解决:解决TortoiseGit的No supported authentication methods available错误。
总的来说,遇到"No supported authentication methods available"这个问题,就像是钓到了一条刺猬。解决它就像是给刺猬穿上一件新衣服。首先,你需要生成新的SSH秘钥对,然后,将你的公钥添加到Git服务器,这样,你的SSH客户端就能认出刺猬了。接下来,将你的私钥添加到SSH agent,让你的SSH客户端拥有这个刺猬的“身份证”。最后,你还需要告诉TortoiseGit,如何使用这个新的SSH秘钥,也就是将刺猬的“身份证”告诉TortoiseGit。只需要做到这四点,你就可以顺利地解决这个问题了。希望这个步骤为步的解决方法能够帮到你解决这个问题,让你的代码
1657 21