ssh 故障排除,连接到 alpine docker image ssh_exchange_identification: Connection closed by remote host
Posted
技术标签:
【中文标题】ssh 故障排除,连接到 alpine docker image ssh_exchange_identification: Connection closed by remote host【英文标题】:ssh troubleshooting, connecting to alpine docker image ssh_exchange_identification: Connection closed by remote host 【发布时间】:2021-09-18 21:22:10 【问题描述】:我有一个基于 alpine 的 dockerimage,想要在其中启用 ssh 连接(我使用这个镜像进行本地开发,上面已经安装了 php 和其他东西)
Dockerfile
FROM alpine:3.13
# utils
RUN apk add openssh \
&& apk add nano
#Enable ssh login
RUN apk add openrc && rc-update add sshd \
&& sed -i '/^#Port 22/s/^#//' /etc/ssh/sshd_config \
&& sed -i '/^#PasswordAuthentication/s/^#//' /etc/ssh/sshd_config \
&& sed -i '/^#PermitEmptyPasswords no/s/^#PermitEmptyPasswords no/PermitEmptyPasswords yes/' /etc/ssh/sshd_config \
&& sed -i '/^#PermitRootLogin prohibit-password/s/^#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config \
&& echo "root:Docker!" | chpasswd
我尝试从来宾主机进行如下连接并出现错误(端口 2022 是正确的,我在 docker-compose 22 中将端口转发到那个端口):
root@vagrant:/docker# ssh -vvv root@localhost -p 2022
OpenSSH_7.2p2 Ubuntu-4ubuntu2.8, OpenSSL 1.0.2g 1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "localhost" port 2022
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to localhost [::1] port 2022.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.8
ssh_exchange_identification: Connection closed by remote host
我使用ubuntu
基本映像进行了类似的设置工作正常,现在正在迁移到 alpine 但不明白为什么不起作用。
在/etc/ssh/sshd_config
的docker容器内配置sshd:
# $OpenBSD: sshd_config,v 1.103 2018/04/09 20:41:22 tj Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/bin:/usr/bin:/sbin:/usr/sbin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options override the
# default value.
Port 22
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
#HostKey /etc/ssh/ssh_host_ed25519_key
# Ciphers and keying
#RekeyLimit default none
# Logging
#SyslogFacility AUTH
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6
#MaxSessions 10
#PubkeyAuthentication yes
# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2
# but this is overridden so installations will only check .ssh/authorized_keys
AuthorizedKeysFile .ssh/authorized_keys
#AuthorizedPrincipalsFile none
#AuthorizedKeysCommand none
#AuthorizedKeysCommandUser nobody
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
PasswordAuthentication yes
PermitEmptyPasswords yes
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication and
# PasswordAuthentication. Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
#AllowAgentForwarding yes
# Feel free to re-enable these if your use case requires them.
AllowTcpForwarding no
GatewayPorts no
X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PermitTTY yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS no
#PidFile /run/sshd.pid
#MaxStartups 10:30:100
#PermitTunnel no
#ChrootDirectory none
#VersionAddendum none
# no default banner path
#Banner none
# override default of no subsystems
Subsystem sftp /usr/lib/ssh/sftp-server
# Example of overriding settings on a per-user basis
#Match User anoncvs
# X11Forwarding no
# AllowTcpForwarding no
# PermitTTY no
# ForceCommand cvs server
【问题讨论】:
我认为sshd
没有在您的容器中运行。
【参考方案1】:
sshd 没有在您的容器中运行。如果您在包含此文件的目录中创建一个名为 entrypoint.sh
的文件
#!/bin/sh
ssh-keygen -A
/usr/sbin/sshd -D -e
并将您的 Dockerfile 更改为
FROM alpine:3.13
# utils
RUN apk add openssh \
&& apk add nano
#Enable ssh login
RUN apk add openrc && rc-update add sshd \
&& sed -i '/^#Port 22/s/^#//' /etc/ssh/sshd_config \
&& sed -i '/^#PasswordAuthentication/s/^#//' /etc/ssh/sshd_config \
&& sed -i '/^#PermitEmptyPasswords no/s/^#PermitEmptyPasswords no/PermitEmptyPasswords yes/' /etc/ssh/sshd_config \
&& sed -i '/^#PermitRootLogin prohibit-password/s/^#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config \
&& echo "root:Docker!" | chpasswd
COPY entrypoint.sh /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
然后就可以了
【讨论】:
我在容器内手动运行并工作。谢谢,我以为这个/etc/init.d/sshd start
启动了它(手动运行)
这只是覆盖了原来的 docker cmd
命令,所以现在 nginx 没有运行
在一个容器中运行多个进程可能有点棘手。尝试从这里开始,看看你想走哪条路:docs.docker.com/config/containers/multi-service_container
这不起作用 sshd 正在阻止调用启动主管,尝试也作为守护进程运行 &
#!/bin/sh # start ssh service ssh-keygen -A /usr/sbin/sshd -D -e #start phpfpm and nginx /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
我之前在 ubuntu 容器中的入口点是这样的,并且运行良好 #!/usr/bin/env bash php-fpm7.1; service ssh start; nginx -g 'daemon off;'
【参考方案2】:
在您的示例中,您没有在容器中启动 sshd
(或其他任何内容)。只需对您的 Dockerfile
进行细微更改...
FROM alpine:3.13
# utils
RUN apk add openssh \
&& apk add nano
#Enable ssh login
RUN apk add openrc && rc-update add sshd \
&& sed -i '/^#Port 22/s/^#//' /etc/ssh/sshd_config \
&& sed -i '/^#PasswordAuthentication/s/^#//' /etc/ssh/sshd_config \
&& sed -i '/^#PermitEmptyPasswords no/s/^#PermitEmptyPasswords no/PermitEmptyPasswords yes/' /etc/ssh/sshd_config \
&& sed -i '/^#PermitRootLogin prohibit-password/s/^#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config \
&& echo "root:Docker!" | chpasswd
RUN sed -i '/^tty/ s/^/#/' /etc/inittab
CMD ["/sbin/init"]
...我可以成功连接:
[lars@myhost ~]$ ssh -p 2022 root@localhost
Warning: Permanently added '[localhost]:2022' (ED25519) to the list of known hosts.
root@localhost's password:
Welcome to Alpine!
The Alpine Wiki contains a large amount of how-to guides and general
information about administrating Alpine systems.
See <http://wiki.alpinelinux.org/>.
You can setup the system with the command: setup-alpine
You may change this message by editing /etc/motd.
7d6687d73ed3:~#
【讨论】:
以上是关于ssh 故障排除,连接到 alpine docker image ssh_exchange_identification: Connection closed by remote host的主要内容,如果未能解决你的问题,请参考以下文章
对无法连接到 MySQL 的 Spring Boot 应用程序进行故障排除
一步一步:如何进行 Xdebug 故障排除连接到客户端 IDE
2020-12-08centos 出现ssH不能连接的故障排除