centos7中使用ssh登陆docker容器

一、Docer 在CentOS 7中安装

  1. rpm -Uvh http://dl.Fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

  2. yum -y install docker.io    #安装Docker

  3. service docker start        #启动Docker

  4. chkconfig docker on         #加载到开机启动

  5. docker images               #可以使用的镜像

  6. docker pull centos          #下载centos 镜像

  7. docker stats `docker ps | awk '{print $1}' | grep -v CONTAINER` #查看所有运行容器

二、运行Docker容器并使用SSH管理

  1. docker run -i -t centos /bin/bash  #启动一个Docker容器,并进入到bash中

  2. yum -y install openssh-server      #安装sshd

  3. ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

  4. ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key 

  5. ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ""

  6. /usr/sbin/sshd -D               #后台启动sshd(whereis、which 查找执行命令)

  7.  vi /etc/ssh/sshd_config  #将UsePAM yes改成no,否则ssh登陆到容器时会马上退出

  8. yum -y install passwd              #安装passwd 命令

  9. passwd root                        #修改root用户密码,用于ssh登录名

  10.  docker commit containerid imagename   #containerid:容器的id,imagename:提交时候镜像的名称

  11. docker run -d -p 10022:22 imagename /usr/sbin/sshd -D #参数-d表示后台运行,-p表示docker到主机的端口的映射

  12. ssh root@localhost -p 10022

  13. 使用SecureCRT 用10222端口登陆容器,就可以管理了。




    







你可能感兴趣的:(SSHD,docker)