选择主流的openssh-server作为服务端:

root@161f67ccad50:/# apt-get install openssh-server -y
Reading package lists... Done
Building dependency tree
Reading state information... Done
openssh-server is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 6 not upgraded.
root@161f67ccad50:/#

  如果需要正常启动SSH服务,则目录/var/run/sshd必须存在。手动创建并启动SSH服务:

root@161f67ccad50:/# mkdir -p /var/run/sshd
root@161f67ccad50:/# /usr/sbin/sshd -D &
[1] 3020
root@161f67ccad50:/#

  此时查看容器的22端口:

root@161f67ccad50:/# netstat -lnutp|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3020/sshd
tcp6 0 0 :::22 :::* LISTEN 3020/sshd
root@161f67ccad50:/# 在root用户家目录创建.ssh目录,并复制需要登录的公钥信息到.ssh目录下的authorized_keys中:
root@161f67ccad50:/# mkdir /root/.ssh
root@161f67ccad50:/# cd /root/.ssh
root@161f67ccad50:~/.ssh# ls
root@161f67ccad50:~/.ssh# vi /root/.ssh/authorized_keys

  创建自启动的SSH服务可执行文件run.sh,并添加可执行权限:


root@161f67ccad50:/# cat run.sh
#!/bin/bash
/usr/sbin/sshd -D &
root@161f67ccad50:/# chmod +x run.sh
root@161f67ccad50:/#

  退出容器:


root@161f67ccad50:/# exit
exit
[root@docker ~]#

3.保存镜像


  将退出的容器用docker commit命令保存为一个新的sshd:ubuntu镜像:


[root@docker ~]# docker commit 161f67ccad50 sshd:ubuntu
sha256:f328073a034ae63f93114a92b62141f22a578131ecb663702ac17916bde456a2
[root@docker ~]#

  使用docker images查看本地生成的新镜像sshd:ubuntu:


[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd ubuntu f328073a034a 2 minutes ago 284MB
centos 7 3fa822599e10 3 hours ago 204MB
mariadb latest d29cee62e770 26 hours ago 398MB
nginx latest 9e7424e5dbae 7 days ago 108MB
ubuntu 16.04 20c44cd7596f 12 days ago 123MB
ubuntu latest 20c44cd7596f 12 days ago 123MB
ubuntu 14.04 d6ed29ffda6b 12 days ago 221MB
busybox latest 6ad733544a63 3 weeks ago 1.13MB
centos latest d123f4e55e12 3 weeks ago 197MB
alpine latest 053cde6e8953 3 weeks ago 3.96MB
[root@docker ~]#

4.使用镜像


  启动容器,并添加端口映射到容器的22端口:


[root@docker ~]# docker run -it --name sshd_ubuntu -p 10022:22  sshd:ubuntu
root@0f8481ffd0d0:/# netstat -lnutp|grep 22
root@0f8481ffd0d0:/# /usr/sbin/sshd -D &
[1] 16
root@0f8481ffd0d0:/# netstat -lnutp|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 16/sshd
tcp6 0 0 :::22 :::* LISTEN 16/sshd
root@0f8481ffd0d0:/#

  在宿主机通过ssh连接10022端口:


[root@docker ~]# ssh 10.0.0.31 -p 10022
The authenticity of host '[10.0.0.31]:10022 ([10.0.0.31]:10022)' can't be established.
ECDSA key fingerprint is 74:a1:80:00:85:17:d5:ec:57:7a:cb:cb:1e:7d:4a:1f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.0.0.31]:10022' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 14.04 LTS (GNU/Linux 4.4.0-98-generic x86_64) * Documentation: https://help.ubuntu.com/ The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law. root@0f8481ffd0d0:~#

2、使用Dockerfile创建


1.创建工作目录


[root@docker ~]# mkdir -p sshd_ubuntu
[root@docker ~]# ls
anaconda-ks.cfg daemon.json docker-pid sshd_ubuntu
[root@docker ~]#

  在其中创建Dockerfile和run.sh文件:


[root@docker ~]# cd sshd_ubuntu/ && touch Dockerfile run.sh
[root@docker sshd_ubuntu]# ls
Dockerfile run.sh
[root@docker sshd_ubuntu]#

2.编写run.sh脚本和authorized_keys文件


[root@docker sshd_ubuntu]# vim run.sh
[root@docker sshd_ubuntu]# cat run.sh
#!/bin/bash
/usr/sbin/sshd -D &
[root@docker sshd_ubuntu]# cat /root/.ssh/id_rsa.pub > ./authorized_keys
[root@docker sshd_ubuntu]#

3.编写Dockerfile


[root@docker sshd_ubuntu]# cat Dockerfile
# 基础镜像信息
FROM ubuntu:14.04 # 维护者信息
MAINTAINER staryjie staryjie@163.com # 更新apt缓存、安装ssh服务
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir -p /var/run/sshd /root/.ssh
RUN sed -ri 's#session required pam_loginuid.so#session required pam_loginuid.so#g' /etc/pam.d/sshd # 配置免密要和自启动脚本
ADD authorized_keys /root/.ssh/authorized_keys
ADD run.sh /run.sh
RUN chmod 755 /run.sh # 暴露22端口
EXPOSE 22 # 设置脚本自启动
CMD ["/run.sh"]
[root@docker sshd_ubuntu]#

4.创建镜像


[root@docker ~]# cd ~/sshd_ubuntu/ && docker build -t sshd:ubuntu2 .
Removing intermediate container e86118d7da77
Successfully built 12abdcc3350f
Successfully tagged sshd:ubuntu2
[root@docker sshd_ubuntu]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
sshd ubuntu2 12abdcc3350f 7 seconds ago 284MB
sshd ubuntu f328073a034a About an hour ago 284MB
centos 7 3fa822599e10 4 hours ago 204MB
mariadb latest d29cee62e770 27 hours ago 398MB
nginx latest 9e7424e5dbae 7 days ago 108MB
ubuntu 16.04 20c44cd7596f 12 days ago 123MB
ubuntu latest 20c44cd7596f 12 days ago 123MB
ubuntu 14.04 d6ed29ffda6b 12 days ago 221MB
busybox latest 6ad733544a63 3 weeks ago 1.13MB
centos latest d123f4e55e12 3 weeks ago 197MB
alpine latest 053cde6e8953 3 weeks ago 3.96MB
[root@docker sshd_ubuntu]#

5.测试镜像,运行容器


[root@docker sshd_ubuntu]# docker run -it --name ssh_test -p 10122:22 sshd:ubuntu2 bash
root@c03d5c93ec84:/# netstat -lnutp|grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 17/sshd
tcp6 0 0 :::22 :::* LISTEN 17/sshd
root@c03d5c93ec84:/#

宿主机ssh连接:



[root@docker ~]# ssh 10.0.0.31 -p 10122
The authenticity of host '[10.0.0.31]:10122 ([10.0.0.31]:10122)' can't be established.
ECDSA key fingerprint is 13:3a:46:78:aa:b0:ac:9b:75:1f:ba:99:82:c6:8b:76.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[10.0.0.31]:10122' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 14.04 LTS (GNU/Linux 4.4.0-98-generic x86_64) * Documentation: https://help.ubuntu.com/ The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright. Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law. root@c03d5c93ec84:~#
 

Ubuntu下面的docker开启ssh服务的更多相关文章

  1. win10下Linux子系统开启ssh服务

    原文:win10下Linux子系统开启ssh服务 为了便于交流共同学习,博主QQ群242629020(stm32-MCU认认真真交流群) 欢迎批评指导!!!电梯:https://jq.qq.com/? ...

  2. [Linux] 在 Ubuntu 19.10 上开启 SSH 服务并允许远程登录

    在 Ubuntu 19.10 上安装 SSH,并开启服务 0.检查并确认系统当前是否已安装SSH: sudo ps -e | grep ssh 如果只有 ssh-agent 说明 ssh-server ...

  3. Docker开启ssh服务

    一.准备 apt-get update       更新环境 apt-get install vim      安装vim vim  /etc/apt/source.list     更换软件源, 我 ...

  4. ubuntu 下安装和启动SSH 服务

    安装OPENSSH 服务端 sudo apt-get install openssh-server 查看进程是否启动 ps -e | grep ssh 删除密钥文件 rm /etc/ssh/ssh_h ...

  5. linux开启ssh服务

    本文概略:1)ubuntu发行版开启ssh.2)centos发行版开启ssh 1.ubuntu发行版安装/开启ssh服务 1.1 安装ssh服务端 sudo apt-get install opens ...

  6. Ubuntu下开启ssh服务

    网上有很多介绍在Ubuntu下开启SSH服务的文章,但大多数介绍的方法测试后都不太理想,均不能实现远程登录到Ubuntu上,最后分析原因是都没有真正开启ssh-server服务.最终成功的方法如下: ...

  7. Docker CentOS / Ubuntu容器开启 SSH 服务

    Docker CentOS / Ubuntu容器开启 SSH 服务 在CentOS容器内执行 yum install passwd openssl openssh-server -y # Ubuntu ...

  8. 让ubuntu开启ssh服务以及让vi/vim正常使用方向键与退格键

    VIM 修复方法: 安装vim full版本,在full版本下键盘正常,安装好后同样使用vi命令.ubuntu预装的是vim tiny版本,而需要的是vim full版本.执行下面的语句安装vim f ...

  9. ubuntu如何开启SSH服务

    ubuntu如何开启SSH服务 分类: Linux 运维与操作基础2013-02-24 13:33 2868人阅读 评论(0) 收藏 举报 sshd ubuntu ubuntu默认并没有安装ssh服务 ...

随机推荐

  1. vue2.0 父子组件数据传递prop

    vue的一个核心概念就是组件,而组件实例的作用域是孤立的,所以组件之间是不能直接引用其他组件的数据的.极端点举例来说,就是可以在同一个项目中,每一个组件内都可以定义相同名称的数据. data () { ...

  2. 使用Git将项目上传到GitHub管理

    首先你需要一个github账号.https://github.com/ 我们使用git需要先安装git工具. 1.进入Github首页,点击New repository新建一个项目 2.填写相应信息后 ...

  3. Java的字符串

    1.String 类 2.StringBuilder 类 1.String类 1.1.构造方法 String的构造方法格式 说明 new String(String st) 把字符串数据封装成字符串对 ...

  4. 轻松解决oracle11g 空表不能exp导出的问题。

    解决方法: 1插入一条数据(或者再删除),浪费时间,有时几百张表会累死的.2创建数据库之前使用代码: Sql代码 alter system set  deferred_segment_creation ...

  5. 重新指派usb转串口模块在linux系统中的设备调用名称

    How to remap /dev/ttyUSB* to a specific name to be called by my program. How to map /dev/ttyUSB* to ...

  6. Ruby学习笔记1 -- 基本语法和数据类型, Class

    Ruby 有4种数据类型:String, Boolen, Array, Hashes Ruby 有3种操作方法:Method, attribute, ?? Ruby 有xxx: Classes, Ob ...

  7. 关于微信支付接口,curl错误代码58

    微信支付接口,curl错误代码58 之前的微信付款到用户零钱都是好好的,今天运营来找我, 我想了了下,就是进行了网站搬家 看了下 微信支付相关的证书配置文件 知道了,在这个 要改下证书的路径 WxPa ...

  8. lampp中的ftp使用介绍

    搭建完毕lampp 具体要求如下:使用Lampp的proftpd,开通多个FTP用户,并各分配一个目录,而且需要限制用户在自己的目录里面,可以自由读写. 操作步骤:第一步:设置ftp用户组,输入命令: ...

  9. gzip0

    但是Apache是专门为PHP所匹配的,其兼容性最好),类似于IIS.下面我们具体来说说Apache里怎么启用gzip压缩: Apache启用gzip 如果要开启gzip的话,一定要打开下面二个模块. ...

  10. myeclipse项目 不能打开

    重启电脑后, myeclipse项目 不能打开了, 之前都是好好的!! 出现: Failed to read the project description file (.project) for ' ...