gpt4 book ai didi

ubuntu - SSH 服务器不接受连接

转载 作者:行者123 更新时间:2023-12-02 20:52:18 24 4
gpt4 key购买 nike

我使用 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

最佳答案

您正在尝试以 root 身份连接这在 /etc/ssh/sshd_config 中被禁止作为标准选项在容器中。您要么需要设置 user在您的 Dockerfile或允许 root-login或复制您的 Dockerfile 中的公钥.我不鼓励你允许 root -登录并通过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 .

关于ubuntu - SSH 服务器不接受连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25252845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com