- 启动centos的docker容器
-
docker run -it --name ssh centos /bin/bash
- 进入容器的交互模式后,升级并安装openssh-server
-
yum -y update yum install -y openssh-server
- 编辑sshd的配置文件/etc/ssh/sshd_config,将其中UsePAM参数设置成“no”
- 生成RSA密钥对,一路回车
-
ssh-keygen -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key
-
在宿主机也生成秘钥对,把生成的公钥文件拷贝到容器的~/.ssh/authorized_keys文件中。
-
#ssh-keygen -t rsa 一路回车,生成无密码的密钥对。
将公钥添加到认证文件中:
#cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys文件并设置authorized_keys的访问权限:
#chmod 600 ~/.ssh/authorized_keys -
设置容器root密码
-
echo "root:password"|chpasswd
-
启动sshd服务并退出容器
-
/usr/sbin/sshd -D exit
-
运行docker ps -a 获取刚刚操作的容器的ID,然后提交该容器成为一个镜像文件:docker commit 刚刚得到的ID 新镜像的名称。
-
查看新生成的镜像 docker images ,运行后可以看到自己的镜像。
-
运行新生成的镜像。
-
docker run -p 10000:22 -d imasgeName /usr/sbin/sshd -D 容器的端口会映射到10000端口。
-
本地测试。ssh localhost -p 10000 即可登录到容器
docker中创建支持ssh的Centos镜像
最新推荐文章于 2024-07-16 17:15:49 发布