zoukankan      html  css  js  c++  java
  • 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@sessions*requireds*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@sessions*requireds*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@sessions*requireds*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"]
     
  • 相关阅读:
    Building Apache Thrift on CentOS 6.5
    ToStringBuilder 学习
    对List中对象的去重
    MyEclipse启动Tomcat服务器时老是跳到Debug调试上
    JS 实现点击展开菜单
    详解公钥、私钥、数字证书的概念 转载
    eclipse svn 忽略 target目录 等等... 我用的后边的方法 (转载)
    Log4j XML 配置
    JS完成改变新闻字体大中小的显示
    Javascript 简单学习
  • 原文地址:https://www.cnblogs.com/xuanmanstein/p/8166210.html
Copyright © 2011-2022 走看看