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"]
相關文章
相關標籤/搜索