Linux学习--Docker实现CentOS7添加SSH
Posted 丢爸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习--Docker实现CentOS7添加SSH相关的知识,希望对你有一定的参考价值。
查看本地镜像仓库
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 6.10 f1af727749c4 4 weeks ago 194MB
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
centos latest 5d0da3dc9764 4 weeks ago 231MB
nginx 1.14 295c7be07902 2 years ago 109MB
ubuntu 15.10 9b9cb95443b5 5 years ago 137MB
使用CentOS7镜像启动容器
#通过CentOS7镜像启动容器
[root@docker ~]# docker container run -it --name="centos7_sshd" centos:7
#查看CentOS版本
[root@cf6418a83218 /]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
#配置本地yum源
[root@cf6418a83218 /]# cd /etc/yum.repos.d/
CentOS-CR.repo CentOS-Media.repo CentOS-Vault.repo CentOS-x86_64-kernel.repo
[root@cf6418a83218 yum.repos.d]# mkdir bak
[root@cf6418a83218 yum.repos.d]# mv *.repo bak/
#注:需要在宿主机(192.168.88.101)上安装vsftp软件,并启动服务,将CentOS7的本地iso文件挂载到/var/ftp/centos7目录下
[root@cf6418a83218 yum.repos.d]# vi local.repo
[local]
name = linuxbase
baseurl = ftp://192.168.88.101/centos7
gpgcheck = 0
enabled = 1
[root@cf6418a83218 yum.repos.d]# yum makecache
#安装ssh服务端程序
[root@cf6418a83218 yum.repos.d]# yum install -y openssh-server
#ssh服务初使化操作
[root@cf6418a83218 yum.repos.d]# mkdir /var/run/sshd
[root@cf6418a83218 yum.repos.d]# echo 'UseDNS no' >> /etc/ssh/sshd_config
[root@cf6418a83218 yum.repos.d]# sed -i -e '/pam_loginuid.so/d' /etc/pam.d/sshd
[root@cf6418a83218 yum.repos.d]# echo 'root:123456' | chpasswd
[root@cf6418a83218 yum.repos.d]# /usr/bin/ssh-keygen -A
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA ED25519
制作第一版镜像
[root@docker ~]# docker commit cf6418a83218 tye/centos7_sshd:v1
sha256:356d2ac088cefd18805914e69e07821fa6b98ae423b30f9ca776fadbc54d26c4
[root@docker ~]# docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
tye/centos7_sshd v1 356d2ac088ce 17 seconds ago 285MB
centos 6.10 f1af727749c4 4 weeks ago 194MB
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
centos latest 5d0da3dc9764 4 weeks ago 231MB
nginx 1.14 295c7be07902 2 years ago 109MB
ubuntu 15.10 9b9cb95443b5 5 years ago 137MB
通过新创建的镜像启动一个容器实例
[root@docker ~]# docker container run -d --name="centos7_sshd_new" -p 2222:22 tye/centos7_sshd:v1 /usr/sbin/sshd -D
3c9429c6574c66054a48e006112b03a650d7b2dbdadebac65dee38981356af50
[root@docker ~]# docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3c9429c6574c tye/centos7_sshd:v1 "/usr/sbin/sshd -D" 4 seconds ago Up 3 seconds 0.0.0.0:2222->22/tcp, :::2222->22/tcp centos7_sshd_new
通过ssh连接测试
[C:\\~]$ ssh 192.168.88.101 2222
Connecting to 192.168.88.101:2222...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
WARNING! The remote SSH server rejected X11 forwarding request.
# 连接成功
[root@3c9429c6574c ~]#
以上是关于Linux学习--Docker实现CentOS7添加SSH的主要内容,如果未能解决你的问题,请参考以下文章
docker 入门 - 01- Docker 在 Centos7 上安装与测试