欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

docker中安装centos6

程序员文章站 2022-07-15 14:04:37
...

当使用docker安装最新的centos镜像时,拉取的是centos 7镜像,使用时会出现 Failed to get D-Bus connection: Operation not permitted 的坑,尝试了使用官方介绍的方法来处理,也是挺复杂的,具体可见我写的另一篇博文:

Failed to get D-Bus connection: Operation not permitted , docker安装centos7之坑

最后还是决定在docker中安装centos6镜像,避免这个烦心的问题

使用官方的docker hub拉取centos6镜像时,总是会出现下载失败,试了好多次都是这样

docker pull centos:6

后来通过配置国内的docker镜像源,以下载centos6镜像

1、配置国内docker镜像源

使用中国科学大学的docker镜像缓存,在配置文件 /etc/docker/daemon.json 中加入以下内容(如果没有该文件,则新增):

{
  "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn/"]
}

重新启动dockerd

sudo service docker restart

2、拉取centos6镜像

docker pull centos:6

3、创建centos6容器

docker run --name mycentos -it centos:6 /bin/bash

进入到centos6之后,默认已经是有which、ifconfig、less、ip等常用命令了,而如果是在docker中使用centos7镜像时,是没有以上这些命令的,要重新安装

4、使用yum安装ssh

设置国内的yum镜像源(阿里云的centos镜像源),下载速度会大大提升,使用默认yum镜像也行,速度慢很多

curl http://mirrors.aliyun.com/repo/Centos-6.repo > /etc/yum.repos.d/CentOS-Base-6-aliyun.repo
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
yum makecache
yum install -y openssh-clients openssh-server

注意:刚开始进入到docker中的centos6时,是没有service这个命令的,而当安装 openssh 时,里面会依赖到 initscripts 软件包,这个将自动进行安装,安装后就有 service 命令可以使用了,很方便

启动ssh

[aaa@qq.com /]# chkconfig sshd on
[aaa@qq.com /]# service sshd start
Generating SSH2 RSA host key:                              [  OK  ]
Generating SSH1 RSA host key:                              [  OK  ]
Generating SSH2 DSA host key:                              [  OK  ]
Starting sshd:                                             [  OK  ]

在docker的centos6中,启动ssh时,会自动创建ssh的rsa、dsa**,而如果是在docker中的centos7刚开始启动ssh时,则需要创建相应的**,否则会报相关的**不存在

# docker 中首次启动 centos 7 的 ssh
ssh-****** -t rsa -f /etc/ssh/ssh_host_rsa_key
ssh-****** -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
ssh-****** -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

5、修改ssh配置

启动好ssh后,还要修改一下配置,否则会连接后自动关闭,连接本机或另的机器ssh连接过来都会

[aaa@qq.com /]# ssh localhost
root@localhost's password: 
Connection to localhost closed.

修改ssh的配置文件

vi /etc/ssh/sshd_config

将第97行的UsePAM yes,改为 UsePAM no

保存退出,重启ssh

[aaa@qq.com /]# service sshd restart
Stopping sshd:                                             [  OK  ]
Starting sshd:                                             [  OK  ]

现在就能正常使用ssh连接访问了

[aaa@qq.com /]# ssh localhost
root@localhost's password: 
Last login: Sun Jun  4 15:50:46 2017 from 172.17.42.1

将UsePAM设置为no,主要是禁止PAM验证,usePam为非对称**认证 UsePam,如果是yes的话非对称**验证失败,仍然可用口令登录 

 

欢迎关注本人的微信公众号“大数据与人工智能Lab”(BigdataAILab),获取更多资讯

docker中安装centos6