github ssh密钥
The need to manage multiple GitHub accounts on the same machine comes up at some point in time for most developers. Every single time I happen to change my Mac or need to Git push with a new work account, I end up surfing for the how to’s of something I have done over half a dozen times.
大多数开发人员有时会需要在同一台机器上管理多个GitHub帐户。 每当我碰巧更换Mac或需要用一个新的工作帐户推动Git推送时,我最终都会为自己做过六遍的事情做些冲浪。
My laziness in not documenting the process and inability to remember the steps makes me spent a decent amount of time getting the bits and pieces from all over the web and then somehow making it work.
我懒于不记录流程,并且无法记住步骤,这使我花费了大量的时间从网上获取点点滴滴,然后以某种方式使其工作。
I’m sure there are many of you who have been there, done that and many more of you who are just waiting for the next time the same thing occurs (myself included!). This endeavor is meant to help us all out.
我敢肯定,有很多人去过那里,做了这些,还有更多人在等下一次相同的事情发生(包括我自己!)。 这项努力旨在帮助我们所有人。
1.生成SSH密钥 (1. Generating the SSH keys)
Before generating an SSH key, we can check to see if we have any existing SSH keys: ls -al ~/.ssh
This will list out all existing public and private key pairs, if any.
在生成SSH密钥之前,我们可以检查是否有现有的SSH密钥: ls -al ~/.ssh
这将列出所有现有的公用和专用密钥对(如果有)。
If ~/.ssh/id_rsa
is available, we can reuse it, or else we can first generate a key to the default ~/.ssh/id_rsa
by running:
如果~/.ssh/id_rsa
可用,我们可以重用它,否则我们可以先运行以下命令为默认~/.ssh/id_rsa
生成密钥:
ssh-keygen -t rsa
When asked for the location to save the keys, accept the default location by pressing enter. A private key and public key ~/.ssh/id_rsa.pub
will be created at the default ssh location ~/.ssh/
.
当询问保存密钥的位置时,请按Enter键接受默认位置。 私钥和公钥~/.ssh/id_rsa.pub
将在默认ssh位置~/.ssh/
。
Let’s use this default key pair for our personal account.
让我们将此默认密钥对用于我们的个人帐户。
For the work accounts, we will create different SSH keys. The below code will generate the SSH keys, and saves the public key with the tag “email@work_mail.com” to ~/.ssh/id_rsa_work_user1.pub
对于工作帐户,我们将创建不同的SSH密钥。 以下代码将生成SSH密钥,并将带有标签“ email@work_mail.com”的公共密钥保存到~/.ssh/id_rsa_work_user1.pub
$ ssh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"
We have two different keys created:
我们创建了两个不同的密钥:
~/.ssh/id_rsa
~/.ssh/id_rsa_work_user1
2.将新的SSH密钥添加到相应的GitHub帐户 (2. Adding the new SSH key to the corresponding GitHub account)
We already have the SSH public keys ready, and we will ask our GitHub accounts to trust the keys we have created. This is to get rid of the need for typing in the username and password every time you make a Git push.
我们已经准备好了SSH公共密钥,并且我们将要求GitHub帐户信任我们创建的密钥。 这是为了避免您每次进行Git推送时都键入用户名和密码。
Copy the public key pbcopy < ~/.ssh/id_rsa.
pub and then log in to your personal GitHub account:
复制公钥pbcopy < ~/.ssh/id_rsa.
pub,然后登录到您的个人GitHub帐户:
Go to
Settings
转到
Settings
Select
SSH and GPG keys
from the menu to the left.从左侧菜单中选择
SSH and GPG keys
。Click on
New SSH key
, provide a suitable title, and paste the key in the box below单击“
New SSH key
,提供合适的标题,然后将密钥粘贴在下面的框中Click
Add key
— and you’re done!点击
Add key
-完成!
For the work accounts, use the corresponding public keys (
pbcopy < ~/.ssh/id_rsa_work_user1.
pub) and repeat the above steps in your GitHub work accounts.对于工作帐户,请使用相应的公共密钥(
pbcopy < ~/.ssh/id_rsa_work_user1.
),并在GitHub工作帐户中重复上述步骤。
3。 使用ssh-agent注册新的SSH密钥 (3 . Registering the new SSH Keys with the ssh-agent)
To use the keys, we have to register them with the ssh-agent on our machine. Ensure ssh-agent is running using the command eval "$(ssh-agent -s)"
.Add the keys to the ssh-agent like so:
要使用密钥,我们必须在计算机上的ssh-agent中注册它们。 确保使用命令eval "$(ssh-agent -s)"
来运行eval "$(ssh-agent -s)"
将密钥添加到ssh-agent中,如下所示:
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_work_user1
Make the ssh-agent use the respective SSH keys for the different SSH Hosts.
使ssh-agent对不同的SSH主机使用各自的SSH密钥。
This is the crucial part, and we have two different approaches:
这是关键部分,我们有两种不同的方法:
Using the SSH configuration file (Step 4), and having only one active SSH key in the ssh-agent at a time (Step 5).
使用SSH配置文件(第4步),并且一次在ssh-agent中只有一个活动的SSH密钥(第5步)。
4.创建SSH配置文件 (4. Creating the SSH config File)
Here we are actually adding the SSH configuration rules for different hosts, stating which identity file to use for which domain.
在这里,我们实际上是在为不同的主机添加SSH配置规则,说明要在哪个域中使用哪个身份文件。
The SSH config file will be available at ~/.ssh/config. Edit it if it exists, or else we can just create it.
SSH配置文件将在〜/ .ssh / config中提供 。 编辑它(如果存在),否则我们就可以创建它。
$ cd ~/.ssh/
$ touch config // Creates the file if not exists
$ code config // Opens the file in VS code, use any editor
Make configuration entries for the relevant GitHub accounts similar to the one below in your ~/.ssh/config
file:
使相关GitHub帐户的配置条目类似于~/.ssh/config
文件中的以下内容:
# Personal account, - the default config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# Work account-1
Host github.com-work_user1
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work_user1
“work_user1” is the GitHub user id for the work account.
“ work_user1 ”是工作帐户的GitHub用户ID。
“github.com-work_user1” is a notation used to differentiate the multiple Git accounts. You can also use “work_user1.github.com” notation as well. Make sure you’re consistent with what hostname notation you use. This is relevant when you clone a repository or when you set the remote origin for a local repository
“ g ithub.com- work_user1 ”是用于区分多个Git帐户的符号。 您也可以使用“ work_user1.g ithub.com”表示法。 确保您与所使用的主机名符号保持一致。 在克隆存储库或为本地存储库设置远程源时,这是相关的
The above configuration asks ssh-agent to:
上面的配置要求ssh-agent:
Use id_rsa as the key for any Git URL that uses @github.com
使用id_rsa作为密钥 对于 任何使用@ github.com的 Git URL
Use the id_rsa_work_user1 key for any Git URL that uses @github.com-work_user1
使用任何的Git URL使用@ github.com-work_user1的id_rsa_work_user1关键
5.一次在ssh-agent中有一个活动的SSH密钥 (5. One active SSH key in the ssh-agent at a time)
This approach doesn’t require the SSH config rules. Rather we manually ensure that the ssh-agent has only the relevant key attached at the time of any Git operation.
这种方法不需要SSH配置规则。 而是我们手动确保ssh-agent在执行任何Git操作时仅附加了相关的密钥。
ssh-add -l
will list all the SSH keys attached to the ssh-agent. Remove all of them and add the one key you are about to use.
ssh-add -l
将列出附加到ssh-agent的所有SSH密钥。 删除所有这些,然后添加您将要使用的一个密钥。
If it’s to a personal Git account that you are about to push:
如果您要推送到个人Git帐户:
$ ssh-add -D //removes all ssh entries from the ssh-agent
$ ssh-add ~/.ssh/id_rsa // Adds the relevant ssh key
The ssh-agent now has the key mapped with the personal GitHub account, and we can do a Git push to the personal repository.
现在,ssh-agent的密钥已映射到个人GitHub帐户,我们可以将Git推送到个人存储库。
To push to your work GitHub account-1, change the SSH key mapped with the ssh-agent by removing the existing key and adding the SSH key mapped with the GitHub work account.
要推送到您的工作GitHub帐户1,请更改ssh-agent映射的SSH密钥,方法是删除现有密钥并添加映射到GitHub工作帐户的SSH密钥。
$ ssh-add -D
$ ssh-add ~/.ssh/id_rsa_work_user1
The ssh-agent at present has the key mapped with the work Github account, and you can do a Git push to the work repository. This requires a bit of manual effort, though.
目前ssh-agent的密钥已映射到工作Github帐户,您可以将Git推送到工作存储库。 不过,这需要一些人工。
为本地存储库设置git remote Url (Setting the git remote Url for the local repositories)
Once we have local Git repositories cloned /created, ensure the Git config user name and email is exactly what you want. GitHub identifies the author of any commit from the email id attached with the commit description.
克隆/创建本地Git存储库后,请确保Git配置用户名和电子邮件正是您想要的。 GitHub通过提交描述随附的电子邮件ID来标识任何提交的作者。
To list the config name and email in the local Git directory, do git config user.name
and git config user.email
. If it’s not found, update accordingly.
要在本地Git目录中列出配置名称和电子邮件,请执行git config user.name
和git config user.email
。 如果找不到,请进行相应更新。
git config user.name "User 1" // Updates git config user name
git config user.email "user1@workMail.com"
6.在克隆存储库时 (6. While Cloning Repositories)
Note: step 7 will help, if we have the repository already available on local.
注意:如果我们的存储库已经在本地可用,则第7步将有所帮助。
Now that the configurations are in place, we can go ahead and clone the corresponding repositories. On cloning, make a note that we use the host names that we used in the SSH config.
现在配置已经就绪,我们可以继续克隆相应的存储库。 克隆时,请注意,我们使用了SSH配置中使用的主机名。
Repositories can be cloned using the clone command Git provides:
可以使用Git提供的克隆命令来克隆存储库:
git clone git@github.com:personal_account_name/repo_name.git
The work repository will require a change to be made with this command:
工作存储库将需要使用以下命令进行更改:
git clone git@github.com-work_user1:work_user1/repo_name.git
This change is made depending on the host name defined in the SSH config. The string between @ and : should match what we have given in the SSH config file.
所做的更改取决于SSH配置中定义的主机名。 @和:之间的字符串应与我们在SSH配置文件中提供的字符串匹配。
7.对于本地现有存储库 (7. For Locally Existing Repositories)
If we have the repository already cloned:
如果我们已经克隆了存储库:
List the Git remote of the repository, git remote -v
列出存储库的Git远程git remote -v
Check whether the URL matches our GitHub host to be used, or else update the remote origin URL.
检查URL是否与要使用的GitHub主机匹配,否则更新远程原始URL。
git remote set-url origin git@github.com-worker_user1:worker_user1/repo_name.git
Ensure the string between @ and : matches the Host we have given in the SSH config.
确保@和:之间的字符串与我们在SSH配置中指定的主机匹配。
If you are creating a new repository on local:
如果要在本地创建新的存储库:
Initialize Git in the project folder git init
.
在项目文件夹git init
初始化Git。
Create the new repository in the GitHub account and then add it as the Git remote to the local repository.
在GitHub帐户中创建新的存储库,然后将其作为Git远程添加到本地存储库。
git remote add origin git@github.com-work_user1:work_user1/repo_name.git
Ensure the string between @ and : matches the Host we have given in the SSH config.
确保@和:之间的字符串与我们在SSH配置中指定的主机匹配。
Push the initial commit to the GitHub repository:
将初始提交推送到GitHub存储库:
git add .
git commit -m "Initial commit"
git push -u origin master
We are done!
我们完了!
Adding or updating the Git remote of the local Git directory with the proper host will take care of selecting the correct SSH key to verify our identity with GitHub. With all the above in place, our git operations
should work seamlessly.
使用适当的主机添加或更新本地Git目录的Git远程服务器将负责选择正确的SSH密钥,以验证我们在GitHub上的身份。 完成上述所有git operations
,我们的git operations
应无缝运行。
翻译自: https://www.freecodecamp.org/news/manage-multiple-github-accounts-the-ssh-way-2dadc30ccaca/
github ssh密钥