【问题标题】:Error while trying to ssh a docker container : System is booting up尝试 ssh docker 容器时出错:系统正在启动
【发布时间】:2020-02-29 02:49:42
【问题描述】:

我正在尝试构建映像并将容器作为 ssh 服务器运行。 我希望能够从另一个容器 (jenkins/jenkins) SSH 该容器 (remote_host)

我正在使用带有 Centos 的 VM。

我正在使用这个 docker 文件。我从我的 VM 主机上运行它(Centos 也是)

FROM centos

RUN yum -y install openssh-server openssh-clients

RUN useradd -ms /bin/bash remote_user && \
  echo 'remote_user:12345' | chpasswd && \
  mkdir /home/remote_user/.ssh && \
  chmod 700 /home/remote_user/.ssh

COPY remote-key.pub /home/remote_user/.ssh/authorized_keys

RUN chown remote_user:remote_user -R /home/remote_user/.ssh && \
    chmod 600 /home/remote_user/.ssh/authorized_keys

EXPOSE 22
RUN /usr/bin/ssh-keygen -A
CMD ["/usr/sbin/sshd", "-D"]

我的 docker 撰写文件

---
networks:
  net: ~
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins
    networks:
      - net
    ports:
      - "8080:8080"
    volumes:
      - "$PWD/jenkins_home:/var/jenkins_home"
  remote_host:
    container_name: remote-host
    image: remote-host
    build:
      context: centos7
    networks:
      - net
version: "3"

我从我的主机运行docker-compose build

Building remote_host
Step 1/8 : FROM centos
 ---> 0f3e07c0138f
Step 2/8 : RUN yum -y install openssh-server openssh-clients
 ---> Using cache
 ---> 277411f7cc41
Step 3/8 : RUN useradd -ms /bin/bash remote_user &&   echo 'remote_user:12345' | chpasswd &&   mkdir /home/remote_user/.ssh &&   chmod 700 /home/remote_user/.ssh
 ---> Using cache
 ---> c42b15de9da7
Step 4/8 : COPY remote-key.pub /home/remote_user/.ssh/authorized_keys
 ---> Using cache
 ---> f205521e83cb
Step 5/8 : RUN chown remote_user:remote_user -R /home/remote_user/.ssh &&     chmod 600 /home/remote_user/.ssh/authorized_keys
 ---> Using cache
 ---> a7bb438b87ed
Step 6/8 : EXPOSE 22
 ---> Using cache
 ---> 7f28ef8e4ec9
Step 7/8 : RUN /usr/bin/ssh-keygen -A
 ---> Using cache
 ---> a4fae9730627
Step 8/8 : CMD ["/usr/sbin/sshd", "-D"]
 ---> Using cache
 ---> 3fe69c9789a6
Successfully built 3fe69c9789a6
Successfully tagged remote-host:latest

然后我运行docker-compose up -d

docker ps 给我:

0f9987444fcf        remote-host         "/usr/sbin/sshd -D"      28 minutes ago      Up 16 minutes       22/tcp                              remote-host
4c9ba830f419        jenkins/jenkins     "/sbin/tini -- /usr/…"   7 hours ago         Up 7 hours          0.0.0.0:8080->8080/tcp, 50000/tcp   jenkins 

我 ssh 我的第一个容器:

docker exec -it jenkins bash

然后当我尝试从第一个容器 ssh 到第二个容器时

ssh remote_user@remote_host

我收到了这个错误

Are you sure you want to continue connecting (yes/no)?
remote_user@remote_host's password:12345
"System is booting up. Unprivileged users are not permitted to log in yet. Please come back later. For technical details, see pam_nologin(8)."
Authentication failed.

【问题讨论】:

  • 您的ssh 服务器日志说什么?尝试过基于密钥的身份验证吗?我有一个 openssh-server build 如果有帮助的话。
  • 谢谢 masseyb,我会试试你的 openssh 服务器

标签: docker docker-compose centos dockerfile openssh


【解决方案1】:

解决方案:

请像这样编辑您的 dockerfile:

FROM centos
RUN yum -y install openssh-server
RUN useradd remote_user && \
    echo remote_user:1234 | chpasswd && \
    mkdir /home/remote_user/.ssh && \
    chmod 700 /home/remote_user/.ssh
COPY remote-key.pub /home/remote_user/.ssh/authorized_keys
RUN chown remote_user:remote_user -R /home/remote_user/.ssh && \
    chmod 600 /home/remote_user/.ssh/authorized_keys
RUN /usr/bin/ssh-keygen -A
EXPOSE 22
RUN rm -rf /run/nologin
CMD /usr/sbin/sshd -D

【讨论】:

  • 我不会添加具有默认密码的用户,如果有人在生产环境中运行它而不在每次生成新容器/扩展图像时禁用用户,这将是一个安全风险。此外,您正在复制授权密钥(我会在运行时绑定挂载它们 - 同样的问题,如果不删除/破坏您的密钥,我会让您访问我的环境),可以完全禁用密码身份验证。
【解决方案2】:

如果远程主机不在启动过程中,则登录远程主机,成为root并删除/run/nologin文件。

【讨论】:

    【解决方案3】:

    解决方案 1:

    将此命令添加到您的代码中

    RUN rm -rf /run/nologin
    

    以上命令清除中间用户(临时用户)。

    解决方案 2:

    如果它不起作用,只需重新启动您的 VirtualMachine。

    我希望我的解决方案能正常工作。

    【讨论】:

      【解决方案4】:

      找到解决方案使用

      ssh-keygen -f "/var/jenkins_home/.ssh/known_hosts" -R "remote_host"
      

      【讨论】:

      • 谢谢大家。使用 ssh-keygen -f "/var/jenkins_home/.ssh/known_hosts" -R "remote_host" 找到了解决方案
      【解决方案5】:

      要回答您的问题,请使用以下命令

      ssh-keygen -f "/var/jenkins_home/.ssh/known_hosts" -R "remote_host"
      

      但是,即使在相同的情况下,我的权限也被拒绝

      【讨论】:

        猜你喜欢
        • 2018-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-07
        • 1970-01-01
        • 2017-11-29
        • 1970-01-01
        相关资源
        最近更新 更多