使用docker安装虚拟机并打开ssh连接

一、需求

  最近有个需求,要连接很多个linux系统进行测试软件功能,但是我这里只有几个虚拟机,所以需要使用docker来安装几十个或者上百个虚拟机来进行测试。

二、安装docker

  这里就不演示怎么安装了,网上有很多,也可以看这个https://www.runoob.com/docker/centos-docker-install.html,这个上面有多种机器安装docker的教程。

三、具体步骤

  1、拉取centos镜像

docker pull centos:centos7

  2、查看/启动镜像

#查看镜像
docker images
#启动镜像
docker run -itd --name my-centos centos:centos7

  3、进入镜像

#获取容器的id
docker ps
#进入容器内部
docker exec -it 9bd5d8e8a3e7 /bin/bash

  4、为容器安装软件

#安装ssh
yum install -y openssh-server openssh-clients
#修改密码命令
yum install -y passwd
#service命令
yum install -y initscripts

  5、修改密码

#修改密码命令
passwd

  6、修改sshd_config配置文件

#修改文件
vi /etc/ssh/sshd_config

#找到UsePAM参数,设置为no

  7、重启ssh并退出容器

#重启ssh
service sshd start

#这里会报错
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
#直接进行下面的命令就行,使用最后一步的命令启动就解决这个问题了

#退出容器
exit

 

  8、将刚刚修改的容器保存为新的镜像

docker commit 9bd5d8e8a3e7 my-ssh-centos

  9、启动新的镜像

#注意要暴露对外映射的端口 --privileged=true 和后面的 /sbin/init 必须要有,以特权模式启动容器,否则无法使用systemctl启动服务
docker run -tid --name my-ssh-0 -p 50022:22 --privileged=true 9bd5d8e8a3e7 /sbin/init

 

posted @ 2019-12-18 11:25  卫旗  阅读(2933)  评论(0编辑  收藏  举报