无法使用Ubuntu 16.04登录Dockerized SSH守护程序服务

时间:2016-09-07 18:11:33

标签: ubuntu ssh docker dockerfile

当我从Docker website执行以下示例时,它可以正常工作。但是,只要我将其更改为ubuntu:16.04screencast密码就无法运行。它只是一直提示输入正确的密码。 16.04chpasswd有什么不同吗?

# sshd
#
# VERSION               0.0.2

FROM ubuntu:14.04
MAINTAINER Sven Dowideit <SvenDowideit@docker.com>

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

更新:我还尝试用CMD ["/usr/sbin/sshd", "-D"]替换最后一行RUN service ssh restart,但它仍然无法正常工作。让我对何时使用service ssh restart/usr/sbin/sshd -D

感到好奇

2 个答案:

答案 0 :(得分:0)

一般来说,if you are trying to ssh into a container, something might be wrongnsenter命令现在是docker exec,但帖子的基本要点仍然存在。)

Ubuntu ssh config

Ubuntu 14:04 /etc/ssh/sshd_config

root@1404:/# grep PermitRootLogin /etc/ssh/sshd_config 
PermitRootLogin without-password
# the setting of "PermitRootLogin without-password".

默认的Ubuntu 16:04 /etc/ssh/sshd_config包含更新的PermitRootLogin配置。

root@1604:/# grep PermitRootLogin /etc/ssh/sshd_config 
PermitRootLogin prohibit-password
# the setting of "PermitRootLogin without-password".

sed命令's/PermitRootLogin without-password/PermitRootLogin yes/'只会更改16.04配置中的注释,而不是所需的配置行。

修复

使用sed -ir 's/^\s*PermitRootLogin\s+.+/PermitRootLogin yes/' /etc/ssh/sshd_config

正则表达式分解为

^      # Start of line
\s*    # 0 or many white spaces
PermitRootLogin
\s+    # 1 or many white spaces
.+     # anything else on the line

因此,请使用PermitRootLogin *替换以PermitRootLogin yes开头的所有非注释行。您还可以contribute your updates back to Docker

答案 1 :(得分:0)

在拉动最新版本的Ubuntu(18.04)时,我也遇到了此问题。原始解决方案无法解决该问题,因为在此版本中,关键字的前缀为“#”。无论您使用的是14:04、16:04还是18:04,以下更新的语法都会对其进行修复:

sed -ir 's/^#*PermitRootLogin .*/PermitRootLogin yes/' /etc/ssh/sshd_config