[Docker][SSH]远程登录Ubuntu¶
参考:
通过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-password
为PermitRootLogin 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:~#
自定义¶
-
主机
IP 127.0.0.1
可以替换为localhost
$ ssh root@localhost -p 32769
-
可以在启动容器时指定主机端口号
$ docker run -d -p 32212:22 --name test_sshd zjzstu/ubuntu:18.04-ssh
参数
-p
将容器端口22
映射到主机端口32212
环境变量¶
sshd
会在启动shell
之前对会对环境进行清理,通过Dockerfile
中ENV
指令输入的环境变量将会失效
有两种解决方法:
-
在
Dockerfile
中输入环境变量到shell
初始文件,比如/etc/profile
... # 无效 ENV NOTVISIBLE "in users profile" # 有效 RUN echo "export VISIBLE=now" >> /etc/profile EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"]
-
启动容器时手动设置
$ docker run -e ENV=value ...