创建一个提供 ssh 连接的 centos7.6.1810 容器

先登录Docker

PS C:\Users\LiXinlei\Desktop> docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (lixinlei): lixinlei
Password:
Login Succeeded

拉取最新的CentOS镜像

PS C:\Users\LiXinlei\Desktop> docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8ba884070f61: Downloading
latest: Pulling from library/centos
8ba884070f61: Pull complete
Digest: sha256:8d487d68857f5bc9595793279b33d082b03713341ddec91054382641d14db861
Status: Downloaded newer image for centos:latest

查看系统中的镜像

  • 有新下载的镜像:centos latest 9f38484d220f 3 weeks ago 202MB
  • 镜像id为:9f38484d220f
PS C:\Users\LiXinlei\Desktop> docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
jvm001                     latest              cebfdc78e25b        3 weeks ago         298MB
centos                     latest              9f38484d220f        3 weeks ago         202MB
hello-world                latest              fce289e99eb9        3 months ago        1.84kB
centos                     6.8                 e54faac158ff        6 months ago        195MB
docker4w/nsenter-dockerd   latest              cae870735e91        17 months ago       187kB

运行新下载的镜像

PS C:\Users\LiXinlei\Desktop> docker run -itd 9f38484d220f /bin/bash
8d5ad5ca35f9228a8d9c8dd227e455052b2b56c1677105ca8723da1cae87d01e

查看运行中的Docker容器

  • CONTAINER ID :8d5ad5ca35f9
PS C:\Users\LiXinlei\Desktop> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
8d5ad5ca35f9        9f38484d220f        "/bin/bash"         7 seconds ago       Up 6 seconds                            nostalgic_archimedes

进入刚运行起来的容器

PS C:\Users\LiXinlei\Desktop> docker attach 8d5ad5ca35f9
[root@8d5ad5ca35f9 /]#

安装ifconfig

[root@8d5ad5ca35f9 /]# yum search ifconfig
Loaded plugins: fastestmirror, ovl
Determining fastest mirrors
 * base: mirrors.nwsuaf.edu.cn
 * extras: mirrors.nwsuaf.edu.cn
 * updates: mirrors.163.com
base                                                                                                                                                                  | 3.6 kB  00:00:00
extras                                                                                                                                                                | 3.4 kB  00:00:00
updates                                                                                                                                                               | 3.4 kB  00:00:00
(1/4): base/7/x86_64/group_gz                                                                                                                                         | 166 kB  00:00:04
(2/4): extras/7/x86_64/primary_db                                                                                                                                     | 187 kB  00:00:04
(3/4): base/7/x86_64/primary_db                                                                                                                                       | 6.0 MB  00:00:04
(4/4): updates/7/x86_64/primary_db                                                                                                                                    | 3.4 MB  00:00:05
===================================================================================== Matched: ifconfig =====================================================================================
net-tools.x86_64 : Basic networking tools
[root@8d5ad5ca35f9 /]# yum install net-tools.x86_64
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.nwsuaf.edu.cn
 * extras: mirrors.nwsuaf.edu.cn
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.24.20131004git.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================
 Package                                    Arch                                    Version                                                      Repository                             Size
=============================================================================================================================================================================================
Installing:
 net-tools                                  x86_64                                  2.0-0.24.20131004git.el7                                     base                                  306 k

Transaction Summary
=============================================================================================================================================================================================
Install  1 Package

Total download size: 306 k
Installed size: 918 k
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/net-tools-2.0-0.24.20131004git.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY       ]  0.0 B/s | 184 kB  --:--:-- ETA
Public key for net-tools-2.0-0.24.20131004git.el7.x86_64.rpm is not installed
net-tools-2.0-0.24.20131004git.el7.x86_64.rpm                                                                                                                         | 306 kB  00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <[email protected]>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-6.1810.2.el7.centos.x86_64 (@CentOS)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : net-tools-2.0-0.24.20131004git.el7.x86_64                                                                                                                                 1/1
  Verifying  : net-tools-2.0-0.24.20131004git.el7.x86_64                                                                                                                                 1/1

Installed:
  net-tools.x86_64 0:2.0-0.24.20131004git.el7

Complete!

安装ssh

[root@8d5ad5ca35f9 /]# yum install -y openssh-server
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.nwsuaf.edu.cn
 * extras: mirrors.nwsuaf.edu.cn
 * updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:7.4p1-16.el7 will be installed
--> Processing Dependency: openssh = 7.4p1-16.el7 for package: openssh-server-7.4p1-16.el7.x86_64
--> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-7.4p1-16.el7.x86_64
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-7.4p1-16.el7.x86_64
--> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-7.4p1-16.el7.x86_64
--> Running transaction check
---> Package fipscheck-lib.x86_64 0:1.4.1-6.el7 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-6.el7.x86_64
---> Package openssh.x86_64 0:7.4p1-16.el7 will be installed
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package fipscheck.x86_64 0:1.4.1-6.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================================================================================================
 Package                                             Arch                                     Version                                           Repository                              Size
=============================================================================================================================================================================================
Installing:
 openssh-server                                      x86_64                                   7.4p1-16.el7                                      base                                   458 k
Installing for dependencies:
 fipscheck                                           x86_64                                   1.4.1-6.el7                                       base                                    21 k
 fipscheck-lib                                       x86_64                                   1.4.1-6.el7                                       base                                    11 k
 openssh                                             x86_64                                   7.4p1-16.el7                                      base                                   510 k
 tcp_wrappers-libs                                   x86_64                                   7.6-77.el7                                        base                                    66 k

Transaction Summary
=============================================================================================================================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 1.0 M
Installed size: 3.0 M
Downloading packages:
(1/5): openssh-server-7.4p1-16.el7.x86_64.rpm                                                                                                                         | 458 kB  00:00:01
fipscheck-1.4.1-6.el7.x86_64.r FAILED                                           54% [======================================                                ] 236 kB/s | 580 kB  00:00:02 ETA
http://mirrors.cqu.edu.cn/CentOS/7.6.1810/os/x86_64/Packages/fipscheck-1.4.1-6.el7.x86_64.rpm: [Errno 14] curl#52 - "Empty reply from server"              ] 236 kB/s | 580 kB  00:00:02 ETA
Trying other mirror.
(2/5): fipscheck-1.4.1-6.el7.x86_64.rpm                                                                                                                               |  21 kB  00:00:00
(3/5): fipscheck-lib-1.4.1-6.el7.x86_64.rpm                                                                                                                           |  11 kB  00:00:04
(4/5): tcp_wrappers-libs-7.6-77.el7.x86_64.rpm                                                                                                                        |  66 kB  00:00:04
(5/5): openssh-7.4p1-16.el7.x86_64.rpm                                                                                                                                | 510 kB  00:00:12
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                         86 kB/s | 1.0 MB  00:00:12
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : fipscheck-1.4.1-6.el7.x86_64                                                                                                                                              1/5
  Installing : fipscheck-lib-1.4.1-6.el7.x86_64                                                                                                                                          2/5
  Installing : openssh-7.4p1-16.el7.x86_64                                                                                                                                               3/5
  Installing : tcp_wrappers-libs-7.6-77.el7.x86_64                                                                                                                                       4/5
  Installing : openssh-server-7.4p1-16.el7.x86_64                                                                                                                                        5/5
  Verifying  : fipscheck-lib-1.4.1-6.el7.x86_64                                                                                                                                          1/5
  Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                                                                                                                                       2/5
  Verifying  : fipscheck-1.4.1-6.el7.x86_64                                                                                                                                              3/5
  Verifying  : openssh-7.4p1-16.el7.x86_64                                                                                                                                               4/5
  Verifying  : openssh-server-7.4p1-16.el7.x86_64                                                                                                                                        5/5

Installed:
  openssh-server.x86_64 0:7.4p1-16.el7

Dependency Installed:
  fipscheck.x86_64 0:1.4.1-6.el7              fipscheck-lib.x86_64 0:1.4.1-6.el7              openssh.x86_64 0:7.4p1-16.el7              tcp_wrappers-libs.x86_64 0:7.6-77.el7

Complete!

生成几个秘钥

[root@8d5ad5ca35f9 /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
SHA256:cuH6k5OFEBv+fdM3EKwBJpY33mn1Tr+RcYoyOXHeTNw root@8d5ad5ca35f9
The key's randomart image is:
+---[RSA 2048]----+
|       o.o. .    |
|      +.oo . +   |
|     . ++ o =.o. |
|      +. o.=..o+E|
|      .oSo.= *+.=|
|       +o B = ==o|
|      .  + = . .+|
|       .=      . |
|        .o       |
+----[SHA256]-----+
[root@8d5ad5ca35f9 /]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
Generating public/private ecdsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key.
Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub.
The key fingerprint is:
SHA256:UnPHgWzwHMKGg3j/6eZIU7zwXV9gObbadYJE2Q7WucE root@8d5ad5ca35f9
The key's randomart image is:
+---[ECDSA 256]---+
|   . . ooo..o= . |
|  . o o o++++.E. |
|   . . oo.+.+o*o |
|      .o o o +o+ |
|      o.S.  o o +|
|       =oo . + +.|
|      o.o . . o  |
|     . oo        |
|      .o.        |
+----[SHA256]-----+
[root@8d5ad5ca35f9 /]# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_ed25519_key.
Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub.
The key fingerprint is:
SHA256:VbBEemIKTDScB6ld4MzpEYafkNmfXmlH71lciQFofp0 root@8d5ad5ca35f9
The key's randomart image is:
+--[ED25519 256]--+
|   BO=   .=oo.o .|
|  =B=+o  =.o . ..|
|   =O= .*o+..... |
|  ..+ooo+=...Eo  |
|    ...oS... o   |
|      .     o    |
|                 |
|                 |
|                 |
+----[SHA256]-----+

启动ssh

[root@8d5ad5ca35f9 /]# /usr/sbin/sshd

查看ssh是否启动成功

  • 22 端口被监听了;
[root@8d5ad5ca35f9 /]# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      104/sshd
tcp6       0      0 :::22

设置容器的root密码

[root@8d5ad5ca35f9 /]# echo "root:111111"|chpasswd

退出容器

[root@8d5ad5ca35f9 /]# exit
exit

把容器8d5ad5ca35f9提交成一个新的镜像centos7.6.1810

PS C:\Users\LiXinlei\Desktop> docker commit 8d5ad5ca35f9 centos7.6.1810
sha256:bbcca69cc31f08adca70a8189eb39803eb75d895a0d33a6a12c30ba637443ff2

查看系统中的镜像

  • centos7.6.1810 有了;
PS C:\Users\LiXinlei\Desktop> docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
centos7.6.1810             latest              bbcca69cc31f        7 seconds ago       288MB
jvm001                     latest              cebfdc78e25b        3 weeks ago         298MB
centos                     latest              9f38484d220f        3 weeks ago         202MB
hello-world                latest              fce289e99eb9        3 months ago        1.84kB
centos                     6.8                 e54faac158ff        6 months ago        195MB
docker4w/nsenter-dockerd   latest              cae870735e91        17 months ago       187kB

把镜像centos7.6.1810 run起来

PS C:\Users\LiXinlei\Desktop> docker run -itd --name centos7.6.1810 -h centos7.6.1810 -p 9010:22 centos7.6.1810 /usr/sbin/sshd -D

查看正在运行的容器

  • 容器 centos7.6.1810 有了;
PS C:\Users\LiXinlei\Desktop> docker ps
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                  NAMES
1ee39d3f084e        centos7.6.1810      "/usr/sbin/sshd -D"   6 minutes ago       Up 6 minutes        0.0.0.0:9010->22/tcp   centos7.6.1810

注:这个容器就可以从宿主机ssh连上了,作为安装 rabbitmq 的 Linux 环境;

猜你喜欢

转载自blog.csdn.net/weixin_33669968/article/details/89168315
今日推荐