【问题标题】:SSH server does not accept connectionsSSH 服务器不接受连接
【发布时间】:2014-10-04 20:18:37
【问题描述】:

我使用 Docker 在本地构建了一个 Docker 映像,它运行 SSH 服务器 + 一些其他服务。 Dockerfile 是内部的,所以我不能在这里发布。但我做的基本事情是:

RUN apt-get install -q -y openssh-server openssh-client

RUN cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults

RUN chmod a-w /etc/ssh/sshd_config.factory-defaults

RUN mkdir /var/run/sshd

RUN echo 'root:changeme' |chpasswd

ADD ./bootstrap.sh /root/bootstrap.sh

ENTRYPOINT ["sh", "/root/bootstrap.sh"]

在 bootstrap.sh 内部:

...
echo "Starting SSH service..."
/usr/sbin/sshd -D > /dev/null 2>&1 &
ps -A | grep sshd
...

...到目前为止它仍然有效。 我可以从主机连接到 SSH 服务器(在 Ubuntu 容器内运行)。

到目前为止,一切都很好。现在我已将图像上传到 Dockerhub 并在 tutum.co 上运行它。

现在问题:SSH 服务器启动,但我无法连接到它。甚至在容器的局部内部。顺便说一句,我有一个浏览器外壳,所以我仍然能够执行命令。

我在容器内执行:

ssh root@localhost -v                        
OpenSSH_5.9p1 Debian-5ubuntu1.4, OpenSSL 1.0.1 14 Mar 2012                      
debug1: Reading configuration data /etc/ssh/ssh_config                          
debug1: /etc/ssh/ssh_config line 19: Applying options for *                     
debug1: Connecting to localhost [::1] port 22.                                  
debug1: Connection established.                                                 
debug1: permanently_set_uid: 0/0                                                
debug1: identity file /root/.ssh/id_rsa type -1                                 
debug1: identity file /root/.ssh/id_rsa-cert type -1                            
debug1: identity file /root/.ssh/id_dsa type -1                                 
debug1: identity file /root/.ssh/id_dsa-cert type -1                            
debug1: identity file /root/.ssh/id_ecdsa type -1                               
debug1: identity file /root/.ssh/id_ecdsa-cert type -1                          
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debia
n-5ubuntu1.4                                                                    
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1.4 pat OpenSSH*                     
debug1: Enabling compatibility mode for protocol 2.0                            
debug1: Local version string SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.4            
debug1: SSH2_MSG_KEXINIT sent                                                   
Read from socket failed: Connection reset by peer 

【问题讨论】:

    标签: ubuntu ssh docker openssh


    【解决方案1】:

    您正在尝试以root 的身份进行连接,这在容器中的/etc/ssh/sshd_config 中作为标准选项被禁止。您需要在Dockerfile 中设置user 或允许root-login 或在Dockerfile 中复制公钥。我不鼓励您允许root-login 和通过Dockerfile 复制您的公钥。我的解决方案是通过以下方式编辑您的Dockerfile

    # Set root passwd
    RUN echo 'root:changeme' |chpasswd
    
    # Add user so that container does not run as root
    RUN useradd -m michaelkunzmann
    RUN echo "michaelkunzmann:test" | chpasswd
    RUN usermod -s /bin/bash michaelkunzmann
    RUN usermod -aG sudo michaelkunzmann
    ENV HOME /home/michaelkunzmann
    ENV HOSTNAME michaelkunzmannsbox
    

    然后构建镜像并通过ssh michaelkunzmann@localhost -v 登录。 (如果这不起作用,请确保您使用的是正确的port。您可以指定它,例如使用docker run -d -p 127.0.0.1:5000:22 -P --name="name-your-container!" your-image。然后您可以通过以下方式登录 ssh michaelkunzmann@localhost -p 5000.)

    顺便说一句,如果您在容器中运行多个进程,您应该熟悉Supervisor

    【讨论】:

    • 如果这不起作用,请回复我。我今天遇到了同样的问题,并且我得到了它的工作,所以我有一些可能的解决方案。
    • 根对我的用例来说不是问题,实际上我的用例需要根。问题一定出在 tutum.co,因为它在本地工作。
    • 好的,我会在今天晚些时候回复您!
    • 密码长度可能存在问题。这是一个服务器错误。它并不为人所知,但有一些人遇到过。您可以尝试在您的ssh_config 中设置Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc 并告诉我它是否有效。基本上,这个想法是通过排除使用较长密码的选项来减少密码长度。
    【解决方案2】:

    我的 docker 文件供像我这样的其他人参考:

    FROM centos:6
    
    RUN yum install -y openssh-server
    RUN mkdir /var/run/sshd
    
    RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
    RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
    
    RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
    RUN sed -ri 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config
    
    RUN echo 'root:root' | chpasswd
    
    CMD ["/usr/sbin/sshd", "-D"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 2021-11-01
      • 1970-01-01
      相关资源
      最近更新 更多