git 无法打开用户的 ssh/authorized_keys
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14819084/
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
Can't open ssh/authorized_keys of user
提问by Thoma Biguères
I know this may be trivial for some of you.
我知道这对你们中的一些人来说可能是微不足道的。
I'm not a linux expert, and I'm trying to play around with git.
To do so I wanted to try to add my public SSH key to the ~/.ssh/authorized_keys
of my user git.
我不是 linux 专家,我正在尝试使用 git。为此,我想尝试将我的公共 SSH 密钥添加到~/.ssh/authorized_keys
我的用户 git 中。
the problem though is that when I login with putty into my server with the git user, I can't access any file called ~/.ssh/authorized_keys
.
但问题是,当我使用 git 用户使用 putty 登录到我的服务器时,我无法访问任何名为~/.ssh/authorized_keys
.
So I tried to do that with root, maybe this is the solution, but I thought there was one authorized_keys
per user.
所以我试图用 root 来做到这一点,也许这就是解决方案,但我认为authorized_keys
每个用户都有一个。
I can see the authorized key, but I don't wannat mess everything up, so I would like to be clear on this one.
Is there a way to use my git user account and to modify the ssh/authorized_keys
?
我可以看到授权密钥,但我不想把一切搞砸,所以我想清楚这一点。有没有办法使用我的 git 用户帐户并修改ssh/authorized_keys
?
Thanks a lot!
非常感谢!
回答by Simon Whitaker
Sounds like you're almost there! I'm not sure exactly what you have and haven't done though, so I'll explain the whole process.
听起来你快到了!我不确定你到底做了什么,还没有做什么,所以我会解释整个过程。
First, I'm guessing (because you're using puTTY) that your computer runs Windows? If so, first you'll need to install Git for Windows, which you can download from the official Git website. Download it and install it, accepting the default choices in the installer.
首先,我猜(因为您使用的是 puTTY)您的计算机运行的是 Windows?如果是这样,首先您需要安装适用于 Windows 的 Git,您可以从Git 官方网站下载。下载并安装它,接受安装程序中的默认选项。
That will leave you with an item in your Start menu called Git Bash. You'll use this to perform what comes next. (You don't actually need Git itself installed, but the Git for Windows installer adds some additional tools like ssh-keygen
that you will need.)
这将在您的开始菜单中留下一个名为Git Bash 的项目。您将使用它来执行接下来的操作。(您实际上并不需要安装 Git 本身,但 Git for Windows 安装程序添加了一些ssh-keygen
您需要的其他工具。)
If your computer is actually running Linux or Mac OS X rather than Windows then you already have the tools you need. You can follow the same instructions, but instead of using Git Bash to enter commands, use a terminal window.
如果您的计算机实际上运行的是 Linux 或 Mac OS X 而不是 Windows,那么您已经拥有了所需的工具。您可以按照相同的说明进行操作,但不要使用 Git Bash 输入命令,而是使用终端窗口。
From now on, I'll just refer to typing things "in the terminal". If you're using Windows, type these things in the Git Bash window.
从现在开始,我将只提到“在终端中”输入内容。如果您使用的是 Windows,请在 Git Bash 窗口中键入这些内容。
Step 1: On your own computer, check for an SSH key pair
步骤 1:在您自己的计算机上,检查 SSH 密钥对
In the terminal, type:
在终端中,输入:
ls ~/.ssh/id_rsa*
This should list two files: id_rsa and id_rsa.pub. If they exist, move on to step 2. If not, type:
这应该列出两个文件:id_rsa 和 id_rsa.pub。如果它们存在,请转到第 2 步。如果不存在,请键入:
ssh-keygen
then follow the prompts to create them. Then run the ls
command again to confirm that they're now there.
然后按照提示创建它们。然后ls
再次运行该命令以确认它们现在在那里。
Step 2: Upload your public SSH key to the server
第 2 步:将您的公共 SSH 密钥上传到服务器
The public key is the one called id_rsa.pub. You can upload it to the server using the scp
command:
公钥是名为 id_rsa.pub 的那个。您可以使用以下scp
命令将其上传到服务器:
scp ~/.ssh/id_rsa.pub [email protected]
Enter the git user's password when prompted.
出现提示时输入 git 用户的密码。
Step 3: add your key to the git user's authorized_keys file
第 3 步:将您的密钥添加到 git 用户的 authorized_keys 文件中
First SSH in to the server as the git user:
首先以 git 用户身份通过 SSH 连接到服务器:
ssh [email protected]
Enter the git user's password again. Once you're logged in as the git user, type the following:
再次输入git用户的密码。以 git 用户身份登录后,键入以下内容:
mkdir -p ~/.ssh/
This will create the .ssh directory if it doesn't already exist. If it does exist, it doesn't do anything.
如果 .ssh 目录不存在,这将创建它。如果它确实存在,它不会做任何事情。
Now add your key to the authorized_keys file:
现在将您的密钥添加到 authorized_keys 文件中:
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
That will take the contents of id_rsa.pub, the file that you just uploaded, and add them to the end of the authorized_keys file. If authorized_keys doesn't exist, this command will create it first.
这将获取 id_rsa.pub 的内容,即您刚刚上传的文件,并将它们添加到 authorized_keys 文件的末尾。如果authorized_keys 不存在,此命令将首先创建它。
(Note: Be really careful to type two right angled brackets (>>
) in that command line. Two right angled brackets means appendthe contents of id_rsa.pub to the authorized_keys file. If you only use one that means replacethe contents of authorized_keys with the contents of id_rsa.pub, and you don't want to do that.)
(注意:>>
在该命令行中输入两个右尖括号 ( ) 时要非常小心。两个右尖括号表示将id_rsa.pub 的内容附加到 authorized_keys 文件中。如果只使用一个,则表示将authorized_keys 的内容替换为id_rsa.pub 的内容,而您不想这样做。)
You can check this has worked by running cat
on each file and making sure that you can see the contents of id_rsa.pub at the end of authorized_keys:
您可以通过cat
在每个文件上运行并确保您可以在authorized_keys 的末尾看到id_rsa.pub 的内容来检查这是否有效:
cat ~/id_rsa.pub
cat ~/.ssh/authorized_keys
Once you've confirmed that, delete id_rsa.pub; you won't need it again.
确认后,删除 id_rsa.pub;你不会再需要它了。
rm ~/.ssh/id_rsa.pub
Finally, set permissions on the .ssh directory and .ssh/authorized_keys so that only the owner of those files (the git user) can access them. Otherwise, the SSH server will refuse to use them. So:
最后,设置 .ssh 目录和 .ssh/authorized_keys 的权限,以便只有这些文件的所有者(git 用户)可以访问它们。否则,SSH 服务器将拒绝使用它们。所以:
chmod 700 ~/.ssh
chmod 400 ~/.ssh/authorized_keys
That makes the directory usable only by the git user, and the file inside it only accessible to the git user.
这使得目录只能由 git 用户使用,并且其中的文件只能由 git 用户访问。
You should find that you're now good to go!
你应该发现你现在可以开始了!
回答by ebneter
The .ssh directory and the file authorized_keys do not exist by default, you must create them. Make sure that the directory has permissions 0700 and the files in it have permissions 0600, or ssh will not work.
默认情况下,.ssh 目录和文件authorized_keys 不存在,您必须创建它们。确保目录的权限为 0700,其中的文件权限为 0600,否则 ssh 将无法工作。
回答by Zeveso
I had the same issue, this is what fixed it for me:
我遇到了同样的问题,这是为我解决的问题:
chown -R NEW_USER /home/NEW_USER
chown -R NEW_USER /opt/git
Turns out that I had the wrong file owner which I probably messed up when I was creating the user. Of course my new user was 'git'.
原来我有错误的文件所有者,我可能在创建用户时搞砸了。当然,我的新用户是“git”。
The way this messed up the SSH connect was that the user 'git' could not access '~/.ssh/authorized_keys'.
这弄乱了 SSH 连接的方式是用户“git”无法访问“~/.ssh/authorized_keys”。
Hope that helps someone else out.
希望能帮助别人。
回答by Bodie Leonard
The trick was to $ ssh-add ~/.ssh/id_custom
诀窍是 $ ssh-add ~/.ssh/id_custom
My problem was multiple git hub accounts on one local machine.
我的问题是在一台本地机器上有多个 git hub 帐户。