Dockerfile:制作可ssh登录的镜像

简介:

    我们先手动制作一个可以ssh登录的容器,然后按照操作步骤编写Dockerfile,用docker build根据Dockerfile创建镜像,最后我们可以用这个镜像来生成可ssh登录的容器了。

    一、首先创建一个容器并登入

1
2
3
4
5
6
[root@localhost ~] # docker images centos
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              centos7             ae0c2d0bdc10        4 weeks ago         224 MB
centos              latest              ae0c2d0bdc10        4 weeks ago         224 MB
[root@localhost ~] # docker run -i -t centos:centos7 /bin/bash
[root@5255b18871ae /] #

    二、在容器里安装ssh服务端

    由于centos:centos7镜像里没有安装passwd、openssl和openssh-server,我们用yum安装一下:

1
[root@5255b18871ae /] # yum install passwd openssl openssh-server -y

    设置root密码为123456:

1
2
3
[root@5255b18871ae /] # echo '123456' | passwd --stdin root
Changing password  for  user root.
passwd : all authentication tokens updated successfully.

    我们如果现在启动sshd,sshd会报错:

1
2
Could not load host key:  /etc/ssh/ssh_host_rsa_key
Could not load host key:  /etc/ssh/ssh_host_ecdsa_key

    所以我们先生成/etc/ssh/ssh_host_rsa_key和/etc/ssh/ssh_host_ecdsa_key:

1
2
[root@5255b18871ae /] # ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@5255b18871ae /] # ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''

    然后查一下容器的IP,以daemon方式启动sshd:

1
2
3
4
5
6
7
8
[root@5255b18871ae /] # ip addr ls eth0
270: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP 
     link /ether  02:42:ac:11:00:81 brd ff:ff:ff:ff:ff:ff
     inet 172.17.0.129 /16  scope global eth0
        valid_lft forever preferred_lft forever
     inet6 fe80::42:acff:fe11:81 /64  scope link 
        valid_lft forever preferred_lft forever
[root@5255b18871ae /] # /usr/sbin/sshd -D

    我们看到容器IP为172.17.0.129,从外部远程ssh到这个容器:

1
2
3
4
5
6
7
[root@localhost ~] # ssh root@172.17.0.129
The authenticity of host  '172.17.0.129 (172.17.0.129)'  can't be established.
RSA key fingerprint is 81:ab:5d:18:88:73:d2:5b:cf:1b:1a:10:1c:e7:b4:1e.
Are you sure you want to  continue  connecting ( yes /no )?  yes
Warning: Permanently added  '172.17.0.129'  (RSA) to the list of known hosts.
root@172.17.0.129's password: 
Connection to 172.17.0.129 closed.

    发现容器立即关闭了连接,原因是容器的ssh使用了pam_loginuid.so模块,我们把它关掉:

1
[root@5255b18871ae /] # sed -i '/^session\s\+required\s\+pam_loginuid.so/s/^/#/' /etc/pam.d/sshd

    上述命令的意思是:在/etc/pam.d/sshd文件里注释掉"session    required     pam_loginuid.so"这一行。

    然后重新启动sshd:

1
[root@5255b18871ae /] # /usr/sbin/sshd -D

    再次尝试远程ssh登入:

1
2
3
4
[root@localhost ~] # ssh root@172.17.0.129
root@172.17.0.129's password: 
Last login: Tue Dec  2 03:00:07 2014 from 172.17.42.1
[root@5255b18871ae ~] #

    登入成功!

    三、编写Dockerfile

    根据上面的操作步骤,在docker服务器端创建Dockerfile文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 设置基本的镜像,后续命令都以这个镜像为基础
FROM centos:centos7
# 作者信息
MAINTAINER Qicheng, http: //qicheng0211 .blog.51cto.com
# RUN命令会在上面指定的镜像里执行任何命令
RUN yum  install  passwd  openssl openssh-server -y
RUN  echo  '123456'  |  passwd  --stdin root
RUN  ssh -keygen -q -t rsa -b 2048 -f  /etc/ssh/ssh_host_rsa_key  -N  ''
RUN  ssh -keygen -q -t ecdsa -f  /etc/ssh/ssh_host_ecdsa_key  -N  ''
RUN  sed  -i  '/^session\s\+required\s\+pam_loginuid.so/s/^/#/'  /etc/pam .d /sshd
RUN  mkdir  -p  /root/ . ssh  &&  chown  root.root  /root  &&  chmod  700  /root/ . ssh
# 暴露ssh端口22
EXPOSE 22
# 设定运行镜像时的默认命令:输出ip,并以daemon方式启动sshd
CMD ip addr  ls  eth0 |  awk  '{print $2}'  |  egrep  -o  '([0-9]+\.){3}[0-9]+' ; /usr/sbin/sshd  -D

    四、根据Dockerfile来创建镜像

    用docker build根据Dockerfile创建镜像(centos:autosshd):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
[root@localhost ~] # docker build -t centos:autosshd - < Dockerfile
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon 
Step 0 : FROM centos:centos7
  ---> ae0c2d0bdc10
Step 1 : MAINTAINER Qicheng, http: //qicheng0211 .blog.51cto.com/
  ---> Running  in  26c2fddd9156
  ---> 1807df1e23db
Removing intermediate container 26c2fddd9156
Step 2 : RUN yum  install  passwd  openssl openssh-server -y
  ---> Running  in  e10f052d4263
Loaded plugins: fastestmirror
Determining fastest mirrors
  * base: mirrors.aliyun.com
  * extras: mirrors.aliyun.com
  * updates: mirrors.aliyun.com
......
Installed:
   openssh-server.x86_64 0:6.4p1-8.el7     openssl.x86_64 1:1.0.1e-34.el7_0.6    
   passwd .x86_64 0:0.79-4.el7             
 
Dependency Installed:
   fipscheck.x86_64 0:1.4.1-5.el7           fipscheck-lib.x86_64 0:1.4.1-5.el7   
   make .x86_64 1:3.82-21.el7                openssh.x86_64 0:6.4p1-8.el7         
   tcp_wrappers-libs.x86_64 0:7.6-77.el7   
 
Complete!
  ---> 20e63694ff20
Removing intermediate container e10f052d4263
Step 3 : RUN  echo  '123456'  |  passwd  --stdin root
  ---> Running  in  d0f1b578cc27
Changing password  for  user root.
passwd : all authentication tokens updated successfully.
  ---> f1b73ad76b66
Removing intermediate container d0f1b578cc27
Step 4 : RUN  ssh -keygen -q -t rsa -b 2048 -f  /etc/ssh/ssh_host_rsa_key  -N  ''
  ---> Running  in  f31b1aa24883
  ---> 647bb8cb3fc9
Removing intermediate container f31b1aa24883
Step 5 : RUN  ssh -keygen -q -t ecdsa -f  /etc/ssh/ssh_host_ecdsa_key  -N  ''
  ---> Running  in  401c7e2cf34d
  ---> f79b9c8bf108
Removing intermediate container 401c7e2cf34d
Step 6 : RUN  sed  -i  '/^session\s\+required\s\+pam_loginuid.so/s/^/#/'  /etc/pam .d /sshd
  ---> Running  in  00c28bea761b
  ---> 50f7f29c64a9
Removing intermediate container 00c28bea761b
Step 7 : RUN  mkdir  -p  /root/ . ssh  &&  chown  root.root  /root  &&  chmod  700  /root/ . ssh
  ---> Running  in  a3a94d599b6b
  ---> f91df92e2194
Removing intermediate container a3a94d599b6b
Step 8 : EXPOSE 22
  ---> Running  in  28ee83c39a27
  ---> 7a82bca0db6a
Removing intermediate container 28ee83c39a27
Step 9 : CMD ip addr  ls  eth0 |  awk  '{print $2}'  |  egrep  -o  '([0-9]+\.){3}[0-9]+' ; /usr/sbin/sshd  -D
  ---> Running  in  41d58259b402
  ---> bd345297137b
Removing intermediate container 41d58259b402
Successfully built bd345297137b
[root@localhost ~] # docker images centos
REPOSITORY          TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
centos              autosshd            bd345297137b        3 minutes ago       300 MB
centos              centos7             ae0c2d0bdc10        4 weeks ago         224 MB
centos              latest              ae0c2d0bdc10        4 weeks ago         224 MB

    我们看到centos:autosshd镜像已经成功创建了。

    用这个镜像创建的容器都是可ssh登入的,我们验证一下:

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~] # docker run -d --name=mytest1 centos:autosshd
614c6573b88451b073ee6aa10b8081337f3f2af8e77bf999bd0537173cf8c1fc
[root@localhost ~] # docker logs mytest1
172.17.0.136
[root@localhost ~] # ssh root@172.17.0.136
The authenticity of host  '172.17.0.136 (172.17.0.136)'  can't be established.
RSA key fingerprint is 35:b2:77:e9:32:ba:74:58:84:66:89:be:1b:78:ec:75.
Are you sure you want to  continue  connecting ( yes /no )?  yes
Warning: Permanently added  '172.17.0.136'  (RSA) to the list of known hosts.
root@172.17.0.136's password: 
[root@614c6573b884 ~] #

    ssh登入成功!

    



本文转自 张斌_青岛 51CTO博客,原文链接:http://blog.51cto.com/qicheng0211/1585398

相关文章
|
应用服务中间件 网络安全 数据安全/隐私保护
网关服务器配置指南:实现自动DHCP地址分配、HTTP服务和SSH无密码登录。
哇哈哈,道具都准备好了,咱们的魔术秀就要开始了。现在,你的网关服务器已经魔法满满,自动分配IP,提供网页服务,SSH登录如入无人之境。而整个世界,只会知道效果,不会知道是你在幕后操控一切。这就是真正的数字世界魔法师,随手拈来,手到擒来。
713 14
|
JavaScript 应用服务中间件 Linux
【应用服务 App Service】解决无法从Azure门户SSH登录问题
【应用服务 App Service】解决无法从Azure门户SSH登录问题
511 0
|
Ubuntu 安全 网络安全
Docker镜像:Ubuntu支持systemctl、SSH和VNC
总的来说,Docker提供了一个灵活且强大的方式来创建和运行自定义的Ubuntu镜像。通过理解和使用Dockerfile,你可以轻松地创建一个支持systemctl、SSH和VNC的Ubuntu镜像。
1075 21
|
Ubuntu Linux 网络安全
Ubuntu Docker镜像:支持systemctl、SSH、VNC
总的来说,创建一个支持systemctl、SSH和VNC的Ubuntu Docker镜像需要一些技术知识,但是通过学习和实践,我们可以掌握这些技术。这将使我们能够更有效地使用Docker,更好地管理我们的应用程序和服务。CentOS系统中的日志文件位置和管理
501 17
|
Linux 网络安全 数据安全/隐私保护
配置ssh免密登录
配置ssh免密登录
1056 106
|
安全 网络安全 数据安全/隐私保护
Debian 12系统中允许Root远程SSH登录解决方法!
在 Debian 12 系统中开启 SSH 远程 Root 登录需修改 SSH 配置文件 (`sshd_config`),将 `PermitRootLogin` 设置为 `yes` 并确保密码认证启用。完成后重启 SSH 服务并验证连接。若防火墙启用,需放行端口 22。注意,直接开放 Root 登录可能带来安全风险,建议使用普通用户登录后切换至 Root。
2772 1
|
监控 Ubuntu 安全
debian或Ubuntu中开启ssh允许root远程ssh登录的方法
在Debian或Ubuntu系统中启用root用户的SSH远程登录需要编辑SSH配置文件、设置root密码并重启SSH服务。虽然这可以在某些情况下提供便利,但必须注意安全性,通过使用强密码、限制IP访问、使用SSH密钥认证等方法来保护服务器的安全。
8764 5
|
安全 Linux 网络安全
|
机器学习/深度学习 存储 Linux
【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
608 4
|
存储 安全 测试技术
【超实用却暗藏杀机】sshpass:一键免密SSH登录的神器,为何生产环境却要敬而远之?探秘背后的安全隐患与替代方案!
【8月更文挑战第16天】sshpass 是一款便捷工具,可实现自动化SSH登录,简化脚本中的远程连接流程。通过后台自动处理密码输入,便于执行远程操作,如 `sshpass -p &#39;yourpassword&#39; ssh user@remotehost`。也可结合更多SSH选项使用,例如指定私钥文件。然而,因需明文传递密码,存在较大安全隐患,不适于生产环境;推荐使用公钥认证以增强安全性。
1984 4