dockerizing a sshd daemon based on Centos7

docker 有一个sshd服务是比较容易用来测试的, 所以有必要做一个sshd镜像.

docker文档里面有一篇是基于ubuntu : 14.04来制作sshd镜像的.

因为本人习惯了使用centos, 本文将基于centos7来制作一个sshd镜像.

先列出ubuntu制作sshd镜像的Dockerfile : 

# sshd
#
# VERSION               0.0.2

FROM ubuntu:14.04
MAINTAINER Sven Dowideit <SvenDowideit@docker.com>

RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
# 设置默认密码
RUN echo 'root:screencast' | chpasswd
RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config

# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd

ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

修改成适合centos7的版本 : 

# mkdir /data01/sshd
# vi Dockerfile

# sshd
#
# VERSION               0.0.2

FROM centos:centos7
MAINTAINER digoal.zhou

RUN yum install -y openssh-server
RUN yum install -y openssh-clients
RUN mkdir /var/run/sshd
RUN echo 'UseDNS no' >> /etc/ssh/sshd_config
sed -i -e '/pam_loginuid.so/d' /etc/pam.d/sshd
# 设置默认密码
RUN echo 'root:Digoal_sshd_1999' | chpasswd
RUN /usr/bin/ssh-keygen -A

# 要在其他主机访问的话, 建议expose出去.
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

制作image : 

[root@localhost sshd]# docker build -t digoal/sshd .
Sending build context to Docker daemon  2.56 kB
Sending build context to Docker daemon
Step 0 : FROM centos:centos7
 ---> ae0c2d0bdc10
Step 1 : MAINTAINER digoal.zhou
 ---> Running in 072ae2460e25
 ---> 3c5e418bb4b1
Removing intermediate container 072ae2460e25
Step 2 : RUN yum install -y openssh-server
 ---> Running in bac9ecaa70cf
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: centos.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:6.4p1-8.el7 will be installed
--> Processing Dependency: openssh = 6.4p1-8.el7 for package: openssh-server-6.4p1-8.el7.x86_64
--> Processing Dependency: fipscheck-lib(x86-64) >= 1.3.0 for package: openssh-server-6.4p1-8.el7.x86_64
--> Processing Dependency: libwrap.so.0()(64bit) for package: openssh-server-6.4p1-8.el7.x86_64
--> Processing Dependency: libfipscheck.so.1()(64bit) for package: openssh-server-6.4p1-8.el7.x86_64
--> Running transaction check
---> Package fipscheck-lib.x86_64 0:1.4.1-5.el7 will be installed
--> Processing Dependency: /usr/bin/fipscheck for package: fipscheck-lib-1.4.1-5.el7.x86_64
---> Package openssh.x86_64 0:6.4p1-8.el7 will be installed
---> Package tcp_wrappers-libs.x86_64 0:7.6-77.el7 will be installed
--> Running transaction check
---> Package fipscheck.x86_64 0:1.4.1-5.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                  Arch          Version               Repository   Size
================================================================================
Installing:
 openssh-server           x86_64        6.4p1-8.el7           base        367 k
Installing for dependencies:
 fipscheck                x86_64        1.4.1-5.el7           base         21 k
 fipscheck-lib            x86_64        1.4.1-5.el7           base         11 k
 openssh                  x86_64        6.4p1-8.el7           base        341 k
 tcp_wrappers-libs        x86_64        7.6-77.el7            base         66 k

Transaction Summary
================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 806 k
Installed size: 1.9 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/base/packages/fipscheck-lib-1.4.1-5.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for fipscheck-lib-1.4.1-5.el7.x86_64.rpm is not installed
--------------------------------------------------------------------------------
Total                                              327 kB/s | 806 kB  00:02
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
 Userid     : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
 Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
 Package    : centos-release-7-0.1406.el7.centos.2.5.x86_64 (@Updates/$releasever)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : fipscheck-lib-1.4.1-5.el7.x86_64                             1/5
  Installing : fipscheck-1.4.1-5.el7.x86_64                                 2/5
  Installing : openssh-6.4p1-8.el7.x86_64                                   3/5
  Installing : tcp_wrappers-libs-7.6-77.el7.x86_64                          4/5
  Installing : openssh-server-6.4p1-8.el7.x86_64                            5/5
  Verifying  : tcp_wrappers-libs-7.6-77.el7.x86_64                          1/5
  Verifying  : fipscheck-1.4.1-5.el7.x86_64                                 2/5
  Verifying  : openssh-server-6.4p1-8.el7.x86_64                            3/5
  Verifying  : openssh-6.4p1-8.el7.x86_64                                   4/5
  Verifying  : fipscheck-lib-1.4.1-5.el7.x86_64                             5/5 

Installed:
  openssh-server.x86_64 0:6.4p1-8.el7                                           

Dependency Installed:
  fipscheck.x86_64 0:1.4.1-5.el7      fipscheck-lib.x86_64 0:1.4.1-5.el7
  openssh.x86_64 0:6.4p1-8.el7        tcp_wrappers-libs.x86_64 0:7.6-77.el7     

Complete!
 ---> c48a513d5431
Removing intermediate container bac9ecaa70cf
Step 3 : RUN mkdir /var/run/sshd
 ---> Running in b0b25471af5d
 ---> e61b7a8bb4d9
Removing intermediate container b0b25471af5d
Step 4 : RUN echo 'UseDNS no' >> /etc/ssh/sshd_config
 ---> Running in 3d7072b4b9f5
 ---> 8ed4d6eb45c1
Removing intermediate container 3d7072b4b9f5
Step 5 : RUN echo 'root:Digoal_sshd_1999' | chpasswd
 ---> Running in 54eebb17b732
 ---> ecb67638a0df
Removing intermediate container 54eebb17b732
Step 6 : RUN /usr/bin/ssh-keygen -A
 ---> Running in cc7fc7b7a49e
ssh-keygen: generating new host keys: RSA1 RSA DSA ECDSA
 ---> 2b73dfeaebf1
Removing intermediate container cc7fc7b7a49e
Step 7 : EXPOSE 22
 ---> Running in fa5ca073d31b
 ---> 6e0fcfed929b
Removing intermediate container fa5ca073d31b
Step 8 : CMD ["/usr/sbin/sshd", "-D"]
 ---> Running in 0d53a37829c1
 ---> 3e5d8edfaeee
Removing intermediate container 0d53a37829c1
Successfully built 3e5d8edfaeee

上传到docker hub, 方便以后使用 : 

[root@localhost sshd]# docker login
Username: digoal
Password:
Email: digoal@126.com
Login Succeeded
[root@localhost sshd]# docker push digoal/sshd
...
Pushing tag for rev [3e5d8edfaeee] on {https://cdn-registry-1.docker.io/v1/repositories/digoal/sshd/tags/latest}

测试 : 

[root@localhost ~]# docker run -d --name digoal digoal/sshd
486381d4428e917b6572eb1a802972eb576b0fa3731178c2cd055a5def9a02ea
[root@localhost ~]# docker inspect -f '{{.NetworkSettings.IPAddress}}' digoal
172.17.0.13

使用Dockerfile设置的初始密码登录container : 

[root@localhost ~]# ssh root@172.17.0.13
The authenticity of host '172.17.0.13 (172.17.0.13)' can't be established.
ECDSA key fingerprint is 76:34:4f:98:d5:56:cd:2c:e4:f8:9c:14:5a:82:f6:bf.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.13' (ECDSA) to the list of known hosts.
root@172.17.0.13's password: 

修改默认密码

[root@486381d4428e ~]# echo 'root:helloworld' | chpasswd
[root@486381d4428e ~]# exit
logout
Connection to 172.17.0.13 closed.

使用新密码登录container : 

[root@localhost ~]# ssh root@172.17.0.13
root@172.17.0.13's password:
Last login: Thu Nov 27 11:52:03 2014 from 172.17.42.1
[root@486381d4428e ~]# 

有了这个镜像, 想做测试就更方便了, 感觉就好像起了一个虚拟机.

最后有一个建议, 参考 : 

http://blog.163.com/digoal@126/blog/static/163877040201411165323773/

即建议修改 /etc/pam.d/sshd, 注释如下 : 

# session    required     pam_loginuid.so

修改后, 重新提交镜像.

[参考]
1. http://docs.docker.com/examples/running_ssh_service/

时间: 2024-08-04 11:20:24

dockerizing a sshd daemon based on Centos7的相关文章

A clojure DSL for system admin and deployment with many remote machines

update: Allow passing command line arguments to task now. 1.What is clojure-control?     The idea came from node-control.     Define clusters and tasks for system administration or code deployment, then execute them on one or many remote machines.   

Greenplum 大集群应该调整的sshd_config配置

背景 Greenplum是MPP数据库,所以大的集群可能涉及很多的主机以及很多的segments. Greenplum的很多管理脚本都会涉及ssh的连接,通过SSH进行远程的管理或命令的调用. 因此如果有并发的管理任务,会建立很多的SSH会话. 但是默认情况下Linux的sshd_config配置是比较保守的,没有想到应用会发起那么多的SSH会话. 如果你遇到这样的报错就要关注一下sshd的配置了 ssh_exchange_identification: Connection closed by

BSDRP v0.35发布 一个可定制的FreeBSD发行版

SD Router Project (BSDRP) 是一个可定制的 FreeBSD 发行版,主要提供 IP 路由服务,适合小型的 ISP .新版本主要基于刚刚发布不久的 FreeBSD 8.2 版本,增加多链路的 PPP 守护进程,拥有 PPPoE.L2TP.PPTP 等特性. 更新日志: - Update to FreeBSD 8.2-RELEASE- BSDRP's nanobsd patches were include to FreeBSD-current, then replace B

CentOS7常用环境设置

一.云服务器ECS 地域:华东2 系统镜像:CentOS 7.3 64位 设置安全组,开放端口:80.3306.27017.21.22.2222.3717.8888.5672.15672.25672 二.基础安全设置 1.禁ping echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all   2.修改ssh登录端口 修改/etc/ssh/sshd_config 找到#Port 22这一行,去掉注释,把端口号改为2222 重启ssh服务systemctl

CentOS7编译安装新版LNMP环境_Linux

由于公司要求需要最新版的ZABBIX2.4.4需要最新版的系统CENTOS7和新版的LNMP环境,所以本人摸索着使用新版的环境搭建了LNMP系统,环境版本如下: 系统:CentOS 7 x86_64 NGINX:nginx-1.7.12 数据库:mariadb-10.0.13 PHP:php-5.5.23 首先做一些准备工作,先把centos7的防火墙更换成iptables,可以参见如下链接 centos7防火墙设置iptables 修改成iptables之后就可以清空iptables里面的过滤

如何使用Denyhosts防止黑客SSHD扫描

环境:Ubuntu Server 12.04 denyhost是一个由python写的脚本,目前最新版本为2.6,可以实现对自动查找恶意ssh连接,然后把恶意ip加入到/etc/hosts.deny文件里面,以实现对服务器的安全防护 安装denyhost # apt-get install mailutils            //安装邮件功能,以实现当有黑客攻击的时候报警到管理员邮箱 # apt-get install denyhosts        //安装denyhost软件,以实现

sshd系统自带启动脚本详解

SSH 为 Secure Shell 的缩写.sshd服务是linux系统中最经常使用的服务之一.由于其规避了明文传送口令.内容本文及中间人攻击的安全隐患,因此经常作为远程管理系统的首选方案.虽然各个linux发行版本或多或少都会有所差异,但sshd服务一定会作为标准配置出现. 本文通过分析/etc/init.d/sshd脚本来理解linux系统是如何处理sshd服务的启动.关闭等操作的.帮助我们在理解sshd服务的同时,也能够在遇到问题时快速排查.定位.不详之处,还望见谅,希望大家能够多多提出

CentOS7 安装docker 解决启动不了的问题_docker

 CentOS7安装docker 启动不了,在工作当中遇到这个问题,当时急的不清,网上资料又不多,不过经过自己的捣鼓终于解决了 这个问题! [root@test ~]# yum update [root@test ~]# yum install docker [root@test ~]# service docker start Redirecting to /bin/systemctl start docker.service Job for docker.service failed bec

CentOS7+MySQL/MariaDB+Galera+HAProxy+Keepalived构建高可用数据库集群

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dgd2010.blog.51cto.com/1539422/1603972 方案优势: Galera能够实现MySQL/MariaDB数据库的主主复制和多主复制等模式,这些复制模式都是同步进行的,同步时间非常短 每一个节点都可以同时写入和读取,当某一节点发生故障时,可自动从集群中自动剔除 HAProxy能提供负载均衡和故障判断等功能解决服务器系统存在的单点故障 Keepaliv