1、拉取镜像:docker pull centos:7
2、运行容器并进入:docker run -i -t -p 1022:22 --name ssh centos:7 /bin/bash
3、安装passwd,openssl,openssh-server:yum install passwd openssl openssh-server -y
4、启动sshd:/usr/sbin/sshd -D &
这时报以下错误:
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
5、执行以下命令解决:
# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
6、修改 /etc/ssh/sshd_config 配置信息
UsePAM yes 改为 UsePAM no
UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no
7、重启ssh:/usr/sbin/sshd -D &
参考:https://www.cnblogs.com/ruanqj/p/7374544.html