先启动一个纯的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就能成功。