centos 实现ssh远程连接docker
为什么80%的码农都做不了架构师?>>>
一、查看当前镜像
[root@iZ25av9xi4hZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos latest 980e0e4c79ec 3 weeks ago 196.7 MB
[root@iZ25av9xi4hZ ~]#
上面的centos 是通过docker pull centos拉取获得的最新centos7 版本
二、创建一个容器,并配置ssh、tomcat、jdk
2.1 创建一个容器(命名为base_centos)
[root@iZ25av9xi4hZ ~]# docker run -it --name base_centos centos:latest /bin/bash
[root@cf6b692adf02 /]#
2.2 安装net-tools, iproute
net-tools 可以使用ifconfig等命令
[root@cf6b692adf02 /]# yum install -y net-tools
iproute 可以使用ip add 查看网络配置
[root@cf6b692adf02 /]# yum install -y iproute
3、sshd安装,及配置
3.1 安装openssh
[root@cf6b692adf02 /]# yum install -y openssh
[root@cf6b692adf02 /]# yum install -y openssh-server
[root@cf6b692adf02 /]# yum install -y openssh-clients
3.2 配置私钥
输入 命令显示如下,分别配置私钥。其中输入的地方全部选择enter键跳过
[root@cf6b692adf02 /]#/usr/sbin/sshd
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
[root@cf6b692adf02 /]# ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
d6:46:9c:ef:bf:5d:45:95:59:50:b9:9b:fa:a6:1d:3d root@cf6b692adf02
The key's randomart image is:
+--[ RSA 2048]----+
| .oO|
| . . +.|
| + o|
| o . o |
| S o . +|
| . . . oo|
| . .Eo|
| o.o+|
| .*+.|
+-----------------+
[root@cf6b692adf02 /]# ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
Generating public/private ecdsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key.
Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub.
The key fingerprint is:
7f:ad:3b:5f:93:c2:6e:f0:05:f8:75:80:18:f8:ba:83 root@cf6b692adf02
The key's randomart image is:
+--[ECDSA 256]---+
| ..o . |
| . . . . |
| . . . |
| .. . ..|
| S. . o .|
| .. ..o ..|
| . .. +ooo.|
| E o .o+...|
| . +=. |
+-----------------+
[root@cf6b692adf02 /]#
[root@cf6b692adf02 /]# ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key
Generating public/private ed25519 key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /etc/ssh/ssh_host_ed25519_key.
Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub.
The key fingerprint is:
44:93:0d:94:eb:e2:a4:3e:a3:fe:ab:e7:4f:2e:f0:44 root@cf6b692adf02
The key's randomart image is:
+--[ED25519 256--+
| .== |
| .o.. |
| .. |
| E .. |
| . .S |
| . . o . |
| + +.. |
| Bo. |
| .+B=*o |
+-----------------+
[root@cf6b692adf02 /]#
最后执行一次,再查看sshd进程,发现是启动的。
[root@cf6b692adf02 /]#ps -a | grep sshd
[root@cf6b692adf02 /]# /usr/sbin/sshd
[root@cf6b692adf02 /]# ps -ef | grep sshd
root 109 1 0 13:16 ? 00:00:00 /usr/sbin/sshd
root 128 1 0 13:22 ? 00:00:00 grep --color=auto sshd
[root@cf6b692adf02 /]#
将sshd 加入开机自启(/etc/rc.d/rc.local)
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.touch /var/lock/subsys/local
/usr/sbin/sshd
4、tomcat 安装及配置
4.1 安装wget命令
#yum install -y wget
4.2 下载tomcat8
[root@cf6b692adf02 tmp]# wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.5.5/bin/apache-tomcat-8.5.5.tar.gz
4.3 安装tomcat8,并加入开机自启
5、jdk 安装配置
[root@cf6b692adf02 tmp]#wget http://download.oracle.com/otn-pub/java/jdk/8u101-b13/jdk-8u101-linux-x64.rpm?AuthParam=1475328855_221393517c76253d935635ef2ec114d1[root@cf6b692adf02 tmp]#mv jdk-8u101-linux-x64.rpm?AuthParam=1475328855_221393517c76253d935635ef2ec114d1 jdk.rpm
[root@cf6b692adf02 tmp]#rpm -ivh jdk.rpm
Preparing... ################################# [100%]
Updating / installing...1:jdk1.8.0_101-2000:1.8.0_101-fcs ################################# [100%]
Unpacking JAR files...tools.jar...plugin.jar...javaws.jar...deploy.jar...rt.jar...jsse.jar...charsets.jar...localedata.jar...[root@cf6b692adf02 tmp]# java -version
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
[root@cf6b692adf02 tmp]#
jdk rpm安装在/usr/java/jdk1.8.0_101/jre/bin/java
5、写脚本启动关闭tomcat
5.1 安装vim
[root@cf6b692adf02 tomcat]# yum install vim* -y
五、
5.1 创建新的镜像文件
将之前做的容器弄成镜像文件,取名base:latest
[root@iZ25av9xi4hZ ~]# docker commit cf6b692adf02 base:latest
sha256:a90294e9b9b5b375c895ff32bfd34120797e8391bdbcbfa53b3792d636280f70
查看镜像下载所有的镜像文件
[root@iZ25av9xi4hZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
base latest a90294e9b9b5 2 minutes ago 934.9 MB
docker.io/centos latest 980e0e4c79ec 3 weeks ago 196.7 MB
[root@iZ25av9xi4hZ ~]#
5.2创建tomcat_cl 容器,并设置其ssh对于宿主机器的10022端口,8080端口对应宿主机器的10088端口
[root@iZ25av9xi4hZ ~]# docker run -p 10022:22 -p 10088:8080 --name tomcat_cl -d base:latest /usr/sbin/sshd -D
e21a8af9269cd06b3950f59020de4d29723580c20bd35334ea6ff3fed28fb043
[root@iZ25av9xi4hZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e21a8af9269c base:latest "/usr/sbin/sshd -D" 7 seconds ago Up 6 seconds 0.0.0.0:10022->22/tcp, 0.0.0.0:10088->8080/tcp tomcat_cl
cf6b692adf02 centos:latest "/bin/bash" About an hour ago Up 10 minutes base_centos
[root@iZ25av9xi4hZ ~]#
5.3 ssh登录容器
[root@iZ25av9xi4hZ ~]# ssh root@127.0.0.1 -p 10022
The authenticity of host '[127.0.0.1]:10022 ([127.0.0.1]:10022)' can't be established.
ECDSA key fingerprint is 7f:ad:3b:5f:93:c2:6e:f0:05:f8:75:80:18:f8:ba:83.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[127.0.0.1]:10022' (ECDSA) to the list of known hosts.
root@127.0.0.1's password:
Permission denied, please try again.
开始没有创建密码,所以叫修改一下密码
[root@iZ25av9xi4hZ ~]# docker exec -it tomcat_cl /bin/bash
[root@e21a8af9269c /]# passwd
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@e21a8af9269c /]# exit
exit
[root@iZ25av9xi4hZ ~]#
再次ssh ,可以正常进入
[root@iZ25av9xi4hZ ~]# ssh root@127.0.0.1 -p 10022
root@127.0.0.1's password:
[root@e21a8af9269c ~]#
5.4 在宿主机器检查对应的映射端口
[root@iZ25av9xi4hZ ~]# ps -aux | grep 10022
root 17088 0.0 1.5 188360 15708 ? Sl 22:41 0:00 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 10022 -container-ip 192.168.0.3 -container-port 22
root 17218 0.0 0.0 112660 960 pts/3 S+ 22:46 0:00 grep --color=auto 10022
[root@iZ25av9xi4hZ ~]# ps -aux | grep 10088
root 17079 0.0 1.5 131020 15652 ? Sl 22:41 0:00 docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 10088 -container-ip 192.168.0.3 -container-port 8080
root 17226 0.0 0.0 112664 960 pts/3 S+ 22:47 0:00 grep --color=auto 10088
[root@iZ25av9xi4hZ ~]#
进入容器去启动tomcat
[root@iZ25av9xi4hZ ~]# ssh root@127.0.0.1 -p 10022
root@127.0.0.1's password:
[root@e21a8af9269c ~]# ps -ef | grep java
root 178 162 0 14:52 pts/0 00:00:00 grep --color=auto java
[root@e21a8af9269c ~]# /app/apache-tomcat-cl/bin/startup.sh
Using CATALINA_BASE: /app/apache-tomcat-cl
Using CATALINA_HOME: /app/apache-tomcat-cl
Using CATALINA_TMPDIR: /app/apache-tomcat-cl/temp
Using JRE_HOME: /usr
Using CLASSPATH: /app/apache-tomcat-cl/bin/bootstrap.jar:/app/apache-tomcat-cl/bin/tomcat-juli.jar
Tomcat started.
[root@e21a8af9269c ~]# ps -ef | grep java root 193 1 56 14:52 pts/0 00:00:02 /usr/bin/java -Djava.util.logging.config.file=/app/apache-tomcat-cl/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -classpath /app/apache-tomcat-cl/bin/bootstrap.jar:/app/apache-tomcat-cl/bin/tomcat-juli.jar -Dcatalina.base=/app/apache-tomcat-cl -Dcatalina.home=/app/apache-tomcat-cl -Djava.io.tmpdir=/app/apache-tomcat-cl/temp org.apache.catalina.startup.Bootstrap start
root 210 162 0 14:52 pts/0 00:00:00 grep --color=auto java
[root@e21a8af9269c ~]#
通过浏览器输入宿主ip:端口。可以看到tomcat运行起来了。
docker一个小的案例容器搞定,有什么错误的地方欢迎指出
转载于:https://my.oschina.net/jywm/blog/754123
centos 实现ssh远程连接docker相关推荐
- 解决华为云CentOS 7 SSH 远程连接不上的问题
买了华为云的多台ECS,使用的CentOS 7.5 x64系统,对系统做了网络优化,优化的参数在公司LAN和阿里云服务器上均试验无问题. 问题描述: 1. 使用一段时间后,会出现不定期的公司办公室内s ...
- docker 安装centos7并SSH远程连接
1.安装centos7 镜像 1.搜索并拉取centos镜像(默认最新镜像)docker search centosdocker pull centos2.建立本机对应docker-centos76 ...
- Centos 7无法SSH远程连接及解决方法
Centos 7无法SSH远程连接及解决方法 小土豆Linux学习随笔 -- 清听凌雪慕忆 以前运维中,服务器或者虚拟机安装完Centos 7系统,配置网络设置后即可利用SSH远程.昨夜,怪哉,不 ...
- VSCode通过SSH远程连接CentOS主机的问题解决记录
VSCode通过SSH远程连接CentSO主机的问题解决记录 为了明确讨论问题,我们将安装了VSCode和SSH Remote插件的主机称为远控主机,将被SSH连接的Linux主机称为被控主机. 问题 ...
- Linux下SSH远程连接断开后让程序继续运行解决办法
screen -S yourname #新建一个叫yourname的session screen -r yourname #回到yourname这个session screen -X -S [your ...
- 解决阿里云 ssh 远程连接短时间没操作就会断掉的问题
在本地主机(例如:你自己的 MacBook)中打开 sshd 的配置文件,命令语句如下: sudo vim /etc/ssh/sshd_config 注意:使用用户 root 编辑配置文件,否则会报错 ...
- 【Linux 主机ssh远程连接暴力破解详解】
昨天,安全老师布置了一个安全的小作业,在下想了想,做什么好呢?最近学了很多.那就做一个Linux主机远程连接的暴力破解密码的作业. 一.前期信息搜集 kali 作为黑客主机,centos作为被攻击主机 ...
- 远程连接docker里的Linux容器
远程连接docker里的Linux容器 安装容器的SSH服务 apt-get install openssh-server 安装vim编辑器 apt-get install vim 安装lrzsz a ...
- SSH远程连接云服务器出错
SSH远程连接华为云出错 错误信息 背景 解决办法 错误信息 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING ...
最新文章
- 2018-3-27 遗传算法中的轮盘赌
- 香港科技大学谢知遥教授实验组招收机器学习全奖博士生
- VS报错:此项目需要缓解Spectre漏洞的库
- 设置让php能够以root权限来执行exec() 或者 shell_exec()
- 基于 abp vNext 和 .NET Core 开发博客项目 - 定时任务最佳实战(三)
- 倒计时3天|阿里云数据库创新上云峰会亮点剧透
- 621. Task Scheduler 任务调度器
- 在 JQuery Mobile 中实现瀑布流图库布局
- mysql 开发 生产_在没有表锁定的情况下在巨大的MySQL生产表...
- svn拉取文件合并_四、Jenkins+SVN拉取文件下发
- 愚人节的礼物 栈
- 促进大数据发展行动纲要
- 轻量纯css框架,网页设计的12个轻量CSS框架
- 许三多身上的职业精神
- 在线ASCII转换工具
- 无法打开登录所请求的数据库 。登录失败。用户*登录失败。解决办法
- 已解决vue-router4路由报“[Vue Router warn]: No match found for location with path“
- Premature optimization is the root of all evil.
- Windows下Git服务(Bonobo)安装
- 蒙特卡洛模拟方法的matlab实现(2)
热门文章
- Architecture(5)电商APP组件化探索
- Ansible 详细用法部署安装
- (翻译) MongoDB(15) 在 Tarball 上安装MongoDB社区版
- 使用dd工具对磁盘RAID5和10进行I/O性能测试
- C语言第12轮:指针
- Apache HttpClient POST数据(https)
- Apache 别名与重定向
- Quartz+Spring 自定义作业调度(作业在DB中配置)
- php完全匹配,如何在PHP中使用正则表达式找到完全匹配的内容?
- 开源 Android pdf 阅读器开发总结