zoukankan      html  css  js  c++  java
  • docker基于commit命令创建支持ssh服务的镜像

    以centos为基础,目的使用ssh服务远程连接docker容器。

    环境:宿主机centos7(宿主机ip地址为192.168.164.130),直接搜索docker的centos镜像,下载最新版本。

    [root@localhost ~]# docker search -s 15 centos

     

    [root@localhost ~]# docker pull docker.io/centos

    使用centos镜像启动一个容器centos_ssh

    [root@localhost ~]# docker run -it --name=centos_ssh centos /bin/bash

    进入容器之后,升级并安装ssh组件,命令如下:

    [root@ fdb751de5762 ~]# yum -y update

    [root@ fdb751de5762 ~]# yum -y install openssh-server

    [root@ fdb751de5762 ~]# vi /etc/ssh/sshd_config

    编辑sshd的配置文件/etc/ssh/sshd_config,将其中的UsePAM yes改为UsePAM no

    在宿主机中生成密钥对,把生成的公钥文件中的内容复制到容器的/root/.ssh/authorized_keys里,如果无对应目录则手动创建。

    [root@localhost ~]# ssh-keygen -t rsa

    [root@localhost ~]# cat /root/.ssh/id_rsa.pub

     

    进入centos_ssh容器

    [root@ fdb751de5762 ~]# mkdir .ssh

    [root@ fdb751de5762 ~]# cd .ssh

    [root@ fdb751de5762 .ssh]# vi authorized_keys

    [root@ fdb751de5762 .ssh]# cat authorized_keys

     

    设置容器root密码

    [root@ fdb751de5762 ~]# echo "root:123456"|chpasswd

    创建 /var/run/sshd/目录,要不然sshd服务启动会报错
    [root@ fdb751de5762 ~]# mkdir /var/run/sshd/

    [root@ fdb751de5762 ~]# /usr/sbin/sshd -D &

    WARNING: 'UsePAM no' is not supported in Red Hat Enterprise Linux and may cause several problems.(1)
    Could not load host key: /etc/ssh/ssh_host_rsa_key(2)

    Could not load host key: /etc/ssh/ssh_host_ecdsa_key

    Could not load host key: /etc/ssh/ssh_host_ed25519_key

    启动时会提示以上信息,(1)表示:修改了sshd_conf文件中的UsePAM yes改成UsePAM no

    (2)表示:没有主机的公私秘钥,重新生成密钥

    [root@fdb751de5762 ~]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key

    [root@fdb751de5762 ~]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key

    [root@fdb751de5762 ~]# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

    [root@fdb751de5762 ~]# /usr/sbin/sshd -D &

     

    [root@fdb751de5762 ~]# ps -ef | grep sshd

    root          1      0  0 22:16 ?        00:00:00 /usr/sbin/sshd -D

    root         33      7  0 22:37 pts/0    00:00:00 grep --color=auto sshd

    [root@fdb751de5762 ~]# yum -y install lsof

    [root@fdb751de5762 ~]# lsof -i:22

     

    [root@ fdb751de5762 ~]# vi run.sh

    #/bin/bash

    /usr/sbin/sshd -D &

    [root@fdb751de5762 ~]# chmod 755 run.sh

    [root@fdb751de5762 ~]# exit

     

    在宿主机中使用commit命令将centos_ssh创建为sshd_centos镜像

    [root@localhost ~]# docker commit fdb751de5762 sshd_centos

     

    运行该镜像并启动sshd服务

    [root@localhost ~]# docker run -p 10022:22 -d sshd_centos /usr/sbin/sshd –D

    [root@localhost ~]# docker ps

     

    说明:10022是宿主机的和容器的通讯端口,外面窗口(局域网)通过访问地址

    ssh root@宿主机IP -p 10022(IP和端口之间有空格,不是:)就能进入容器,如果没有设置ssh免密登录,则输入刚才为容器的root用户设定的密码。

    [root@localhost ~]# ssh root@192.168.134.130 -p 10022

     

    如上所示,成功进入容器。

    如果碰见下述错误

     

    则可以执行下述指令#mv /root/.ssh/known_hosts /tmp

  • 相关阅读:
    ansible-乱
    linux-PXE-12
    linux-ntp-10
    linux-selinxu---性能 -8
    linux-系统启动流程-7
    linux-网络管理-6
    linux-文件系统-5
    linux-包管理器-4
    linux-shell脚本基础-2
    linux-history-ps1-1
  • 原文地址:https://www.cnblogs.com/whutxldwhj/p/6427530.html
Copyright © 2011-2022 走看看