先启动一个纯的centos容器

docker run -it --name=sample centos /bin/bash

然后打ssh localhost会发现如下错误。

bash: ssh: command not found

接下去就教你如何能够ssh localhost.

yum install openssh-server
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

报如下错误:

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就能成功。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐