如何获取和配置Git和GitHub SSH密钥

If you use GitHub without setting up an SSH key, you're really missing out. Just think–all of that time you spent entering your email address and password into the console every time you push a commit could have been spent coding.

如果您在不设置SSH密钥的情况下使用GitHub,那么您真的会错过。 试想一下-每次您每次提交提交都花在控制台上输入电子邮件地址和密码的所有时间都可能花在了编码上。

Well no more. Here's a quick guide to generate and configure an SSH key with GitHub so you never have to authenticate the old fashioned way again.

好吧,不再。 这是使用GitHub生成和配置SSH密钥的快速指南,因此您无需再次验证老式方法。

检查现有的SSH密钥 (Check for an existing SSH key)

First, check if you've already generated SSH keys for your machine. Open a terminal and enter the following command:

首先,检查您是否已经为计算机生成了SSH密钥。 打开一个终端并输入以下命令:

ls -al ~/.ssh

If you've already generated SSH keys, you should see output similar to this:

如果您已经生成了SSH密钥,则应该看到类似于以下的输出:

-rw-------  1 user_name user_name  1766 Jul  7  2018 id_rsa
-rw-r--r--  1 user_name user_name   414 Jul  7  2018 id_rsa.pub
-rw-------  1 user_name user_name 12892 Feb  5 18:39 known_hosts

If your keys already exist, skip ahead to the Copy your public SSH key section below.

如果您的密钥已经存在,请跳到下面的“ 复制您的公共SSH密钥”部分。

If you don't see any output or that directory doesn't exist (you get a No such file or directory message), then run:

如果您看不到任何输出或该目录不存在(您收到“ No such file or directory消息”),请运行:

mkdir $HOME/.ssh

Then generate a new set of keys with:

然后使用以下命令生成一组新密钥:

ssh-keygen -t rsa -b 4096 -C your@email.com

Now check that your keys exist with the ls -al ~/.ssh command and ensure that the output is similar to the one listed above.

现在,使用ls -al ~/.ssh命令检查您的密钥是否存在,并确保输出与上面列出的相似。

Note: SSH keys are always generated as a pair of public (id_rsa.pub) and private (id_rsa) keys. It's extremely important that you never reveal your private key, and only use your public key for things like GitHub authentication. You can read more about how SSH / RSA key pairs work here.

注意: SSH密钥始终作为一对公共( id_rsa.pub )和私有( id_rsa )密钥生成。 切勿泄露私钥仅将公钥用于GitHub身份验证之类,这一点非常重要。 您可以在此处详细了解SSH / RSA密钥对的工作方式。

将您的SSH密钥添加到ssh-agent (Add your SSH key to ssh-agent )

ssh-agent is a program that starts when you log in and stores your private keys. For it to work properly, it needs to be running and have a copy of your private key.

ssh-agent是一个程序,当您登录并存储私钥时会启动。 为了使其正常运行,它需要正在运行并拥有您的私钥副本。

First, make sure that ssh-agent is running with:

首先,确保ssh-agent运行于:

eval "$(ssh-agent -s)" # for Mac and Linux

or:

要么:

eval `ssh-agent -s`
ssh-agent -s # for Windows

Then, add your private key to ssh-agent with:

然后,使用以下命令将私钥添加到ssh-agent

ssh-add ~/.ssh/id_rsa

复制您的公共SSH密钥 (Copy your public SSH key)

Next, you need to copy your public SSH key to the clipboard.

接下来,您需要将公共SSH密钥复制到剪贴板。

For Linux or Mac, print the contents of your public key to the console with:

对于Linux或Mac,使用以下命令将公用密钥的内容打印到控制台:

cat ~/.ssh/id_rsa.pub # Linux

Then highlight and copy the output.

然后突出显示并复制输出。

Or for Windows, simply run:

或对于Windows,只需运行:

clip < ~/.ssh/id_rsa.pub # Windows

将您的公共SSH密钥添加到GitHub (Add your public SSH key to GitHub)

Go to your GitHub settings page and click the "New SSH key" button:

转到您的GitHub 设置页面,然后单击“新建SSH密钥”按钮:

Then give your key a recognizable title and paste in your public (id_rsa.pub) key:

然后给您的密钥一个可识别的标题,然后粘贴到您的公共( id_rsa.pub )密钥中:

Finally, test your authentication with:

最后,使用以下命令测试身份验证:

ssh -T git@github.com

If you've followed all of these steps correctly, you should see this message:

如果正确执行了所有这些步骤,则应该看到以下消息:

Hi your_user_name! You've successfully authenticated, but GitHub does not provide shell access.

有关SSH的更多信息: (More info on SSH:)

翻译自: https://www.freecodecamp.org/news/git-ssh-how-to/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值