突然有需求,需要使用go语言写个ssh终端连接功能,这时候手上又没有服务器,虚拟机也没有,正好使用docker搞起来

docker容器开启sshd服务,模拟服务器

我们知道docker是可以用exec来直接访问容器的,但是还不够high,有时候要模拟服务器的登录总不能用docker exec吧,来吧,老司机带你飞!

以centos为例,需要几步操作

1.安装openssh-server

2.初始化root用户密码

3.开启sshd服务

废话不多说,dockerfile献上

  1. FROM centos
  2. RUN yum install -y wget && \
  3. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
  4. yum install -y passwd && \
  5. yum install -y openssh-server ssh-keygen && \
  6. echo 'abcd1234' | passwd root --stdin
  7. RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \
  8. ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \
  9. ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q
  10. #RUN systemctl enable sshd
  11. CMD /usr/sbin/sshd && tail -f /var/log/wtmp

简单解释一下,

  • 安装openssh-server
  1. yum install -y wget && \
  2. wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
  3. yum install -y passwd && \
  4. yum install -y openssh-server ssh-keygen
  • 修改root密码为88888888
  1. echo '' | passwd root --stdin
  • 创建ssh-keygen创建相关的ssh文件,-q的意思是静默模式(就是默认是需要让你回车输入的,加上这个直接跳过)
  1. RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \
  2. ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \
  3. ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q
  • 开启sshd服务,并用tail来前台执行阻止docker容器退出
  1. CMD /usr/sbin/sshd && tail -f /var/log/wtmp

一、构建镜像

Dockerfile目录下执行,yeah,就是chenqionghe/centos镜像,你也可以弄成自己的,例如muscle/lightwegiht

  1. docker build -t chenqionghe/centos .

二、运行容器

  1. docker run --name centos_ssh -p 2222:22 -it chenqionghe/centos  

三、使用ssh连接容器

这里使用了2222端口来映射容器里的22端口,运行起来就可以使用ssh连接了,输出设置好的88888888密码,注意,这里用的是2222映射的端口  

  1. ~ ssh root@127.0.0.1 -p 2222
  2. root@127.0.0.1's password:
  3. Last login: Tue Nov 20 04:10:17 2018 from 172.17.0.1
  4. [root@a8c8e0fbd74f ~]# ls -l /
  5. total 56
  6. -rw-r--r-- 1 root root 12030 Oct 6 19:15 anaconda-post.log
  7. lrwxrwxrwx 1 root root 7 Oct 6 19:14 bin -> usr/bin
  8. drwxr-xr-x 5 root root 360 Nov 20 04:09 dev
  9. drwxr-xr-x 54 root root 4096 Nov 20 04:09 etc
  10. drwxr-xr-x 2 root root 4096 Apr 11 2018 home
  11. lrwxrwxrwx 1 root root 7 Oct 6 19:14 lib -> usr/lib
  12. lrwxrwxrwx 1 root root 9 Oct 6 19:14 lib64 -> usr/lib64
  13. drwxr-xr-x 2 root root 4096 Apr 11 2018 media
  14. drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt
  15. drwxr-xr-x 2 root root 4096 Apr 11 2018 opt
  16. dr-xr-xr-x 223 root root 0 Nov 20 04:09 proc

以上就是使用docker开启ssh模拟服务器的全过程,

附上ubuntu的Dockerfile,原理大同小异

  1. FROM ubuntu:18.04
  2.  
  3. # Ali apt-get source.list
  4. RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
  5. echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list && \
  6. echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list && \
  7. echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
  8. echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
  9. echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
  10. echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list && \
  11. echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list && \
  12. echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
  13. echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
  14. echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list
  15.  
  16. #安装openssh-server
  17. RUN apt-get -y update && apt-get -y upgrade && apt-get install -y openssh-server
  18.  
  19. #修改默认密码
  20. RUN echo 'root:88888888' | chpasswd
  21. RUN mkdir -p /run/sshd
  22. # 允许登录
  23. RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config
  24.  
  25. RUN apt-get install -y python vim curl
  26.  
  27. CMD /usr/sbin/sshd -D && tail -f /var/log/wtmp

hight起来,light weight baby!

docker使用ssh远程连接容器(没钱买服务器又不想安装虚拟机患者必备)的更多相关文章

  1. docker 安装centos7并SSH远程连接

    1.安装centos7 镜像 1.搜索并拉取centos镜像(默认最新镜像) docker search centos docker pull centos 2.建立本机对应docker-centos ...

  2. 远程连接Kali Linux使用PuTTY实现SSH远程连接

    远程连接Kali Linux使用PuTTY实现SSH远程连接 本书主要以在Android设备上安装的Kali Linux操作系统为主,介绍基于Bash Shell渗透测试.由于在默认情况下,在Andr ...

  3. ssh远程连接docker中linux(ubuntu/centos)

    ssh远程连接docker中linux(ubuntu/centos) https://www.jianshu.com/p/9e4d50ddc57e centos docker pull centos: ...

  4. ssh远程连接docker中的 linux container

    ssh远程连接docker中的container   由于工作需要,要远程连接Container,本地机器是windows,以下为解决步骤: 1. 环境 本地:Windows ↓ Docker版本1. ...

  5. Web SSH远程连接利器:gotty

    Web SSH远程连接利器:gotty 这个东东能让你使用浏览器连接你远程的机器! 一. 环境准备 下载https://github.com/yudai/gotty. 请先配置好 Golang 环境, ...

  6. 虚拟机VMware网络类型&&SSH远程连接Linux

    前言: Linux专题是16年11月开始写,说来惭愧,已经5个月没学Linux,至今感觉连入门还没达到.暑假实习有投运维开发岗位,无奈对Linux不熟悉,校招简历也被刷了.so, 我打算先花1个月内的 ...

  7. 全新 Mac 安装指南(编程篇)(环境变量、Shell 终端、SSH 远程连接)

    注:本文专门用于指导对计算机编程与设计(尤其是互联网产品开发与设计)感兴趣的 Mac 新用户,如何在 Mac OS X 系统上配置开发与上网环境,另有<全新 Mac 安装指南(通用篇)>作 ...

  8. CentOS 6.0修改ssh远程连接端口

    转自:系统运维 » CentOS 6.0修改ssh远程连接端口 实现目的:把ssh默认远程连接端口修改为2222 方法如下: 1.编辑防火墙配置:vi /etc/sysconfig/iptables ...

  9. SSH 远程连接

    ssh远程连接 准备工作: 1 准备两台linux pc 我们一般用的是VMware虚礼软件 2 这两台linux可以互通 3 linux1 :192.168.2.2 这台为你要连接的服务器 linu ...

随机推荐

  1. 2019.03.09 bzoj5371: [Pkusc2018]星际穿越(主席树)

    传送门 题意简述: 给一个序列,对于第iii个位置,它跟[limi,i−1][lim_i,i-1][limi​,i−1]这些位置都存在一条长度为111的无向边. 称dist(u,v)dist(u,v) ...

  2. 1004 Counting Leaves 对于树的存储方式的回顾

    一种新的不使用左右子树递归进行树高计算的方法,使用层次遍历 树的存储方式: 1.本题提供的一种思路: 使用(邻接表的思想)二维数组(vector[n])表示树,横坐标表示 父节点,每一行表示孩子. 能 ...

  3. pycharm License server激活

    2018-11-15 pycharm License server激活有效:https://idea.ouyanglol.com/

  4. Javaweb 编解码流程

    参考: https://www.ibm.com/developerworks/cn/java/j-lo-chinesecoding/#N10263 https://www.cnblogs.com/ch ...

  5. 【python-appium】模拟手机按键搜索异常

    执行代码的过程中运行self.driver.press_keycode(84)设备没反映,则需要关闭#desired_caps["unicodeKeyboard"] = " ...

  6. Py福利,基于uiautomatorviewer 的Python 自动化代码自动生成工具分享(jar已发布GitHub,欢迎Star)

    前言做UI自动化无论你用SDK自带的uiautomatorviewer还是Macaca还是Appium自动的inspector,代码最多的就是那些繁琐重复的找元素后点击,输入,长按.....等.现在偷 ...

  7. 大数据之hiveSQL

    最近增加了学习java基础算法,包括几种排序算法,二叉树(前序,后序,中序),队列和栈,bmp搜索,广义搜索算法,迭代等等一些技巧(自己动手绝对比单纯的理论要强的多,多练练) HIVE是hadoop生 ...

  8. Debian中配置静态IP

    默认安装Debian的时候是用dhcp服务的,有时我们需要设置一下静态IP. 一共涉及两个文件的修改 /etc/network/interfaces auto eth0#iface eth0 inet ...

  9. [转] The QCOW2 Image Format

    The QCOW2 Image Format https://people.gnome.org/~markmc/qcow-image-format.html The QCOW image format ...

  10. Redis Cluster [WARNING] Node 127.0.0.1:7003 has slots in migrating state (15495).

    错误描述 在迁移一个节点上的slot到另一个节点的时候卡在其中的一个slot报错,截图如下: 查询发现在15495的这个slot上面存在一个key,但是并没有发现这个key有什么问题.使用fix进行修 ...