用密碼登錄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的,用上面這個不行。
參考這個 https://github.com/Azure-Samples/docker-django-webapp-linux/blob/master/Dockerfile
          其實是python的debian里sshd_config選項的區別: 
        
 
        眾所周知,sshd_config是sshd的配置文件,其中PermitRootLogin可以限定root用戶通過ssh的登錄方式,如禁止登陸、禁止密碼登錄、僅允許密鑰登陸和開放登陸,以下是對可選項的概括:
| 參數類別 | 是否允許ssh登陸 | 登錄方式 | 交互shell | 
|---|---|---|---|
| yes | 允許 | 沒有限制 | 沒有限制 | 
| without-password | 允許 | 除密碼以外 | 沒有限制 | 
| forced-commands-only | 允許 | 僅允許使用密鑰 | 僅允許已授權的命令 | 
| no | 不允許 | N/A | N/A | 
以上選項中,yes和no的功能顯而易見,只是很粗暴的允許、禁止root用戶進行登陸。without-password在yes的基礎上,禁止了root用戶使用密碼登陸。
          不知為什么 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保持一樣
用公鑰
只要把本機的ida_rsa.pub上傳到容器里就OK了,容器扮演的角色 和Github一樣。container里運行着openssh server,host作為客戶端去連接ssh server。
只不過,ida_rsa.pub的位置要注意,dockerfile的語法里ADD 要絕對路徑 ,COPY 要 當前dockerfile路徑和子路徑 才能用相對路徑。
所以為了簡單起見,還是直接在外面復制出ida_rsa.pub到當前工程,然后再COPY。
如果有多個客戶端(再說)
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"]
