EC2:如何克隆 Git 存储库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19596974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EC2: How to Clone Git Repository
提问by David Williams
I am trying to checkout a private repo from github.com onto my dev instance on EC2.
我正在尝试从 github.com 将私有存储库签出到我在 EC2 上的开发实例上。
$ git clone [email protected]:Org/Product.git
Initialized empty Git repository in /home/ec2-user/Product/.git/
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
How do I do this? I tried SSH forwarding as well, but that didn't work.
我该怎么做呢?我也尝试过 SSH 转发,但是没有用。
回答by Yafang Yang
We need to generate an SSH key (two files - a public key that you share with the world and a private key you keep safe) that we will associate with our Git account. This will allow us to clone our Git repository on an EC2 instance without having to manually type in your username and password or (worse yet) put your password in cleartext when using a script.
我们需要生成一个 SSH 密钥(两个文件 - 一个与世界共享的公钥和一个安全的私钥),我们将与我们的 Git 帐户相关联。这将允许我们在 EC2 实例上克隆我们的 Git 存储库,而无需手动输入您的用户名和密码,或者(更糟糕的是)在使用脚本时以明文形式输入您的密码。
You can generate an SSH key on your local directory and then copy to your EC2 instance. You can also do it on your EC2 instance directly, but each time you generate an SSH key pair on your new instance, you need to register the new key in GitHub every time.
您可以在本地目录上生成 SSH 密钥,然后复制到您的 EC2 实例。您也可以直接在您的 EC2 实例上执行此操作,但是每次在新实例上生成 SSH 密钥对时,您每次都需要在 GitHub 中注册新密钥。
In your local terminal, create an SSH key, substituting your email address.
$ ssh-keygen -t rsa -b 4096 -C [your email address]
Save the key to the default directory, ~/.ssh
Enter a pass-phrase of your choice.
Check that the public and private key are in /.ssh by going to the directory and typing “ls -l id_rsa*”. You should see two files, the public key named “id_rsa.pub” and the private key named “id_rsa”
From the terminal, make sure this private key is not publicly viewable.
$ chmod 600 ~/.ssh/id_rsa
Add your SSH private key to the ssh-agent and store your passphrase in the keychain.
$ ssh-add -k ~/.ssh/id_rsa
Go to the settings under your GitHub account and then click SSH keys and New SSH key
In terminal copy your public key to the clipboard. Or show on the EC2 terminal:
$ pbcopy < ~/.ssh/id_rsa.pub # copy to clipboard $ cat ~/.ssh/id_rsa.pub # If you prefer appear on screen
Paste this into the key box on GitHub and click save. This key is available to ALL your Git repositories.
Sometimes you need to move the public key to “/.ssh/authorized_keys” to make the public key to work in LINUX.
$ mkdir ~/.ssh # if you don't have /.ssh/ folder $ chmod 700 ~/.ssh $ touch ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Follow this article https://help.github.com/articles/error-permission-denied-publickey/to see if the key works and debug.
在您的本地终端中,创建一个 SSH 密钥,替换您的电子邮件地址。
$ ssh-keygen -t rsa -b 4096 -C [your email address]
将密钥保存到默认目录 ~/.ssh
输入您选择的密码。
通过转到目录并键入“ls -l id_rsa*”来检查公钥和私钥是否在 /.ssh 中。您应该看到两个文件,名为“id_rsa.pub”的公钥和名为“id_rsa”的私钥
在终端中,确保此私钥不可公开查看。
$ chmod 600 ~/.ssh/id_rsa
将您的 SSH 私钥添加到 ssh-agent 并将您的密码存储在钥匙串中。
$ ssh-add -k ~/.ssh/id_rsa
转到您的 GitHub 帐户下的设置,然后单击 SSH 密钥和新建 SSH 密钥
在终端中将您的公钥复制到剪贴板。或者在EC2终端上显示:
$ pbcopy < ~/.ssh/id_rsa.pub # copy to clipboard $ cat ~/.ssh/id_rsa.pub # If you prefer appear on screen
将此粘贴到 GitHub 上的密钥框中,然后单击保存。此密钥可用于您的所有 Git 存储库。
有时您需要将公钥移动到“/.ssh/authorized_keys”才能使公钥在 LINUX 中工作。
$ mkdir ~/.ssh # if you don't have /.ssh/ folder $ chmod 700 ~/.ssh $ touch ~/.ssh/authorized_keys $ chmod 600 ~/.ssh/authorized_keys $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
按照这篇文章https://help.github.com/articles/error-permission-denied-publickey/查看密钥是否有效并进行调试。
回答by Bijendra
When working with git or setting up ssh keys on ec2 instance, consider as just your m/c. So steps which were used like generating ssh keys, adding id_rsa.pub key to accepted ssh keys on git account should be performed. After copying the ssh key, check if the connection is established. Checkout the links in the above answers as they directly point to your solution.
在 ec2 实例上使用 git 或设置 ssh 密钥时,请仅将其视为您的 m/c。因此,应该执行诸如生成 ssh 密钥、将 id_rsa.pub 密钥添加到 git 帐户上接受的 ssh 密钥之类的步骤。复制ssh密钥后,检查连接是否建立。查看上述答案中的链接,因为它们直接指向您的解决方案。
回答by ianjs
See this answer.
看到这个答案。
You just need to set up your public and private keys to authenticate with GitHub as described here: https://help.github.com/articles/generating-ssh-keys
您只需要设置您的公钥和私钥以使用 GitHub 进行身份验证,如下所述:https: //help.github.com/articles/generate-ssh-keys