1. 程式人生 > 實用技巧 >ssh方式訪問centos容器

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.
[email protected]'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