如何从centos 6.4 ssh到没有密码的docker容器?

时间:2016-07-27 14:00:00

标签: ssh docker centos

我有一个CentOS 6.4实例。

在这个实例上,我不能用RSA密钥ssh到基于CentOS 6.7的Docker容器中。

在Ubuntu(Trusty)和Amazon Linux实例上,我可以直接进入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)的权限。

我可以将亚马逊Linux ssh到这个容器,这让我相信这个问题不是来自我的docker容器,也不是来自文件和文件夹的权限。

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

我已经将Python更新到2.7.12(因为这实际上是为了ansible ...无论如何)。

容器正在运行。

我删除了known_hosts。

使用ssh config。

-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

2 个答案:

答案 0 :(得分:1)

服务器端的常见问题是:

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

    1. 当用户搜索时(例如 centos @server):

      heroes

    2. 以root身份:

      chmod 600 ~/.ssh/authorized_keys

    3. 用户" centos" (如果需要,可以改变):

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

    4. 以root身份:

      passwd -u -f centos

    5. 在客户端,只记得分发密钥,密钥也应该有600个权限。例如。: sed -i 's/RSAAuthentication no/RSAAuthentication yes/g service sshd reload

答案 1 :(得分:0)

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

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
相关问题