简体   繁体   English

使用 sudo 进行 git clone

[英]make git clone with sudo

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_rsa and id_rsa.pub are under /home/user/.ssh ssh 密钥id_rsaid_rsa.pub/home/user/.ssh

my purpose is the execute git with sudo but I got the following error我的目的是使用sudo执行 git,但出现以下错误

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/.ssh and 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。

When you run git using sudo, git will run as root.当您使用 sudo 运行 git 时,git 将以 root 身份运行。 Because git is running as root, ssh is running as root.因为 git 以 root 身份运行,所以 ssh 以 root 身份运行。 Because ssh is running as root, it is trying to log on to the remote server as root.因为 ssh 以 root 身份运行,所以它试图以 root 身份登录到远程服务器。 The remote server is not okay with this (as it should be!)远程服务器对此不满意(应该如此!)

You will need to do two things:你需要做两件事:

  • Put the username in your URL: ssh://myusername@URL.com/soft.git.将用户名放在您的 URL 中:ssh://myusername@URL.com/soft.git。

  • Make your SSH key available to the root user, because it will look under /root/.ssh instead of /home/user/.ssh .将您的 SSH 密钥提供给 root 用户,因为它会在/root/.ssh而不是/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.) (您也可以将 SSH 指向正​​确的密钥,但我不知道该怎么做,而且 SSH 对权限很挑剔。)

On my computer (Ubunutu 18.04), adding SSH_AUTH_SOCK=$SSH_AUTH_SOCK after sudo and before git fixed the problem:在我的电脑(Ubunutu 18.04)上,在sudo之后和git修复问题之前添加SSH_AUTH_SOCK=$SSH_AUTH_SOCK

sudo SSH_AUTH_SOCK=$SSH_AUTH_SOCK git clone git@github.com:my-github-account/my-repo.git 

Normally, sudo's SSH_AUTH_SOCK environment variable won't be set properly.通常,sudo 的SSH_AUTH_SOCK环境变量不会被正确设置。 Executing the git clone with SSH_AUTH_SOCK=$SSH_AUTH_SOCK sets sudo's SSH_AUTH_SOCK environment variable to whatever it is for you.使用SSH_AUTH_SOCK=$SSH_AUTH_SOCK执行 git clone SSH_AUTH_SOCK=$SSH_AUTH_SOCK sudo 的SSH_AUTH_SOCK环境变量设置为适合您的任何环境变量。

This way, you don't need to add an extra .ssh dir for sudo with copies of your keys, which is what I think one of the other answers suggests.这样,您无需为 sudo 添加额外的.ssh目录和您的密钥副本,我认为这是其他答案之一所建议的。

The solution is more fully explained in this rather old github gist: https://gist.github.com/scottjacobsen/4281310在这个相当古老的 github gist 中更全面地解释了解决方案: https : //gist.github.com/scottjacobsen/4281310

PS I'm adding a new answer several years later; PS 几年后我添加了一个新答案; I googled a solution to this problem, and this SO Q/A is one of the first things that comes up.我在谷歌上搜索了这个问题的解决方案,这个 Q/A 是最先出现的问题之一。

Normally the default remote ssh user is the same as your user name.通常默认远程 ssh 用户与您的用户名相同。 If you're using sudo this will be root which probably isnt' going to work.如果您正在使用sudo这将是root可能不会工作。 You need to supply the remote username.您需要提供远程用户名。

sudo git clone ssh://username@URL.com/soft.git soft_git

You can generally resolve git ssh issues easier by trying to login to the remote with plain ssh.您通常可以通过尝试使用普通 ssh 登录远程来更轻松地解决 git ssh 问题。 You'll get better diagnostics and can see what's going wrong.您将获得更好的诊断结果,并可以看到出了什么问题。

sudo ssh ssh://URL.com/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM