[Docker][SSH]远程登录Ubuntu

参考:

ssh远程连接docker中的container

Dockerize an SSH service

通过docker启动Ubuntu镜像,通过ssh进行远程登录

Dockerfile

参考:sshd命令

编写Dockerfile文件实现ssh应用安装,密码设置以及sshd命令后台运行

FROM zjzstu/ubuntu:18.04

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:THEPASSWORDYOUCREATED' | 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

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
  • FROM指令使用zjzstu/ubuntu:18.04作为父镜像
  • RUN指令更新已安装应用,同时安装openssh-server
  • RUN指令创建文件夹/var/run/sshd
  • RUN指令指定登录密码,修改THEPASSWORDYOUCREATED为自定义密码
  • RUN指令允许ssh登录root账户,修改文件/etc/ssh/sshd_config中的#PermitRootLogin prohibit-passwordPermitRootLogin yes
    • 对于Ubuntu 14.04而言,默认设置为PermitRootLogin without-password
    • 注意:对于Ubuntu 16.04而言,ssh默认打开了PermitRootLogin功能;对于Ubuntu 18.04而言,ssh默认关闭了PermitRootLogin功能。所以如果使用Ubuntu 16.04,需要将#号去除
  • RUN指令设置/etc/pam.d/sshd,防止用户在登录后将被踢出
  • EXPOSE指令设置容器监听端口22
  • CMD指令设置容器启动sshd,并在后台运行(-D表示以后台守护进程方式运行服务器)

执行

首先生成镜像zjzstu/ubuntu:18.04-ssh,实现如下:

$ docker build -t zjzstu/ubuntu:18.04-ssh .

启动镜像生成容器test_sshd,实现如下:

$ docker run -d -P --name test_sshd zjzstu/ubuntu:18.04-ssh
1b44046832b4fd13b1c60921567a431e5aff3bd4fe753ebb6c1e29b2df277828
  • 参数-d表示后台运行
  • 参数-P表示将容器的端口(22)发布到主机
  • 参数--name表示设置容器名

使用命令docker port查询容器映射的主机端口

$ docker ps
CONTAINER ID        IMAGE                     COMMAND               CREATED              STATUS              PORTS                   NAMES
1b44046832b4        zjzstu/ubuntu:18.04-ssh   "/usr/sbin/sshd -D"   About a minute ago   Up About a minute   0.0.0.0:32769->22/tcp   test_sshd

$ docker port 1b44
22/tcp -> 0.0.0.0:32769

即可在主机使用ssh连接容器test_sshd

$ ssh root@127.0.0.1 -p 32769
The authenticity of host '[127.0.0.1]:32769 ([127.0.0.1]:32769)' can't be established.
ECDSA key fingerprint is SHA256:LYUoJeWTbnRgJDxjvtAbukkhVRRXPiDHliUPg/c8wyI.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:32769' (ECDSA) to the list of known hosts.
root@127.0.0.1's password: 
...
...
root@1b44046832b4:~# 

自定义

  1. 主机IP 127.0.0.1可以替换为localhost

    $ ssh root@localhost -p 32769
    
  2. 可以在启动容器时指定主机端口号

    $ docker run -d -p 32212:22 --name test_sshd zjzstu/ubuntu:18.04-ssh
    

    参数-p将容器端口22映射到主机端口32212

环境变量

sshd会在启动shell之前对会对环境进行清理,通过DockerfileENV指令输入的环境变量将会失效

有两种解决方法:

  1. Dockerfile中输入环境变量到shell初始文件,比如/etc/profile

    ...
    # 无效
    ENV NOTVISIBLE "in users profile"
    # 有效
    RUN echo "export VISIBLE=now" >> /etc/profile
    
    EXPOSE 22
    CMD ["/usr/sbin/sshd", "-D"]
    
  2. 启动容器时手动设置

    $ docker run -e ENV=value ...