ssh方式访问centos容器

1. 准备centos镜像

1、下载镜像
docker pull centos:7.4:7.5.1804

2、启动容器
[root@localhost ~]# docker run -d -p 8002:22  --privileged=true centos:7.5.1804 /usr/sbin/init 
1b11913110f02391bd93bae273acfb6f357f2ce7bf7742eb01d267da80a690c7
# 注:-d 后台启动; -p 指定端口,这里将centos容器的22端口映射到宿主机的8002端口
[root@localhost ~]# docker exec -it 1b11913110f02391bd93bae273acfb6f357f2ce7bf7742eb01d267da80a690c7 /bin/bash

3、查看8002 端口动态
[root@localhost ~]# ps -aux|grep 8002
root       31662  0.0  0.2 364448 11160 ?        Sl   14:01   0:00 /usr/bin/docker-proxy -proto tcp -host-ip 0.0.0.0 -host-port 8002 -container-ip 172.17.0.3 -container-port 22
root       33341  0.0  0.0 112668   972 pts/4    S+   14:12   0:00 grep --color=auto 8002

2. 到容器中的操作

1、安装工具
yum install net-tools -y
yum install openssh openssh-server openssh-clients
2、启动sshd
systemctl start sshd
systemctl restart sshd
systemctl enable sshd
3.远程连接容器
[root@localhost ~]# ssh root@宿主机ip -p 8002
The authenticity of host '[172.30.4.129]:8002 ([172.30.4.129]:8002)' can't be established.
ECDSA key fingerprint is SHA256:9V4gfFbgLgowby4NlDW9bTl4TRmNE66GT1PJvIFJ+nw.
ECDSA key fingerprint is MD5:2b:4a:ec:c9:95:36:57:39:49:ed:49:d4:56:28:f1:f0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[172.30.4.129]:8002' (ECDSA) to the list of known hosts.
root@172.30.4.129's password: 
Last login: Mon Sep  7 06:15:20 2020 from 172.30.4.65
[root@1b11913110f0 /]# ls
bin  dev  etc  home  lib  lib64  media  mnt  opt  proc  qqc_da  root  run  sbin  srv  sys  tmp  usr  var

posted @ 2020-09-11 20:39  朝朝哥  阅读(490)  评论(0编辑  收藏  举报