git ssh密钥_如何设置Git SSH密钥

git ssh密钥

When working with Git using the command line, the most common way to handle authentication is through the use of SSH keys.

使用命令行使用Git时,最常见的身份验证方法是使用SSH密钥。

Most GUI-based clients like GitHub Desktop will handle this for you, but sometimes you need the command line, and so it’s very useful to have a SSH key setup in place.

大多数基于GUI的客户端(例如GitHub Desktop)将为您处理此问题,但是有时您需要命令行,因此适当设置SSH密钥非常有用。

Also, sometimes you’ll need an SSH key to do useful things like pulling a repository on a remote server.

另外,有时您需要SSH密钥来执行一些有用的操作,例如在远程服务器上提取存储库。

电脑上的按键 (Keys on your computer)

SSH keys are stored in the ~/.ssh folder.

SSH密钥存储在~/.ssh文件夹中。

You can have more than one key in there, because SSH keys are used for things other than Git.

您可以在其中拥有多个密钥,因为SSH密钥用于Git以外的其他事物。

You can list all your SSH keys by typing

您可以通过键入列出所有SSH密钥

ls -al ~/.ssh

If you have existing keys you’ll notice that they sit in pairs, one file and another similarly named ending with .pub:

如果您有现有的密钥,您会发现它们成对放置,一个文件和另一个类似的文件名以.pub结尾:

Existing SSH keys

The .pub file contains the public key, while the other file contains the private key which should never be shared anywhere.

.pub文件包含公钥,而另一个文件包含私钥,永远不要在任何地方共享。

You should never share the private key anywhere. If you lose the private key, you’ll have to regenerate a new private/public key pair, as the authentication cannot be successfully completed without the private key part.

您永远不要在任何地方共享私钥。 如果丢失了私钥,则必须重新生成新的私钥/公钥对,因为没有私钥部分就无法成功完成身份验证。

生成新密钥 (Generating a new key)

You generate a new SSH key using the command ssh-keygen, which is available on all macOS, Linux and modern Windows computers with the Linux subsystem or the Git for Windows package.

您可以使用ssh-keygen命令生成新的SSH密钥,该命令可在所有具有Linux子系统或Git for Windows软件包的macOS,Linux和现代Windows计算机上使用。

Here’s the command you use:

这是您使用的命令:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

The last part, which in this example is filled with an email address, is a comment. You can enter any email you want, it does not have to be your GitHub account or it can even be a random string. It can be useful to know who generated the key if there is potential for ambiguity.

在此示例中,最后一个部分用电子邮件地址填充,它是注释。 您可以输入所需的任何电子邮件,不必是您的GitHub帐户,甚至可以是随机字符串。 知道谁产生了密钥是否存在歧义可能很有用。

The key generation program will ask you where you want to save the key. If this is the first key, you are suggested to use id_rsa as the filename, but you’d better choose a file name that remembers you the service you are generating it for, like github_rsa.

密钥生成程序将询问您要将密钥保存在何处。 如果这是第一个键,建议您使用id_rsa作为文件名,但是最好选择一个文件名,该文件名可以记住您为其生成服务的服务,例如github_rsa

You can optionally add a password. I highly recommend setting the password. macOS will store the password in the Keychain so you don’t have to repeat it every time.

您可以选择添加密码。 我强烈建议您设置密码。 macOS会将密码存储在钥匙串中,因此您不必每次都重复输入密码。

将密钥添加到GitHub (Add the key to GitHub)

I show the process for GitHub, but it’s the same kind of process that every Git platform uses, with small differences.

我展示了GitHub的流程,但是它与每个Git平台都使用的流程相同,但差别很小。

In the GitHub settings you’ll find the “SSH and GPG keys” menu:

在GitHub设置中,您会找到“ SSH和GPG密钥”菜单:

Clicking it reveals your current setup:

单击它会显示您当前的设置:

As you can see up here I defined 3 SSH keys, one I use locally on my mac, and 2 I use on the remote servers to pull the website code.

正如您在此处看到的那样,我定义了3个SSH密钥,一个在我的Mac本地使用,另外两个在远程服务器上使用,以获取网站代码。

Pressing “New SSH key” lets you add a new one:

按“新建SSH密钥”可添加一个新的:

You set the title to be something meaningful, which you’ll remember 2 years+ from now.

您将标题设置为有意义的东西,从现在起您将记得2年以上。

The key is the one you generated before.

密钥是您之前生成的密钥。

You can open the .pub file of the key, copy its content and paste it in this box.

您可以打开密钥的.pub文件,复制其内容并将其粘贴在此框中。

You can do so using any CLI command like cat id_rsa.pub and you copy/paste the whole key code with the mouse.

您可以使用任何CLI命令(例如cat id_rsa.pub来执行此操作,然后使用鼠标复制/粘贴整个键代码。

Once you save that string to GitHub (or any other service, as the concept is the same for everyone), your Git client will have the credentials it needs to communicate with the remove server, and you should be good to go!

一旦将该字符串保存到GitHub(或每个其他服务,因为每个人的概念都相同),您的Git客户端将具有与删除服务器进行通信所需的凭据,您应该一切顺利!

使用多个键 (Using multiple keys)

It’s recommended to use a different SSH key for every service you intend to use.

建议对要使用的每个服务使用不同的SSH密钥。

This makes it very easy to invalidate a key on a specific service without having to change it on all the services you use if you decide to renew it, either because compromised/publicly exposed or for some other reason.

这使得使特定服务上的密钥失效变得非常容易,而如果您决定更新某个密钥(无论是由于泄露/公开的原因还是出于其他原因),而不必在所有使用的服务上对其进行更改。

翻译自: https://flaviocopes.com/how-to-setup-git-ssh-keys/

git ssh密钥

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值