Clo*_*One 3 linux ssh openssh ubuntu docker
我正在尝试设置一个 Ubuntu 容器,openssh-server以便我可以从主机 ssh 进入它。我知道这不是标准的做法,但我真的很想这样做ssh。
这是我的Dockerfile
# Select base image\nFROM ubuntu:16.04\n\n# Set the current working directory\nWORKDIR /home\n\n# Update the system, download any packages essential for the project\nRUN dpkg --add-architecture i386\nRUN apt-get update && apt-get upgrade -y\nRUN apt-get install -y git build-essential make gcc vim net-tools iputils-ping ca-certificates openssh-server libc6:i386 libstdc++6:i386\n\n# Allow ssh root login\nRUN echo "root:root" | chpasswd\n\n# RUN rpl "PermitRootLogin prohibit-password" "PermitRootLogin yes" /etc/ssh/sshd_config\nRUN sed -i \'s/prohibit-password/yes/\' /etc/ssh/sshd_config\n\nRUN cat /etc/ssh/sshd_config\nRUN mkdir /root/.ssh\n\nRUN chown -R root:root /root/.ssh;chmod -R 700 /root/.ssh\n\nRUN echo \xe2\x80\x9cStrictHostKeyChecking=no\xe2\x80\x9d >> /etc/ssh/ssh_config\n\nRUN service ssh restart\n\n\n# Open port 22 so linked containers can see it\nEXPOSE 22\n\n# Import any additional files into the environment (from the host)\nADD otherfile .\nRun Code Online (Sandbox Code Playgroud)\n\n我启动容器,docker run -t -d -p 2222:22但每当我尝试 ssh 进入它时,我总是会收到错误ssh_exchange_identification: Connection closed by remote host:
\xe2\x9e\x9c ssh -v -p 2222 root@localhost /bin/bash\nOpenSSH_7.9p1, LibreSSL 2.7.3\ndebug1: Reading configuration data /Users/giorgio/.ssh/config\ndebug1: Reading configuration data /etc/ssh/ssh_config\ndebug1: /etc/ssh/ssh_config line 48: Applying options for *\ndebug1: /etc/ssh/ssh_config line 52: Applying options for *\ndebug1: Connecting to localhost port 2222.\ndebug1: Connection established.\ndebug1: identity file /Users/giorgio/.ssh/id_rsa type -1\ndebug1: identity file /Users/giorgio/.ssh/id_rsa-cert type -1\ndebug1: identity file /Users/giorgio/.ssh/id_dsa type -1\ndebug1: identity file /Users/giorgio/.ssh/id_dsa-cert type -1\ndebug1: identity file /Users/giorgio/.ssh/id_ecdsa type -1\ndebug1: identity file /Users/giorgio/.ssh/id_ecdsa-cert type -1\ndebug1: identity file /Users/giorgio/.ssh/id_ed25519 type -1\ndebug1: identity file /Users/giorgio/.ssh/id_ed25519-cert type -1\ndebug1: identity file /Users/giorgio/.ssh/id_xmss type -1\ndebug1: identity file /Users/giorgio/.ssh/id_xmss-cert type -1\ndebug1: Local version string SSH-2.0-OpenSSH_7.9\nssh_exchange_identification: Connection closed by remote host\nRun Code Online (Sandbox Code Playgroud)\n\n有谁知道导致此错误的原因以及如何修复它?
\n小智 5
RUN service ssh restart
这会在镜像创建阶段运行 ssh 服务重启(实际上是启动),而不是在未来运行的容器中运行。你没有CMD也没有,所以它默认为你的基础镜像中配置的(即 bash)ENTRYPOINTDockerfile
换句话说,当您启动容器时,没有 ssh 守护进程在运行。临时解决方案是在正在运行的容器上启动 exec 命令:docker exec your_container_name service ssh start
要正确解决此问题,您需要指示映像在创建容器时启动 sshd(请参阅docker 文档中的dockerize an ssh 服务)。简而言之:
RUN service ssh restart线RUN mkdir /var/run/sshd
CMD ['/usr/sbin/sshd', '-D']
Run Code Online (Sandbox Code Playgroud)