git ssh key 配置

本文详细介绍了如何使用ssh-keygen生成SSH密钥,并通过编辑~/.ssh/config或.git/config文件来为不同的Git服务器或同一服务器上的不同用户指定特定的SSH密钥。包括了具体命令和配置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 产生 key
ssh-keygen

场景一

  • 不同git server,不同 ssh key
    编辑文件 ~/.ssh/config
Host <Host Name>
HostName AAAAAAA.org
IdentityFile ~/.ssh/<key for the given host>

Host <Host1 Name>
HostName BBBBB.org
IdentityFile ~/.ssh/<host 2 key>

场景二

  • 相同git server,不同用户,不同ssh key
git config --local core.sshCommand 'ssh -i private_key_file' 
  • 或者直接编辑 .git/config ,在 core 下面加入 sshCommand = ssh -i ~/.ssh/harryhare
user@workstation:~/workspace/project-name/.git$ cat config
[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    sshCommand = ssh -i ~/location-of/.ssh/private_key -F /dev/null <--Check that this command exist
[remote "origin"]
    url = git@github.com:<user-name>/<project-name>.git  <-- Make sure its the SSH URL and not the WEB URL
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

参考:https://stackoverflow.com/questions/4565700/how-to-specify-the-private-ssh-key-to-use-when-executing-shell-command-on-git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值