docker上安装centos8并实现ssh连接(超详细,超简单)

1.首先我们需要从docker官网上获取centos镜像(也可以自行导入别人的镜像)

docker search centos

2.拉取对应版本的镜像,我这边拉取的是centos8

docker pull centos:centos8

然后通过docker images查看所有镜像,这边显示我们已经拉取下来了

 

3.启动linux

docker run -itd <IMAGE ID > /bin/bash    /bin/bash 我这里为 docker run -itd 300e315adb2f /bin/bash

然后用docker ps命令查看正在运行的容器

4.进入容器

[root@www ~]# docker attach 37228d4329ae
[root@37228d4329ae /]# 

5.官方拉取的centos本身是没有ifconfig命令的,需要配置

[root@37228d4329ae /]# ifconfig
bash: ifconfig: command not found

[root@37228d4329ae /]# yum search ifconfig


Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                                                            603 kB/s | 6.3 MB     00:10    
CentOS Linux 8 - BaseOS                                                               614 kB/s | 2.3 MB     00:03    
CentOS Linux 8 - Extras                                                               6.6 kB/s | 8.6 kB     00:01    
No matches found.

[root@37228d4329ae /]# yum search ifconfig
Failed to set locale, defaulting to C.UTF-8
CentOS Linux 8 - AppStream                                                            603 kB/s | 6.3 MB     00:10    
CentOS Linux 8 - BaseOS                                                               614 kB/s | 2.3 MB     00:03    
CentOS Linux 8 - Extras                                                               6.6 kB/s | 8.6 kB     00:01    
No matches found.
[root@37228d4329ae /]# ifcofig
bash: ifcofig: command not found
[root@37228d4329ae /]# ifconfig
bash: ifconfig: command not found


[root@37228d4329ae /]# yum install net-tools.x86_64


Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:00:42 ago on Wed Jan  6 08:48:44 2021.
Dependencies resolved.
======================================================================================================================
 Package                  Architecture          Version                                   Repository             Size
======================================================================================================================
Installing:
 net-tools                x86_64                2.0-0.52.20160912git.el8                  baseos                322 k

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

Total download size: 322 k
Installed size: 942 k
Is this ok [y/N]: y
Downloading Packages:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm                                         242 kB/s | 322 kB     00:01    
----------------------------------------------------------------------------------------------------------------------
Total                                                                                 169 kB/s | 322 kB     00:01     
warning: /var/cache/dnf/baseos-f6a80ba95cf937f2/packages/net-tools-2.0-0.52.20160912git.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY
CentOS Linux 8 - BaseOS                                                               1.6 MB/s | 1.6 kB     00:00    
Importing GPG key 0x8483C65D:
 Userid     : "CentOS (CentOS Official Signing Key) <security@centos.org>"
 Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
Is this ok [y/N]: y
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                              1/1 
  Installing       : net-tools-2.0-0.52.20160912git.el8.x86_64                                                    1/1 
  Running scriptlet: net-tools-2.0-0.52.20160912git.el8.x86_64                                                    1/1 
  Verifying        : net-tools-2.0-0.52.20160912git.el8.x86_64                                                    1/1 

Installed:
  net-tools-2.0-0.52.20160912git.el8.x86_64                                                                           

Complete!

6.设置centos密码

[root@37228d4329ae /]# passwd root
bash: passwd: command not found
这里需要注意出现这样的错误,是需要补充命令的

yum search passwd  //查找源文件

passwd.x86_64 : An utility for setting or changing passwords using PAM  //查找到的源  不一定都一样 根据自己系统的

yum install passwd.x86_64  //安装

然后设置自己的密码,这个密码需要记住,后面ssh连接的时候需要

[root@37228d4329ae /]# passwd root
Changing password for user root.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

7.配置ssh

yum install openssh-server -y

生成公钥,私钥:

ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_keyGenerating
ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_keyGenerating
ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_keyGenerating

执行: /usr/sbin/sshd -D

[root@37228d4329ae /]# /usr/sbin/sshd -D

或/usr/sbin/sshd
8.退出容器,保存为镜像

从容器退出,但不停止容器: Ctrl+P+Q

$ docker ps

[root@www ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                               NAMES
37228d4329ae   300e315adb2f   "/bin/bash"              42 minutes ago   Up 42 minutes                                       intelligent_allen
5a5909cd8ca4   mysql:latest   "docker-entrypoint.s…"   6 days ago       Up 6 days       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql_test

回到Docker下面,停止容器: docker stop <容器ID>

$ docker stop 37228d4329ae

#提交当前容器到镜像: docker commit <容器ID> <NAME:VERSION>   docker commit 37228d4329ae centos:centos_ssh_v1.0

重启容器

[root@www ~]# docker run --name centos_ssh -itd -p 50001:22 71b4c6967869 /bin/bash
7d60f77d59150901890020b7c162bb90ced7074a28e912ce18932903825c3f6e
[root@www ~]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                               NAMES
7d60f77d5915   71b4c6967869   "/bin/bash"              7 seconds ago   Up 4 seconds   0.0.0.0:50001->22/tcp               centos_ssh
5a5909cd8ca4   mysql:latest   "docker-entrypoint.s…"   6 days ago      Up 6 days      0.0.0.0:3306->3306/tcp, 33060/tcp   mysql_test

 

即可通过ssh工具和端口50001连接
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值