先啓動一個純的centos容器docker
docker run -it --name=sample centos /bin/bashcentos
而後打ssh localhost會發現以下錯誤。bash
bash: ssh: command not foundssh
接下去就教你如何可以ssh localhost.ide
一、yum install openssh-server
二、yum install openssh-clients
這時候再試一下ssh localhost,發現錯誤改變:
ssh: connect to host localhost port 22: Cannot assign requested addressserver
說明sshd服務尚未開啓。用ps -ef也能夠驗證這一點。it
因爲是docker裏面的centos,因此service和systemctl都很差用。class
嘗試手動運行/usr/sbin/sshd容器
報以下錯誤:cli
一、Could not load host key: /etc/ssh/ssh_host_rsa_key
二、Could not load host key: /etc/ssh/ssh_host_ecdsa_key
三、Could not load host key: /etc/ssh/ssh_host_ed25519_key
四、sshd: no hostkeys available -- exiting.
手動執行/usr/sbin/sshd-keygen -A
再執行/usr/sbin/sshd成功。
爲了免密碼本機跳本機,執行以下命令:
一、ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
二、cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
三、chmod 0600 ~/.ssh/authorized_keys
至此,執行ssh localhost就能成功。