ssh链接docker镜像ubuntu与debian

用密码登陆root

docker官网给的sshdemo是ubuntu的, https://docs.docker.com/engine/examples/running_ssh_service/
 
亲测能够
 
FROM ubuntu:16.04
RUN apt update
#sshd
RUN apt install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:aaaa' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-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"]

但python的官方镜像是基于debian的,用上面这个不行。python

参考这个 https://github.com/Azure-Samples/docker-django-webapp-linux/blob/master/Dockerfilelinux

实际上是python的debian里sshd_config选项的区别:

众所周知,sshd_config是sshd的配置文件,其中PermitRootLogin能够限定root用户经过ssh的登陆方式,如禁止登录、禁止密码登陆、仅容许密钥登录和开放登录,如下是对可选项的归纳:git

参数类别 是否容许ssh登录 登陆方式 交互shell
yes 容许 没有限制 没有限制
without-password 容许 除密码之外 没有限制
forced-commands-only 容许 仅容许使用密钥 仅容许已受权的命令
no 不容许 N/A N/A

以上选项中,yes和no的功能显而易见,只是很粗暴的容许、禁止root用户进行登录。without-password在yes的基础上,禁止了root用户使用密码登录。github

不知为何 ubuntu里不是without-password,而python /debian 里是。因此要用密码登陆,得
FROM python
LABEL author="xuqinghan"
LABEL purpose = ''

RUN apt-get update \
    && apt-get -q -y dist-upgrade \
    && apt-get -q -y install --no-install-recommends openssh-server
    #&& apt-get clean \
    #&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir /var/run/sshd
RUN echo 'root:aaaa' | 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"]

只改了一个地方,其余和ubuntu保持同样web

 

用公钥

只要把本机的ida_rsa.pub上传到容器里就OK了,容器扮演的角色 和Github同样。container里运行着openssh server,host做为客户端去链接ssh server。docker

只不过,ida_rsa.pub的位置要注意,dockerfile的语法里ADD 要绝对路径 ,COPY 要 当前dockerfile路径和子路径 才能用相对路径。shell

因此为了简单起见,仍是直接在外面复制出ida_rsa.pub到当前工程,而后再COPY。django

若是有多个客户端(再说)ubuntu

 

 

FROM python
LABEL author="xuqinghan"
LABEL purpose = ''

RUN apt-get update \
    && apt-get -q -y dist-upgrade \
    && apt-get -q -y install --no-install-recommends openssh-server
    #&& apt-get clean \
    #&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN mkdir /var/run/sshd
RUN echo 'root:aaaa' | chpasswd

RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

#在外面复制出id_rsa.pub
#cp ~/.ssh/id_rsa.pub ~/dev/id_rsa.pub

COPY id_rsa.pub /root/.ssh/authorized_keys

# 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"]
相关文章
相关标签/搜索