用Telnet登录Docker容器中的Centos7

时间:2024-03-20 10:35:33

用Telnet登录Docker容器中的Centos7

背景介绍

最近想用python3的telnetlib写一个telnet的客户端,但是发现写完之后无法进行完整的测试,又不想安装一个linux的虚拟机,所以docker pull了centos。下面是完整过程

流程图:

Created with Raphaël 2.1.2本机telnet5000端口docker-machine处理映射到容器23端口telnet登录

实现过程

安装Docker

过程省略

Docker中使用CentOS7镜像

下载镜像

~ $ docker pull centos

启动镜像

~ $ docker run -d -p 5000:23 --name centos7 --privileged=true centos /usr/sbin/init

–pri以特权模式启动,可以在容器中启动多个服务。-p参数进行端口映射,使容器23端口映射到本机5000端口。

进入容器bash

~ $ docker exec -ti a36 /bin/bash

3ab为容器id,可以用docker ps -a查看,使用位数只需能唯一表示容器即可。

centos7查看IP地址

安装net-tools,一路yes

[[email protected] sbin]# yum install net-tools
[[email protected] sbin]# ifconfig

安装telnet服务

[[email protected] /]# yum install telnet-server
[[email protected] /]# yum install telnet
[[email protected] /]# yum install xinetd

设置开机自启动

[[email protected] /]# systemctl enable xinetd.service
[[email protected] /]# systemctl enable telnet.socket

重启服务

[[email protected] /]# systemctl start telnet.socket
[[email protected] /]# systemctl start xinetd

添加一个用户用来以telnet登录,因为Linux拒绝明文密码

[[email protected] /]# adduser zhaojunyu
[[email protected] /]# passwd zhaojunyu

本机测试telnet连接

~ $ telnet localhost 5000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Password:
Last login: Fri Jun 22 11:44:43 on pts/0

[[email protected] ~]$ su root
Password:

[[email protected] zhaojunyu]#

效果图

用Telnet登录Docker容器中的Centos7