mac下ssh鏈接docker的方法

一、進入docker中的linux須要安裝ssh服務

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

二、設置docker的root登陸權限爲yes

修改/etc/ssh/sshd_config文件,找到PermitRootLogin,參數修改成yeslinux

vim /etc/ssh/sshd_config

三、修改docker的root密碼

使用passwd root命令修改root登陸密碼docker

passwd root

四、確認docker中密匙是否存在,沒有則須要建立

以下是不存在密匙vim

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

生成主機密匙(執行以下命令時能夠一路回車):ssh

  1. 生成rsa_key (-t表示生成的密鑰所使用的加密類型;-f項後接要生成的密鑰文件名)
ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
  1. 生成ecdsa_key
ssh-keygen -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key
  1. 生成ed25519_key。
ssh-keygen -t ed25519 -f /etc/ssh/ssh_host_ed25519_key

五、退出容器

exit命令退出容器學習

六、提交這個container爲一個新的image

docker commit <容器id> <鏡像名稱>

七、使用新的鏡像啓動一個指定了ip映射的新容器

把容器的22端口映射到虛擬機的8122端口加密

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

八、在主機ssh登陸docker

ssh root@localhost -p 8122code

終身學習!
相關文章
相關標籤/搜索