如何在没有密码的情况下将centos6.4 ssh到docker容器中



我有一个CentOS 6.4实例。

在这个例子中,我无法使用RSA密钥将其ssh到基于CentOS6.7的Docker容器中。

在Ubuntu(Trusty)和AmazonLinux实例上,我可以ssh到Docker容器中。

我需要使用ssh命令(真正的ansible),而不是docker exec

我运行的命令是ssh -i id_rsa -p 2200 user@localhost

我的Dockerfile看起来是这样的:

From centos:6.7
#update yum repository and install openssh server 
RUN yum update -y
RUN yum install -y openssh-server
RUN yum install -y sudo
RUN useradd user
RUN echo "user  ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN mkdir -p /home/user/.ssh
ADD id_rsa.pub /home/user/.ssh/authorized_keys
RUN chown user /home/user/.ssh -R
RUN chmod 600 /home/user/.ssh/authorized_keys
#generate ssh key 
RUN ssh-keygen -q -N "" -t rsa -f /etc/ssh/ssh_host_rsa_key
EXPOSE  22
CMD ["sh","-c","/usr/sbin/sshd -D;sleep 1"]

我已经检查了所有文件(私钥和公钥,authorized_keys)和目录(/.ssh)的权限

事实上,我可以在amazonLinux上对这个容器进行ssh,这让我相信这个问题不是来自我的docker容器,也不是来自对文件和文件夹的权限。

我已经在Docker容器和本地的CentOS中更改了PAM。

我已经将Python更新到2.7.12(因为这真的是为了ansible……不管怎样)。

容器正在运行。

我已经删除了known_hosts。

使用ssh配置播放。

当将-vvvvv添加到我的ssh命令时,我得到了这个问题:

debug3: Not a RSA1 key file /path/to/project/dir/id_rsa.
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug3: key_read: missing keytype
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug3: key_read: missing whitespace
debug2: key_type_from_name: unknown key type '-----END'
debug3: key_read: missing keytype

服务器端常见的问题有:

  1. ~/.ssh/authorized_keys上的权限应为400/600(仅限所有者的权限)
  2. PAM安全性可能是一个问题,您应该禁用PAM sshd_config
  3. 必须启用将"进入"服务器的用户(例如centos)(出现在/etc/shadow上)
  4. 应该在sshd_config上启用RSA

解决方案:

  1. 当用户登录时(例如centos@服务器):

    chmod 600 ~/.ssh/authorized_keys

  2. 作为根:

    sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config service sshd reload

  3. 对于用户"centos"(如果需要更改):

    passwd -u -f centos

  4. 作为根:

    sed -i 's/RSAAuthentication no/RSAAuthentication yes/g service sshd reload


在客户端,只需记住分发密钥,密钥也应该具有600权限。例如:ssh -i myrsa.key centos@server

在您的主机中尝试此操作:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
sed -i 's|#AuthorizedKeysFile|AuthorizedKeysFile|g' /etc/ssh/sshd_config
sed -i 's|.ssh/authorized_keys|%h/.ssh/authorized_keys|g' /etc/ssh/sshd_config

最新更新