简体   繁体   English

有效的authorized_keys文件?

[英]Valid authorized_keys file?

I have a piece of software I wrote using Python & Paramiko which distributes a public ssh key to a target linux machine. 我有一个使用Python和Paramiko编写的软件,该软件将公共ssh密钥分发给目标linux机器。 Then it tries to connect using the private key. 然后,它尝试使用私钥进行连接。 On one machine (RHEL version 5), it works with no problem. 在一台计算机(RHEL版本5)上,它可以正常工作。 On other machines (CentOS, RHEL version 6), something peculiar happens. 在其他计算机(CentOS,RHEL版本6)上,发生了一些奇怪的事情。 The public key gets distributed but the server refuses the private key (I use plink for testing). 公钥被分发,但是服务器拒绝私钥(我使用plink进行测试)。 I then do the following (in .ssh directory): 然后,我执行以下操作(在.ssh目录中):

cp authorized_keys _temp
cat _temp > authorized_keys
chown testuser authorized_keys
chmod 600 authorized_keys

Then the authentication works (just as before). 然后身份验证工作(就像以前一样)。 So this rules out sshd configuration. 因此,这排除了sshd配置。 Also, as I mentioned before, the distribution works in another machine and authentication is successful with the same distribution mechanism. 而且,正如我之前提到的,分发可以在另一台机器上进行,并且使用相同的分发机制可以成功进行身份验证。 So I guess the distribution is ok. 所以我想分布还可以。 It is probably related to the text file format. 它可能与文本文件格式有关。 I ruled out CR+LF instead of LF by getting the authorized_keys file back to my machine and inspecting it - it had LF only. 通过将authorized_keys文件返回到我的机器并进行检查,我排除了CR + LF而不是LF-它只有LF。

So the only thing I can say for sure currently is that somehow, cat and > (or just one of them) "fixed" the file, but I can't really say which one and how. 因此,我目前只能确定的是,cat和>(或其中一个)以某种方式“修复”了文件,但是我不能真正说出哪个和如何。 Since I would like the process to work without manual intervention and with as little workarounds as possible -> Can anyone explain this? 由于我希望该过程能够在没有人工干预的情况下工作,并且需要尽可能少的解决方法->有人可以解释吗?

Thanks! 谢谢!

You could try to use the dos2unix command to fix the whitespaces: 您可以尝试使用dos2unix命令修复空白:

dos2unix authorized_keys

But I suspect that this could be also a selinux problem. 但是我怀疑这可能也是selinux的问题。 If you visualize the selinux context with ls : 如果使用ls可视化selinux上下文:

ls -lZ .ssh/*

you should get something like 你应该得到像

-rw-------. -rw -------。 git git unconfined_u:object_r:ssh_home_t:s0 authorized_keys git git unconfined_u:object_r:ssh_home_t:s0授权密钥

see the ssh_home_t context. 请参阅ssh_home_t上下文。 If you don't see that, you need to restore it with restorecon : 如果没有看到,则需要使用restorecon还原它:

restorecon -r .ssh

In general, you can use the audit2why command to see what went wrong with selinux issues: 通常,您可以使用audit2why命令查看selinux问题出了什么问题:

audit2why /var/log/audit/audit.log

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

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