docker使用ssh远程连接容器(没钱买服务器又不想安装虚拟机患者必备)
突然有需求,需要使用go语言写个ssh终端连接功能,这时候手上又没有服务器,虚拟机也没有,正好使用docker搞起来
docker容器开启sshd服务,模拟服务器
我们知道docker是可以用exec来直接访问容器的,但是还不够high,有时候要模拟服务器的登录总不能用docker exec吧,来吧,老司机带你飞!
以centos为例,需要几步操作
1.安装openssh-server
2.初始化root用户密码
3.开启sshd服务
废话不多说,dockerfile献上
FROM centos
RUN yum install -y wget && \
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
yum install -y passwd && \
yum install -y openssh-server ssh-keygen && \
echo 'abcd1234' | passwd root --stdin
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q
#RUN systemctl enable sshd
CMD /usr/sbin/sshd && tail -f /var/log/wtmp
简单解释一下,
- 安装openssh-server
yum install -y wget && \
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo && \
yum install -y passwd && \
yum install -y openssh-server ssh-keygen
- 修改root密码为88888888
echo '' | passwd root --stdin
- 创建ssh-keygen创建相关的ssh文件,-q的意思是静默模式(就是默认是需要让你回车输入的,加上这个直接跳过)
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key -N "" -q && \
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N "" -q && \
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key -N "" -q
- 开启sshd服务,并用tail来前台执行阻止docker容器退出
CMD /usr/sbin/sshd && tail -f /var/log/wtmp
一、构建镜像
Dockerfile目录下执行,yeah,就是chenqionghe/centos镜像,你也可以弄成自己的,例如muscle/lightwegiht
docker build -t chenqionghe/centos .
二、运行容器
docker run --name centos_ssh -p 2222:22 -it chenqionghe/centos
三、使用ssh连接容器
这里使用了2222端口来映射容器里的22端口,运行起来就可以使用ssh连接了,输出设置好的88888888密码,注意,这里用的是2222映射的端口
➜ ~ ssh root@127.0.0.1 -p 2222
root@127.0.0.1's password:
Last login: Tue Nov 20 04:10:17 2018 from 172.17.0.1
[root@a8c8e0fbd74f ~]# ls -l /
total 56
-rw-r--r-- 1 root root 12030 Oct 6 19:15 anaconda-post.log
lrwxrwxrwx 1 root root 7 Oct 6 19:14 bin -> usr/bin
drwxr-xr-x 5 root root 360 Nov 20 04:09 dev
drwxr-xr-x 54 root root 4096 Nov 20 04:09 etc
drwxr-xr-x 2 root root 4096 Apr 11 2018 home
lrwxrwxrwx 1 root root 7 Oct 6 19:14 lib -> usr/lib
lrwxrwxrwx 1 root root 9 Oct 6 19:14 lib64 -> usr/lib64
drwxr-xr-x 2 root root 4096 Apr 11 2018 media
drwxr-xr-x 2 root root 4096 Apr 11 2018 mnt
drwxr-xr-x 2 root root 4096 Apr 11 2018 opt
dr-xr-xr-x 223 root root 0 Nov 20 04:09 proc
以上就是使用docker开启ssh模拟服务器的全过程,
附上ubuntu的Dockerfile,原理大同小异
FROM ubuntu:18.04 # Ali apt-get source.list
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list #安装openssh-server
RUN apt-get -y update && apt-get -y upgrade && apt-get install -y openssh-server #修改默认密码
RUN echo 'root:88888888' | chpasswd
RUN mkdir -p /run/sshd
# 允许登录
RUN sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config RUN apt-get install -y python vim curl CMD /usr/sbin/sshd -D && tail -f /var/log/wtmp
hight起来,light weight baby!
docker使用ssh远程连接容器(没钱买服务器又不想安装虚拟机患者必备)的更多相关文章
- docker 安装centos7并SSH远程连接
1.安装centos7 镜像 1.搜索并拉取centos镜像(默认最新镜像) docker search centos docker pull centos 2.建立本机对应docker-centos ...
- 远程连接Kali Linux使用PuTTY实现SSH远程连接
远程连接Kali Linux使用PuTTY实现SSH远程连接 本书主要以在Android设备上安装的Kali Linux操作系统为主,介绍基于Bash Shell渗透测试.由于在默认情况下,在Andr ...
- ssh远程连接docker中linux(ubuntu/centos)
ssh远程连接docker中linux(ubuntu/centos) https://www.jianshu.com/p/9e4d50ddc57e centos docker pull centos: ...
- ssh远程连接docker中的 linux container
ssh远程连接docker中的container 由于工作需要,要远程连接Container,本地机器是windows,以下为解决步骤: 1. 环境 本地:Windows ↓ Docker版本1. ...
- Web SSH远程连接利器:gotty
Web SSH远程连接利器:gotty 这个东东能让你使用浏览器连接你远程的机器! 一. 环境准备 下载https://github.com/yudai/gotty. 请先配置好 Golang 环境, ...
- 虚拟机VMware网络类型&&SSH远程连接Linux
前言: Linux专题是16年11月开始写,说来惭愧,已经5个月没学Linux,至今感觉连入门还没达到.暑假实习有投运维开发岗位,无奈对Linux不熟悉,校招简历也被刷了.so, 我打算先花1个月内的 ...
- 全新 Mac 安装指南(编程篇)(环境变量、Shell 终端、SSH 远程连接)
注:本文专门用于指导对计算机编程与设计(尤其是互联网产品开发与设计)感兴趣的 Mac 新用户,如何在 Mac OS X 系统上配置开发与上网环境,另有<全新 Mac 安装指南(通用篇)>作 ...
- CentOS 6.0修改ssh远程连接端口
转自:系统运维 » CentOS 6.0修改ssh远程连接端口 实现目的:把ssh默认远程连接端口修改为2222 方法如下: 1.编辑防火墙配置:vi /etc/sysconfig/iptables ...
- SSH 远程连接
ssh远程连接 准备工作: 1 准备两台linux pc 我们一般用的是VMware虚礼软件 2 这两台linux可以互通 3 linux1 :192.168.2.2 这台为你要连接的服务器 linu ...
随机推荐
- Git和Eclipse的使用、上传、部署
https://www.jianshu.com/p/812717c740a2 https://blog.csdn.net/lynn_Kun/article/details/73740400 https ...
- ps最最基础的文档
因为学习PHP,但是公司没有前端工程师,修图的时候只好找被人帮忙,一个简答的问题,其实几分钟就搞定了,还要麻烦别人,就自己学了一下ps.一共花了3天时间.学习了一些简单的操作. 工具:Adobe Ph ...
- python操作Redis安装、支持存储类型、普通连接、连接池
一.python操作redis安装和支持存储类型 安装redis模块 pip3 install redis 二.Python操作Redis之普通连接 redis-py提供两个类Redis和Strict ...
- Nginx 教程(1):基本概念
简介 嗨!分享就是关心!所以,我们愿意再跟你分享一点点知识.我们准备了这个划分为三节的<Nginx教程>.如果你对 Nginx 已经有所了解,或者你希望了解更多,这个教程将会对你非常有帮助 ...
- 模拟poj1350
http://poj.org/problem?id=1350 题意:给你一个数,你用这个数重排序后的最大值减去最小值,当这个差值等于0或者6174时就结束,否则就用这个差值再排序再求差值.如果这个数不 ...
- logminer日志挖掘
参考自:https://blog.csdn.net/yes_is_ok/article/details/79296614 原文转自:http://blog.itpub.net/26736162/vie ...
- VS2013和NuGet
1.前言 有时候在使用VS2013时需要用到第三方的dll,这时候NuGet就是一个很方便的工具.但是这个小东东也是和VS不同的版本相关的,最开始不知道,乱安装一气,最后就是很多情况下不能用.这两天在 ...
- 于bugku中游荡意外得到关于CBC翻转攻击思路
个人简介:渣渣一枚,萌新一个,会划水,会喊六六今天在bugku遇到关于CBC翻转攻击的题目,总结了一下关于CBC翻转攻击的原理,以及关于这道题目的解题思路个人博客:https://www.cnblog ...
- JAVA编程思想的理解
1)POP--面向过程编程(Process-oriented programming ): 面向过程编程是以功能为中心来进行思考和组织的一种编程方法,它强调的是系统的数据被加工和处理的过程,在程序 ...
- Transaction rolled back because it has been marked as rollback-only
出现这种错误的原因 1.接口A 调用了接口B 2.接口B报异常了,没有在B里面进行try catch捕获 3.接口A对 接口B进行了try catch捕获 因为接口B报异常 会把当前事物A接口的事物( ...