使用 sudo 进行 git clone

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

make git clone with sudo

linuxgitsshsudo

提问by Anis_Stack

when I make git clone with ssh from a user prompt it works properly.

当我根据用户提示使用 ssh 进行 git clone 时,它​​可以正常工作。

git clone ssh://URL.com/soft.git soft_git

the ssh key id_rsaand id_rsa.pubare under /home/user/.ssh

ssh 密钥 id_rsaid_rsa.pub/home/user/.ssh

my purpose is the execute git with sudobut I got the following error

我的目的是执行 git withsudo但我收到以下错误

Cloning into '/home/user/git/soft'...
Permission denied (publickey,keyboard-interactive).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

I create a folder /root/.sshand I copy the ssh keys into it but I got the same error

我创建了一个文件夹,/root/.ssh然后将 ssh 密钥复制到其中,但出现了相同的错误

how to execute git with sudo properly.

如何正确使用 sudo 执行 git。

回答by Dietrich Epp

When you run git using sudo, git will run as root. Because git is running as root, ssh is running as root. Because ssh is running as root, it is trying to log on to the remote server as root. The remote server is not okay with this (as it should be!)

当您使用 sudo 运行 git 时,git 将以 root 身份运行。因为 git 以 root 身份运行,所以 ssh 以 root 身份运行。因为 ssh 以 root 身份运行,所以它试图以 root 身份登录到远程服务器。远程服务器对此不满意(应该如此!)

You will need to do two things:

你需要做两件事:

  • Put the username in your URL: ssh://[email protected]/soft.git.

  • Make your SSH key available to the root user, because it will look under /root/.sshinstead of /home/user/.ssh. (You could also probably point SSH at the correct key, but I don't know how to do this, and SSH is picky about permissions.)

  • 将用户名放在您的 URL 中:ssh://[email protected]/soft.git。

  • 让你的SSH密钥提供给root用户,因为它会寻找下/root/.ssh代替/home/user/.ssh。(您也可以将 SSH 指向正​​确的密钥,但我不知道该怎么做,而且 SSH 对权限很挑剔。)

回答by Schwern

Normally the default remote ssh user is the same as your user name. If you're using sudothis will be rootwhich probably isnt' going to work. You need to supply the remote username.

通常默认远程 ssh 用户与您的用户名相同。如果您在使用sudo,这将是root这可能心不是”去上班。您需要提供远程用户名。

sudo git clone ssh://[email protected]/soft.git soft_git


You can generally resolve git ssh issues easier by trying to login to the remote with plain ssh. You'll get better diagnostics and can see what's going wrong.

您通常可以通过尝试使用普通 ssh 登录远程来更轻松地解决 git ssh 问题。您将获得更好的诊断结果,并可以看到出了什么问题。

sudo ssh ssh://URL.com/