.ssh 配置与亚马逊 ec2 和 git

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14423652/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:03:08  来源:igfitidea点击:

.ssh config with amazon ec2 and git

gitamazon-ec2configpublic-key

提问by Pio

I have a strange problem with cloning a git repository from an amazon ec2 server. It works without any problems on one of my computers running ubuntu 12.04, while on another one using 12.10 it gives me an error:

我在从亚马逊 ec2 服务器克隆 git 存储库时遇到了一个奇怪的问题。它在我的一台运行 ubuntu 12.04 的计算机上没有任何问题,而在另一台使用 12.10 的计算机上它给了我一个错误:

ssh: Could not resolve hostname ec2server: Name or service not known  
fatal: The remote end hung up unexpectedly

It's like it is not recognizing my config file. I use the following git command to clone :

就像它无法识别我的配置文件一样。我使用以下 git 命令来克隆:

sudo git clone ec2server:/var/www/project.git

or

或者

sudo git clone ec2xxx.compute-1.amazonaws.com:/var/www/project.git

The two config files are identical on both computers, inside the ~./ssh with the following content:

这两个配置文件在两台计算机上都是相同的,在 ~./ssh 中,内容如下:

Host ec2server
   Hostname ec2XXX.compute-1.amazonaws.com
   User ubuntu
   IdentityFile ~/.ssh/mykey.pem

If I substitute the ec2server with the actual address I get the following error:

如果我用实际地址替换 ec2server,我会收到以下错误:

Cloning into 'project'...
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Thanks in advance.

提前致谢。

采纳答案by Pio

Recently I had to re-install the system which held the working configuration and I have not managed to replicate it, so it's not about the linux version (very likely). What I managed to do, is that I managed to use the mentioned original script to clone the repository into my home directory. Here it picked up the name correctly, but if I navigated into /var/www, what I was doing before, it just gave me the same error: could ec2server not found ... . So I figure the problem has to do something with the permissions + commands combination. If anyone can figure it out how to make it work, I will mark he's response as a correct answer, until then I am marking mine as the correct one, since this is the closest to the correct.

最近我不得不重新安装保存工作配置的系统,但我没有设法复制它,所以这与 linux 版本无关(很有可能)。我设法做的是,我设法使用提到的原始脚本将存储库克隆到我的主目录中。在这里它正确地选择了名称,但是如果我导航到 /var/www,我之前在做什么,它只是给了我同样的错误: could ec2server not found ... 。所以我认为问题必须与权限 + 命令组合有关。如果有人能弄清楚如何使它起作用,我会将他的回答标记为正确答案,直到那时我将我的回答标记为正确答案,因为这是最接近正确的答案。

UPDATE

更新

I figured it out what was the problem: I had to change the permission on /var/www folder in order to be able to clone into that directory. Now it's 777 ( used only for dev on a local machine).

我想出了问题所在:我必须更改 /var/www 文件夹的权限才能克隆到该目录中。现在是 777(仅用于本地机器上的开发)。

回答by VonC

It usually is a permission issue.
The chmodon the parent directories of your config file might be different between your two computers.
(and I am not talking about just the immediate parent directory .ssh, but also allthe parent directories)

这通常是权限问题。
chmod您的配置文件的父目录可能是您的两台计算机之间的不同。
(我不是在谈论直接的父目录.ssh,而是所有的父目录)

See "Git SSH authentication", but also know that if any of the parent directories is writable for group or world, ssh won't work.

参见“ Git SSH authentication”,但也知道如果任何父目录对于组或世界是可写的,ssh 将不起作用。



Note also that your second command is not right, and should be:

另请注意,您的第二个命令不正确,应该是:

git clone [email protected]/var/www/project.git

no ':'(a ':' means using a configfile, with an scp-like syntax)

否 ' :'(' :' 表示使用config文件,具有类似scp语法)

it can only work if you have ~/.ssh/id_rsaand ~/.ssh/id_rsa.pubthough.
If you have mykey.pem, then you needa configfile for ssh to know where are your public and private keys, which means only this can work:

如果您有它才能发挥作用~/.ssh/id_rsa~/.ssh/id_rsa.pub虽然。
如果你有 mykey.pem,那么你需要一个config文件让 ssh 知道你的公钥和私钥在哪里,这意味着只有这个可以工作:

git clone ec2server:/var/www/project.git


One other chack (after this threadand this forum) is to check if there is any DNS/DHCP issue (a bit like in "Working with git behind a dynamic DNS").

另一个技巧(在这个帖子和这个论坛之后)是检查是否有任何 DNS/DHCP 问题(有点像“在动态 DNS 后面使用 git”)。

Host ec2server
   Hostname 1xx.xxx.xxx.xxx # ip address of ec2XXX.compute-1.amazonaws.com
   User ubuntu
   IdentityFile ~/.ssh/mykey.pem