docker run -it --name=sample centos /bin/bash
然后打ssh localhost会发现如下错误。
bash: ssh: command not found
接下去就教你如何能够ssh localhost.
1、yum install openssh-server
2、yum install openssh-clients
这时候再试一下ssh localhost,发现错误改变:
ssh: connect to host localhost port 22: Cannot assign requested address
说明sshd服务还没有开启。用ps -ef也可以验证这一点。
由于是docker里面的centos,所以service和systemctl都不好用。
尝试手动运行/usr/sbin/sshd
报如下错误:
1、Could not load host key: /etc/ssh/ssh_host_rsa_key
2、Could not load host key: /etc/ssh/ssh_host_ecdsa_key
3、Could not load host key: /etc/ssh/ssh_host_ed25519_key
4、sshd: no hostkeys available -- exiting.
手动执行/usr/sbin/sshd-keygen -A
再执行/usr/sbin/sshd成功。
为了免密码本机跳本机,执行如下命令:
1、ssh-keygen -t rsa -P ‘‘ -f ~/.ssh/id_rsa
2、cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
3、chmod 0600 ~/.ssh/authorized_keys
至此,执行ssh localhost就能成功。
原文:https://blog.51cto.com/14116247/2413745