+-
mac下ssh连接docker的方法

1、进入docker中的linux需要安装ssh服务

apt-get update
apt-get install openssh-server openssh-client

2、设置docker的root登录权限为yes

修改/etc/ssh/sshd_config文件,找到PermitRootLogin,参数修改为yes

vim /etc/ssh/sshd_config

3、修改docker的root密码

使用passwd root命令修改root登录密码

passwd root

4、确认docker中密匙是否存在,没有则需要创建

如下是不存在密匙

root@c223a9d1c7a0:/home# ll /etc/ssh
total 560
drwxr-xr-x 1 root root   4096 Oct 10 03:29 ./
drwxr-xr-x 1 root root   4096 Oct 10 03:32 ../
-rw-r--r-- 1 root root 553122 Feb 10  2018 moduli
-rw-r--r-- 1 root root   1580 Feb 10  2018 ssh_config
-rw-r--r-- 1 root root   3800 Oct 10 03:29 sshd_config

生成主机密匙(执行如下命令时可以一路回车):

生成rsa_key (-t表示生成的密钥所使用的加密类型;-f项后接要生成的密钥文件名)
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
生成ecdsa_key
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
生成ed25519_key。
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

5、退出容器

exit命令退出容器

6、提交这个container为一个新的image

docker commit <容器id> <镜像名称>

7、使用新的镜像启动一个指定了ip映射的新容器

把容器的22端口映射到虚拟机的8122端口

docker run --name <容器名称> -i -t -p 8122:22 <镜像ID> /usr/sbin/sshd -D

8、在主机ssh登录docker

ssh root@localhost -p 8122

终身学习!